Skip to content

Commit

Permalink
Created Breed 'None' and delete method for Breed entity
Browse files Browse the repository at this point in the history
  • Loading branch information
GEdO23 committed Oct 12, 2024
1 parent 4e39bfe commit 1553ebe
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/main/java/br/com/cpsb/controller/BreedController.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,10 @@ public ModelAndView breedDetails(@PathVariable Long id) {
mv.addObject("breed", breed);
return mv;
}

@GetMapping("/breed_remove/{id}")
public String breedRemove(@PathVariable Long id) {
service.delete(id);
return "redirect:/breed_list";
}
}
36 changes: 33 additions & 3 deletions src/main/java/br/com/cpsb/service/BreedService.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package br.com.cpsb.service;

import br.com.cpsb.model.Breed;
import br.com.cpsb.model.Pet;
import br.com.cpsb.repository.BreedRepository;
import br.com.cpsb.repository.PetRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Service
public class BreedService implements ServiceDto<Long, Breed>{
public class BreedService implements ServiceDto<Long, Breed> {

@Autowired
private BreedRepository repo;

@Autowired
private PetRepository petRepo;

public List<String> getBreedsNames() {
return repo.findAll()
.stream()
Expand All @@ -31,6 +37,16 @@ public Breed getByName(String name) {
return foundBreed.get();
}

public Breed getDefault() {
Optional<Breed> foundBreedNone = repo.findByName("None");

if (foundBreedNone.isEmpty()) {
throw new RuntimeException("BREED 'None' NOT FOUND");
}

return foundBreedNone.get();
}

@Override
public List<Breed> get() {
return repo.findAll();
Expand Down Expand Up @@ -60,8 +76,22 @@ public void put(Long id, Breed breed) {
public void delete(Long id) {
Optional<Breed> foundBreed = repo.findById(id);

if (foundBreed.isPresent()) {
repo.deleteById(id);
if (foundBreed.isEmpty()) {
throw new RuntimeException("BREED NOT FOUND");
}

Breed breed = foundBreed.get();
Breed breedNone = getDefault();

if (breed == breedNone) {
throw new RuntimeException("CANNOT DELETE BREED 'None'");
}

List<Pet> breedPets = petRepo.findAll().stream()
.filter(pet -> pet.getBreed() == breed).toList();

breedPets.forEach(pet -> pet.setBreed(breedNone));

repo.deleteById(id);
}
}
1 change: 1 addition & 0 deletions src/main/resources/import.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
INSERT INTO TB_CPSB_BREED (nm_breed, ds_breed) VALUES ( 'None', 'Este pet não possui uma raça' );
INSERT INTO TB_CPSB_BREED (nm_breed, ds_breed) VALUES ( 'SRD', 'Não apresenta origem definida.' );
INSERT INTO TB_CPSB_BREED (nm_breed, ds_breed) VALUES ( 'Buldogue', 'Trufa e narinas devem ser grandes, amplas e na cor preta, jamais de cor fígado, vermelha ou marrom. O focinho é achatado e largo, curvando-se para cima e muito profundo do canto do olho ao canto da boca. Possui prognatismo inferior. O pêlo é de textura fina, curto, fechado e liso. Cor de pelagem: Unicolor ou “smut” (com fuligem, isto é, de uma só cor com máscara preta ou focinho preto e um sombreamento na pelagem); Tigrado, fulvo, marrom claro, branco ou malhado (combinação de branco com qualquer das cores precedentes). As cores fígado (chocolate), preto e castanho (tricolor) são altamente indesejáveis. Peso: Machos 25 kg; fêmeas 23 kg. Alturaː Entre 31 e 40 centímetros na altura da cernelha.');
INSERT INTO TB_CPSB_BREED (nm_breed, ds_breed) VALUES ( 'Husky', 'A pelagem do Husky possui subpelo e é bastante variada em questão de cor, desde o completamente branco ao preto-e-branco, chocolate-e-branco, cinza-e-branco, além de outras variações.');
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/templates/breed_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ <h1 th:text="#{breed_list_page_title}"></h1>
<th scope="col" th:text="#{breed_name_label}"></th>
<th scope="col">#</th>
<th scope="col">#</th>
<th scope="col">#</th>
</tr>
</thead>
<tbody>
Expand All @@ -42,6 +43,10 @@ <h1 th:text="#{breed_list_page_title}"></h1>
<a th:href="@{/breed_form_update/{idBreed}(idBreed = ${breed.id})}"
th:text="#{update}"></a>
</td>
<td class="col-1">
<a th:href="@{/breed_remove/{idBreed}(idBreed = ${breed.id})}"
th:text="#{remove}"></a>
</td>
</tr>
</tbody>
</table>
Expand Down

0 comments on commit 1553ebe

Please sign in to comment.