Skip to content

Commit

Permalink
Default breed is automatically created and can be deleted when not be…
Browse files Browse the repository at this point in the history
…ing used
  • Loading branch information
GEdO23 committed Oct 17, 2024
1 parent cc153d0 commit 909ba24
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
13 changes: 12 additions & 1 deletion src/main/java/br/com/cpsb/model/Breed.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.naming.Name;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "TB_CPSB_BREED")
Expand All @@ -24,5 +25,15 @@ public class Breed {

@Column(name = "ds_breed", length = 1000)
public String description;

public Breed(String name) {
this.name = name;
this.description = "";
}

public Breed(String name, String description) {
this.name = name;
this.description = description;
}

}
36 changes: 23 additions & 13 deletions src/main/java/br/com/cpsb/service/BreedService.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ 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");
}
private Breed createDefault() {
Breed defaultBreed = new Breed("None", "Default breed");
this.post(defaultBreed);
return defaultBreed;
}

return foundBreedNone.get();
public Breed getDefault() {
Optional<Breed> foundDefaultBreed = repo.findByName("None");
return foundDefaultBreed.orElseGet(this::createDefault);
}

@Override
Expand Down Expand Up @@ -71,6 +72,15 @@ public void put(Long id, Breed breed) {
}
}

public Boolean canBeDeleted(Breed breed) {
Breed defaultBreed = getDefault();

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

return breed != defaultBreed || petsWithRemovedBreed.isEmpty();
}

@Override
public void delete(Long id) {
Optional<Breed> foundBreed = repo.findById(id);
Expand All @@ -80,17 +90,17 @@ public void delete(Long id) {
}

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

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

Breed defaultBreed = getDefault();

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

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

petsWithRemovedBreed.forEach(pet -> pet.setBreed(defaultBreed));
repo.deleteById(id);
}
}
1 change: 0 additions & 1 deletion src/main/resources/import.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
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

0 comments on commit 909ba24

Please sign in to comment.