Skip to content

Commit

Permalink
Исправление видов и происхождения.
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalii Ungurean committed Feb 25, 2025
1 parent d06b916 commit ff6dd40
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/main/java/club/ttg/dnd5/dto/base/BaseUrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import java.util.ArrayList;
import java.util.List;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Getter
@Setter
public abstract class BaseUrl {
private String url;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonProperty(value = "image")
private String imageUrl;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/club/ttg/dnd5/dto/species/LinkedSpeciesDto.java

This file was deleted.

11 changes: 8 additions & 3 deletions src/main/java/club/ttg/dnd5/dto/species/SpeciesDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ public class SpeciesDto extends BaseDTO implements DetailableDTO, GroupStrategy
private CreaturePropertiesDto creatureProperties = new CreaturePropertiesDto();
private String linkImageUrl;
// Связанные сущности
private LinkedSpeciesDto parent = new LinkedSpeciesDto();
private Collection<LinkedSpeciesDto> subspecies = new LinkedHashSet<>();
private SpeciesDto parent;
/**
* Происхождения.
*/
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private Collection<SpeciesDto> lineages = new LinkedHashSet<>();

private Collection<SpeciesFeatureDto> features;
private NameBasedDTO group = new NameBasedDTO();
@JsonIgnore
Expand All @@ -43,7 +48,7 @@ public void hideDetails() {
linkImageUrl = null;
this.creatureProperties = null;
this.parent = null;
this.subspecies = null;
this.lineages = null;
this.features = null;
this.group = null;
setDescription(null);
Expand Down
23 changes: 10 additions & 13 deletions src/main/java/club/ttg/dnd5/service/species/SpeciesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import club.ttg.dnd5.dto.base.NameBasedDTO;
import club.ttg.dnd5.dto.base.create.SourceReference;
import club.ttg.dnd5.dto.species.CreateSpeciesDto;
import club.ttg.dnd5.dto.species.LinkedSpeciesDto;
import club.ttg.dnd5.dto.species.SpeciesCreateFeatureDto;
import club.ttg.dnd5.dto.species.SpeciesDto;
import club.ttg.dnd5.exception.ApiException;
Expand All @@ -14,7 +13,6 @@
import club.ttg.dnd5.model.book.Source;
import club.ttg.dnd5.model.species.Species;
import club.ttg.dnd5.model.species.SpeciesFeature;
//import club.ttg.dnd5.repository.SpeciesFeatureRepository;
import club.ttg.dnd5.repository.SpeciesRepository;
import club.ttg.dnd5.repository.TagRepository;
import club.ttg.dnd5.repository.book.BookRepository;
Expand All @@ -41,7 +39,7 @@ public class SpeciesService {
private final SpeciesRepository speciesRepository;
private final SourceRepository sourceRepository;
private final BookRepository bookRepository;
//private final SpeciesFeatureRepository speciesFeatureRepository;

private final TagRepository tagRepository;
// Public methods
public SpeciesDto findById(String url) {
Expand Down Expand Up @@ -253,12 +251,11 @@ private SpeciesDto toDTO(Species species, boolean hideDetails) {
return dto;
}


private void handleParentAndChild(Species species, SpeciesDto dto) {
//parent
Species speciesParent = species.getParent();
if (speciesParent != null) {
LinkedSpeciesDto parent = new LinkedSpeciesDto();
SpeciesDto parent = new SpeciesDto();
// Set the URL
parent.setUrl(speciesParent.getUrl());

Expand All @@ -270,16 +267,16 @@ private void handleParentAndChild(Species species, SpeciesDto dto) {
.build();

// Set the NameBasedDTO in the parent
parent.setName(parentNameBased);
parent.setNameBasedDTO(parentNameBased);
dto.setParent(parent);
}

Collection<Species> speciesSubSpecies = species.getSubSpecies();
if (speciesSubSpecies != null) {
// Convert each sub-species to a LinkedSpeciesDto
List<LinkedSpeciesDto> subSpeciesDtos = speciesSubSpecies.stream()
List<SpeciesDto> lineages = speciesSubSpecies.stream()
.map(subSpecies -> {
LinkedSpeciesDto linkedSpeciesDto = new LinkedSpeciesDto();
SpeciesDto linkedSpeciesDto = new SpeciesDto();

// Set the URL
linkedSpeciesDto.setUrl(subSpecies.getUrl());
Expand All @@ -292,14 +289,14 @@ private void handleParentAndChild(Species species, SpeciesDto dto) {
.build();

// Set the NameBasedDTO
linkedSpeciesDto.setName(nameBasedDTO);
linkedSpeciesDto.setNameBasedDTO(nameBasedDTO);

return linkedSpeciesDto;
})
.toList();

// Set the list of LinkedSpeciesDto objects
dto.setSubspecies(subSpeciesDtos);
dto.setLineages(lineages);
}
}

Expand All @@ -311,7 +308,7 @@ private SpeciesDto getSpeciesResponse(SpeciesDto speciesDTO) {
}

private void fillSpecies(SpeciesDto speciesDTO, Species species) {
Optional.ofNullable(speciesDTO.getSubspecies())
Optional.ofNullable(speciesDTO.getLineages())
.ifPresent(subSpeciesDtos -> species.setSubSpecies(
subSpeciesDtos.stream()
.map(this::convertToSpecies) // Convert LinkedSpeciesDto to Species
Expand Down Expand Up @@ -393,14 +390,14 @@ private SpeciesFeature convertingSpeciesCreateFeatureToSpeciesFeature(SpeciesCre
* @param linkedSpeciesDto the LinkedSpeciesDto to convert
* @return the corresponding Species entity
*/
private Species convertToSpecies(LinkedSpeciesDto linkedSpeciesDto) {
private Species convertToSpecies(SpeciesDto linkedSpeciesDto) {
Species subSpecies = findByUrl(linkedSpeciesDto.getUrl()); // Find existing species by URL
if (subSpecies == null) {
throw new IllegalArgumentException("No species found for URL: " + linkedSpeciesDto.getUrl());
}

// Update additional fields if needed
NameBasedDTO nameBasedDTO = linkedSpeciesDto.getName();
NameBasedDTO nameBasedDTO = linkedSpeciesDto.getNameBasedDTO();
if (nameBasedDTO != null) {
subSpecies.setName(nameBasedDTO.getName());
subSpecies.setShortName(nameBasedDTO.getShortName());
Expand Down

0 comments on commit ff6dd40

Please sign in to comment.