diff --git a/src/main/java/br/com/cpsb/controller/CpsbApiController.java b/src/main/java/br/com/cpsb/controller/CpsbApiController.java index da0b753..b8fff7e 100644 --- a/src/main/java/br/com/cpsb/controller/CpsbApiController.java +++ b/src/main/java/br/com/cpsb/controller/CpsbApiController.java @@ -8,11 +8,11 @@ public class CpsbApiController { @GetMapping("/pagina_principal") public String index() { - return "index"; + return "pagina_principal"; } @GetMapping("/acesso_negado") public String acessoNegado() { - return "erro_403"; + return "error_403"; } } diff --git a/src/main/java/br/com/cpsb/controller/PetController.java b/src/main/java/br/com/cpsb/controller/PetController.java index 245ce57..08fe74d 100644 --- a/src/main/java/br/com/cpsb/controller/PetController.java +++ b/src/main/java/br/com/cpsb/controller/PetController.java @@ -1,9 +1,8 @@ package br.com.cpsb.controller; import br.com.cpsb.model.Pet; -import br.com.cpsb.model.Raca; -import br.com.cpsb.repository.PetRepository; -import br.com.cpsb.repository.RacaRepository; +import br.com.cpsb.service.PetService; +import br.com.cpsb.service.RacaService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; @@ -19,143 +18,88 @@ public class PetController { @Autowired - private PetRepository petRepository; + private PetService service; @Autowired - private RacaRepository racaRepository; + private RacaService racaService; - //Lista de pets @GetMapping("/lista_pets") public ModelAndView listaPets() { - ModelAndView mv = new ModelAndView("lista_pets"); - - List pets = petRepository.findAll(); + ModelAndView mv = new ModelAndView("pet_lista"); + List pets = service.findAll(); mv.addObject("pets", pets); - return mv; } - //Cadastro de pet @PostMapping("/cadastrar_pet") - public ModelAndView cadastrarPet(Pet newPet) { + public ModelAndView cadastrarPet(Pet pet) { ModelAndView mv = new ModelAndView("redirect:/lista_pets"); - - Optional racaOptional = racaRepository.findAll().stream().filter(raca -> - raca.getNome().equals(newPet.getRaca().getNome()) - ).findFirst(); - - if (racaOptional.isPresent()) { - newPet.setRaca(racaOptional.get()); - } else { - Raca raca = new Raca(); - raca.setNome(newPet.getRaca().getNome()); - raca.setDescricao("Descrição da raça " + newPet.getRaca().getNome()); - racaRepository.save(raca); - newPet.setRaca(raca); - } - - petRepository.save(newPet); - mv.addObject("pet", newPet); - + service.save(pet); + mv.addObject("pet", pet); return mv; } @GetMapping("/formulario_cadastrar_pet") public ModelAndView formularioCadastrarPet() { - ModelAndView mv = new ModelAndView("formulario_cadastrar_pet"); + ModelAndView mv = new ModelAndView("pet_form_cadastrar"); mv.addObject("pet", new Pet()); - - List racas = racaRepository.findAll().stream().map(Raca::getNome).toList(); - mv.addObject("lista_racas", racas); - + List foundRacas = racaService.findAllNomes(); + mv.addObject("lista_racas", foundRacas); return mv; } - //Edição de pet @PostMapping("/atualizar_pet/{id}") - public ModelAndView atualizarPet(@PathVariable Long id, Pet newPet, BindingResult bd) { + public ModelAndView atualizarPet(@PathVariable Long id, Pet pet, BindingResult bd) { if (bd.hasErrors()) { - ModelAndView mv = new ModelAndView("formulario_atualizar_pet"); - mv.addObject("pet", newPet); - - List racas = racaRepository.findAll().stream().map(Raca::getNome).toList(); - mv.addObject("lista_racas", racas); - + ModelAndView mv = new ModelAndView("pet_form_atualizar"); + mv.addObject("pet", pet); + List foundRacas = racaService.findAllNomes(); + mv.addObject("lista_racas", foundRacas); return mv; } - Optional petOptional = petRepository.findById(id); - - if (petOptional.isPresent()) { - Pet pet = petOptional.get(); - pet.setNome(newPet.getNome()); - - Optional racaOptional = racaRepository.findAll().stream().filter(raca -> - raca.getNome().equals(newPet.getRaca().getNome()) - ).findFirst(); - - if (racaOptional.isPresent()) { - pet.setRaca(racaOptional.get()); - } else { - Raca raca = new Raca(); - raca.setNome(newPet.getRaca().getNome()); - raca.setDescricao("Descrição da raça " + newPet.getRaca().getNome()); - racaRepository.save(raca); - pet.setRaca(raca); - } - - petRepository.save(pet); - } + Pet updatedPet = service.getUpdatedPet(id, pet); + service.save(updatedPet); return new ModelAndView("redirect:/lista_pets"); } @GetMapping("/formulario_atualizar_pet/{id}") public ModelAndView formularioAtualizarPet(@PathVariable Long id) { - Optional petOptional = petRepository.findById(id); - - if (petOptional.isPresent()) { - Pet pet = petOptional.get(); - ModelAndView mv = new ModelAndView("formulario_atualizar_pet"); - mv.addObject("pet", pet); + Optional petOptional = service.findById(id); - List racas = racaRepository.findAll().stream().map(Raca::getNome).toList(); - mv.addObject("lista_racas", racas); - - return mv; - } else { + if (petOptional.isEmpty()) { return new ModelAndView("redirect:/lista_pets"); } + + Pet pet = petOptional.get(); + + ModelAndView mv = new ModelAndView("pet_form_atualizar"); + mv.addObject("pet", pet); + List foundRacas = racaService.findAllNomes(); + mv.addObject("lista_racas", foundRacas); + + return mv; } - //Detalhes de pet @GetMapping("/detalhes_pet/{id}") public ModelAndView detalhesPet(@PathVariable Long id) { - Optional petOptional = petRepository.findById(id); - - if (petOptional.isPresent()) { - Pet pet = petOptional.get(); - - ModelAndView mv = new ModelAndView("detalhes_pet"); - mv.addObject("pet", pet); + Optional petOptional = service.findById(id); - return mv; - } else { + if (petOptional.isEmpty()) { return new ModelAndView("redirect:/lista_pets"); } - } + Pet pet = petOptional.get(); + ModelAndView mv = new ModelAndView("pet_detalhes"); + mv.addObject("pet", pet); + + return mv; + } - //Remover pet @GetMapping("/remover_pet/{id}") public String removerPet(@PathVariable Long id) { - Optional petOptional = petRepository.findById(id); - - if (petOptional.isPresent()) { - petRepository.deleteById(id); - } - + service.deleteById(id); return "redirect:/lista_pets"; } - } diff --git a/src/main/java/br/com/cpsb/controller/RacaController.java b/src/main/java/br/com/cpsb/controller/RacaController.java index b4dfbaa..1026aad 100644 --- a/src/main/java/br/com/cpsb/controller/RacaController.java +++ b/src/main/java/br/com/cpsb/controller/RacaController.java @@ -1,7 +1,7 @@ package br.com.cpsb.controller; import br.com.cpsb.model.Raca; -import br.com.cpsb.repository.RacaRepository; +import br.com.cpsb.service.RacaService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @@ -16,51 +16,42 @@ public class RacaController { @Autowired - private RacaRepository racaRepository; + private RacaService service; @GetMapping("/lista_racas") public ModelAndView listaRacas() { - ModelAndView mv = new ModelAndView("lista_racas"); - - List racas = racaRepository.findAll(); + ModelAndView mv = new ModelAndView("raca_lista"); + List racas = service.findAll(); mv.addObject("racas", racas); - return mv; } @PostMapping("/cadastrar_raca") - public ModelAndView cadastrarRaca(Raca new_raca) { + public ModelAndView cadastrarRaca(Raca raca) { ModelAndView mv = new ModelAndView("redirect:/lista_racas"); - - racaRepository.save(new_raca); - - mv.addObject("raca", new_raca); - + service.save(raca); + mv.addObject("raca", raca); return mv; } @GetMapping("/formulario_cadastrar_raca") public ModelAndView formularioCadastrarRaca() { - ModelAndView mv = new ModelAndView("formulario_cadastrar_raca"); - + ModelAndView mv = new ModelAndView("raca_form_cadastrar"); mv.addObject("raca", new Raca()); - return mv; } @GetMapping("/detalhes_raca/{id}") public ModelAndView detalhesRaca(@PathVariable Long id) { - Optional racaOptional = racaRepository.findById(id); - - if (racaOptional.isPresent()) { - Raca raca = racaOptional.get(); - - ModelAndView mv = new ModelAndView("detalhes_raca"); - mv.addObject("raca", raca); + Optional racaOptional = service.findById(id); - return mv; - } else { + if (racaOptional.isEmpty()) { return new ModelAndView("redirect:/lista_racas"); } + + Raca raca = racaOptional.get(); + ModelAndView mv = new ModelAndView("raca_detalhes"); + mv.addObject("raca", raca); + return mv; } } diff --git a/src/main/java/br/com/cpsb/controller/UsuarioController.java b/src/main/java/br/com/cpsb/controller/UsuarioController.java index 2ee4c70..2d75ec9 100644 --- a/src/main/java/br/com/cpsb/controller/UsuarioController.java +++ b/src/main/java/br/com/cpsb/controller/UsuarioController.java @@ -8,6 +8,6 @@ public class UsuarioController { @GetMapping("/login") public String login() { - return "formulario_login_usuario"; + return "login"; } } diff --git a/src/main/java/br/com/cpsb/model/Pet.java b/src/main/java/br/com/cpsb/model/Pet.java index 6c6d5dc..77f3023 100644 --- a/src/main/java/br/com/cpsb/model/Pet.java +++ b/src/main/java/br/com/cpsb/model/Pet.java @@ -27,7 +27,6 @@ public class Pet { private Raca raca; //TODO: Adicionar o restante dos atributos: - // - raca (Raca CLASS) // - especie (Especie CLASS) // - peso (Double) opcional // - observacoes (ArrayList) opcional diff --git a/src/main/java/br/com/cpsb/repository/RacaRepository.java b/src/main/java/br/com/cpsb/repository/RacaRepository.java index 333061f..8cba963 100644 --- a/src/main/java/br/com/cpsb/repository/RacaRepository.java +++ b/src/main/java/br/com/cpsb/repository/RacaRepository.java @@ -3,5 +3,9 @@ import br.com.cpsb.model.Raca; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; +import java.util.Optional; + public interface RacaRepository extends JpaRepository { + Optional findByNome(String raca); } diff --git a/src/main/java/br/com/cpsb/service/PetService.java b/src/main/java/br/com/cpsb/service/PetService.java new file mode 100644 index 0000000..00a7d96 --- /dev/null +++ b/src/main/java/br/com/cpsb/service/PetService.java @@ -0,0 +1,60 @@ +package br.com.cpsb.service; + +import br.com.cpsb.model.Pet; +import br.com.cpsb.model.Raca; +import br.com.cpsb.repository.PetRepository; +import lombok.val; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; + +@Service +public class PetService { + + @Autowired + private RacaService racaService; + + @Autowired + private PetRepository repo; + + public Pet getUpdatedPet(Long id, Pet updatedPet) { + Pet foundPet = findPet(id); + foundPet.setNome(updatedPet.getNome()); + + Raca raca = racaService.findRaca(updatedPet.getRaca()); + foundPet.setRaca(raca); + + return foundPet; + } + + public Pet findPet(Long id) { + Optional petOptional = repo.findById(id); + + if (petOptional.isEmpty()) { + throw new RuntimeException("Pet não encontrado"); + } + + return petOptional.get(); + } + + public List findAll() { + return repo.findAll(); + } + + public void save(Pet pet) { + Raca racaEncontrada = racaService.findRaca(pet.getRaca()); + pet.setRaca(racaEncontrada); + repo.save(pet); + } + + public Optional findById(Long id) { + return repo.findById(id); + } + + public void deleteById(Long id) { + Pet foundPet = findPet(id); + repo.deleteById(foundPet.getId()); + } +} diff --git a/src/main/java/br/com/cpsb/service/RacaService.java b/src/main/java/br/com/cpsb/service/RacaService.java new file mode 100644 index 0000000..de297ab --- /dev/null +++ b/src/main/java/br/com/cpsb/service/RacaService.java @@ -0,0 +1,42 @@ +package br.com.cpsb.service; + +import br.com.cpsb.model.Raca; +import br.com.cpsb.repository.RacaRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; + +@Service +public class RacaService { + + @Autowired + private RacaRepository repo; + + public Raca findRaca(Raca raca) { + Optional racaOptional = repo.findByNome(raca.getNome()); + + if (racaOptional.isEmpty()) { + throw new RuntimeException("Raça não encontrada"); + } + + return racaOptional.get(); + } + + public List findAllNomes() { + return repo.findAll().stream().map(Raca::getNome).toList(); + } + + public List findAll() { + return repo.findAll(); + } + + public void save(Raca raca) { + repo.save(raca); + } + + public Optional findById(Long id) { + return repo.findById(id); + } +} diff --git a/src/main/resources/messages_en.properties b/src/main/resources/messages_en.properties index ac76510..f9d98aa 100644 --- a/src/main/resources/messages_en.properties +++ b/src/main/resources/messages_en.properties @@ -15,7 +15,7 @@ endpoint_race_register=formulario_cadastrar_raca # Nav nav_pet_list=Pets -nav_race_list=Races +nav_race_list=Breeds btn_logout=Exit # Success/Error messages @@ -26,8 +26,8 @@ login_message_error=Invalid username or password login_user_name_label=Username login_user_password_label=Password pet_name_label=Name -pet_race_label=Race -pet_race_description_label=Race description +pet_race_label=Breed +pet_race_description_label=Breed description race_name_label=Name race_description_label=Description @@ -40,9 +40,9 @@ pet_register_page_title=Register Pet pet_update_page_title=Update Pet pet_details_page_title=Pet Details -race_list_page_title=Race List -race_register_page_title=Register Race -race_details_page_title=Race Details +race_list_page_title=Breed List +race_register_page_title=Register Breed +race_details_page_title=Breed Details error_page_title=Error @@ -56,13 +56,13 @@ details=Details update=Update remove=Remove -pet_register=Register pet -race_register=Register race +pet_register=Register new pet +race_register=Register new breed # Go to go_back_home=Go back to home page go_back_pet_list=Go back to pet list -go_back_race_list=Go back to race list +go_back_race_list=Go back to breed list # Errors error_403_title=Error 403! diff --git a/src/main/resources/templates/erro_403.html b/src/main/resources/templates/error_403.html similarity index 100% rename from src/main/resources/templates/erro_403.html rename to src/main/resources/templates/error_403.html diff --git a/src/main/resources/templates/formulario_login_usuario.html b/src/main/resources/templates/login.html similarity index 100% rename from src/main/resources/templates/formulario_login_usuario.html rename to src/main/resources/templates/login.html diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/pagina_principal.html similarity index 100% rename from src/main/resources/templates/index.html rename to src/main/resources/templates/pagina_principal.html diff --git a/src/main/resources/templates/detalhes_pet.html b/src/main/resources/templates/pet_detalhes.html similarity index 100% rename from src/main/resources/templates/detalhes_pet.html rename to src/main/resources/templates/pet_detalhes.html diff --git a/src/main/resources/templates/formulario_atualizar_pet.html b/src/main/resources/templates/pet_form_atualizar.html similarity index 100% rename from src/main/resources/templates/formulario_atualizar_pet.html rename to src/main/resources/templates/pet_form_atualizar.html diff --git a/src/main/resources/templates/formulario_cadastrar_pet.html b/src/main/resources/templates/pet_form_cadastrar.html similarity index 100% rename from src/main/resources/templates/formulario_cadastrar_pet.html rename to src/main/resources/templates/pet_form_cadastrar.html diff --git a/src/main/resources/templates/lista_pets.html b/src/main/resources/templates/pet_lista.html similarity index 100% rename from src/main/resources/templates/lista_pets.html rename to src/main/resources/templates/pet_lista.html diff --git a/src/main/resources/templates/detalhes_raca.html b/src/main/resources/templates/raca_detalhes.html similarity index 100% rename from src/main/resources/templates/detalhes_raca.html rename to src/main/resources/templates/raca_detalhes.html diff --git a/src/main/resources/templates/formulario_cadastrar_raca.html b/src/main/resources/templates/raca_form_cadastrar.html similarity index 100% rename from src/main/resources/templates/formulario_cadastrar_raca.html rename to src/main/resources/templates/raca_form_cadastrar.html diff --git a/src/main/resources/templates/lista_racas.html b/src/main/resources/templates/raca_lista.html similarity index 100% rename from src/main/resources/templates/lista_racas.html rename to src/main/resources/templates/raca_lista.html