Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refatoração e serviços #31

Merged
merged 4 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/br/com/cpsb/controller/CpsbApiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
134 changes: 39 additions & 95 deletions src/main/java/br/com/cpsb/controller/PetController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Pet> pets = petRepository.findAll();
ModelAndView mv = new ModelAndView("pet_lista");
List<Pet> 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<Raca> 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<String> racas = racaRepository.findAll().stream().map(Raca::getNome).toList();
mv.addObject("lista_racas", racas);

List<String> 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<String> 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<String> foundRacas = racaService.findAllNomes();
mv.addObject("lista_racas", foundRacas);
return mv;
}

Optional<Pet> petOptional = petRepository.findById(id);

if (petOptional.isPresent()) {
Pet pet = petOptional.get();
pet.setNome(newPet.getNome());

Optional<Raca> 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<Pet> petOptional = petRepository.findById(id);

if (petOptional.isPresent()) {
Pet pet = petOptional.get();
ModelAndView mv = new ModelAndView("formulario_atualizar_pet");
mv.addObject("pet", pet);
Optional<Pet> petOptional = service.findById(id);

List<String> 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<String> foundRacas = racaService.findAllNomes();
mv.addObject("lista_racas", foundRacas);

return mv;
}

//Detalhes de pet
@GetMapping("/detalhes_pet/{id}")
public ModelAndView detalhesPet(@PathVariable Long id) {
Optional<Pet> petOptional = petRepository.findById(id);

if (petOptional.isPresent()) {
Pet pet = petOptional.get();

ModelAndView mv = new ModelAndView("detalhes_pet");
mv.addObject("pet", pet);
Optional<Pet> 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<Pet> petOptional = petRepository.findById(id);

if (petOptional.isPresent()) {
petRepository.deleteById(id);
}

service.deleteById(id);
return "redirect:/lista_pets";
}

}
39 changes: 15 additions & 24 deletions src/main/java/br/com/cpsb/controller/RacaController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Raca> racas = racaRepository.findAll();
ModelAndView mv = new ModelAndView("raca_lista");
List<Raca> 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<Raca> racaOptional = racaRepository.findById(id);

if (racaOptional.isPresent()) {
Raca raca = racaOptional.get();

ModelAndView mv = new ModelAndView("detalhes_raca");
mv.addObject("raca", raca);
Optional<Raca> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public class UsuarioController {

@GetMapping("/login")
public String login() {
return "formulario_login_usuario";
return "login";
}
}
1 change: 0 additions & 1 deletion src/main/java/br/com/cpsb/model/Pet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) opcional
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/br/com/cpsb/repository/RacaRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Raca, Long> {
Optional<Raca> findByNome(String raca);
}
60 changes: 60 additions & 0 deletions src/main/java/br/com/cpsb/service/PetService.java
Original file line number Diff line number Diff line change
@@ -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<Pet> petOptional = repo.findById(id);

if (petOptional.isEmpty()) {
throw new RuntimeException("Pet não encontrado");
}

return petOptional.get();
}

public List<Pet> findAll() {
return repo.findAll();
}

public void save(Pet pet) {
Raca racaEncontrada = racaService.findRaca(pet.getRaca());
pet.setRaca(racaEncontrada);
repo.save(pet);
}

public Optional<Pet> findById(Long id) {
return repo.findById(id);
}

public void deleteById(Long id) {
Pet foundPet = findPet(id);
repo.deleteById(foundPet.getId());
}
}
Loading