Skip to content

Commit

Permalink
- added TagDto, refactor to use correct.
Browse files Browse the repository at this point in the history
  • Loading branch information
ProtectorRTD committed Nov 17, 2024
1 parent 2051f1d commit 3ef4395
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
15 changes: 15 additions & 0 deletions src/main/java/club/ttg/dnd5/dto/base/TagDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package club.ttg.dnd5.dto.base;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Getter
@Setter
@AllArgsConstructor
public class TagDto {
private String name;
private String value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

import club.ttg.dnd5.dto.base.BaseDTO;
import club.ttg.dnd5.dto.base.HasSourceDTO;
import club.ttg.dnd5.dto.base.TagDto;
import club.ttg.dnd5.model.base.HasTags;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.Collection;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Getter
@Setter
public class SpeciesFeatureDto extends BaseDTO implements HasTags, HasSourceDTO {
private Map<String, String> tags = new HashMap<>();
@JsonProperty("tags")
private Collection<TagDto> tags = new ArrayList<>();

@Override
public String getSource() {
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/club/ttg/dnd5/model/species/Species.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ public class Species extends CreatureProperties implements HasSourceEntity {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id")
private Species parent;

@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private Collection<Species> subSpecies = new ArrayList<>();

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "source")
private Source source = new Source();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package club.ttg.dnd5.utills.species;

import club.ttg.dnd5.dto.base.TagDto;
import club.ttg.dnd5.dto.species.SpeciesFeatureDto;
import club.ttg.dnd5.model.species.Species;
import club.ttg.dnd5.model.species.SpeciesFeature;
Expand All @@ -8,25 +9,39 @@
import lombok.NoArgsConstructor;

import java.util.Collection;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class SpeciesFeatureConverter {
// Converter functions
// Converts SpeciesFeatureDto to SpeciesFeature
private static final BiFunction<SpeciesFeatureDto, SpeciesFeature, SpeciesFeature> DTO_TO_ENTITY_CONVERTER =
(response, speciesFeature) -> {
Converter.MAP_BASE_DTO_TO_ENTITY_NAME.apply(response, speciesFeature);
Converter.MAP_DTO_SOURCE_TO_ENTITY_SOURCE.apply(response, speciesFeature);
speciesFeature.setTags(response.getTags());

// Convert tags from TagDto to Map<String, String>
Map<String, String> tags = response.getTags().stream()
.collect(Collectors.toMap(TagDto::getName, TagDto::getValue));
speciesFeature.setTags(tags);

speciesFeature.setFeatureDescription(response.getDescription());
return speciesFeature;
};

// Converts SpeciesFeature to SpeciesFeatureDto
private static final BiFunction<SpeciesFeature, SpeciesFeatureDto, SpeciesFeatureDto> ENTITY_TO_DTO_CONVERTER =
(feature, dto) -> {
Converter.MAP_ENTITY_TO_BASE_DTO.apply(dto, feature);
Converter.MAP_ENTITY_SOURCE_TO_DTO_SOURCE.apply(dto, feature);
dto.setTags(feature.getTags());

// Convert tags from Map<String, String> to a collection of TagDto
Collection<TagDto> tags = feature.getTags().entrySet().stream()
.map(entry -> new TagDto(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());
dto.setTags(tags);

dto.setDescription(feature.getFeatureDescription());
return dto;
};
Expand Down

0 comments on commit 3ef4395

Please sign in to comment.