Skip to content

Commit

Permalink
- убрал ненужный интерфейс
Browse files Browse the repository at this point in the history
- исправил использование Source
- Подправил на основе этого конвертеры
  • Loading branch information
ProtectorRTD committed Nov 17, 2024
1 parent bf5dece commit 05d5407
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 153 deletions.
21 changes: 0 additions & 21 deletions src/main/java/club/ttg/dnd5/dto/base/HasSourceDTO.java

This file was deleted.

11 changes: 1 addition & 10 deletions src/main/java/club/ttg/dnd5/dto/base/SourceResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,9 @@
@AllArgsConstructor
@NoArgsConstructor
@JsonRootName("source")
public class SourceResponse implements HasSourceDTO {
public class SourceResponse {
private NameBasedDTO name = new NameBasedDTO();
private Short page;
@Override
public String getSource() {
return name.getShortName();
}

@Override
public void setSource(String sourceArcronym) {
this.name.setShortName(sourceArcronym);
}
private boolean homebrew = false;
private boolean thirdParty = false;
}
26 changes: 1 addition & 25 deletions src/main/java/club/ttg/dnd5/dto/species/CreateSpeciesDto.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package club.ttg.dnd5.dto.species;

import club.ttg.dnd5.dto.base.BaseDTO;
import club.ttg.dnd5.dto.base.HasSourceDTO;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand All @@ -15,29 +13,7 @@
@Setter
@AllArgsConstructor
@RequiredArgsConstructor
public class CreateSpeciesDto extends BaseDTO implements HasSourceDTO {
public class CreateSpeciesDto extends BaseDTO {
private CreaturePropertiesDto creatureProperties = new CreaturePropertiesDto();
private Collection<SpeciesFeatureDto> features = new ArrayList<>();

@JsonIgnore
@Override
public Short getPage() {
return (this.getSource() != null) ? this.getSourceDTO().getPage() : -1;
}

@Override
public void setPage(Short page) {
this.getSourceDTO().setPage(page);
}

@JsonIgnore
@Override
public String getSource() {
return this.getSourceDTO().getSource();
}

@Override
public void setSource(String source) {
this.getSourceDTO().setSource(source);
}
}
23 changes: 1 addition & 22 deletions src/main/java/club/ttg/dnd5/dto/species/SpeciesDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import club.ttg.dnd5.dto.base.BaseDTO;
import club.ttg.dnd5.dto.base.DetailableDTO;
import club.ttg.dnd5.dto.base.HasSourceDTO;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
Expand All @@ -18,7 +17,7 @@
@Setter
@AllArgsConstructor
@RequiredArgsConstructor
public class SpeciesDto extends BaseDTO implements DetailableDTO, HasSourceDTO{
public class SpeciesDto extends BaseDTO implements DetailableDTO{
// Включаем свойства существа через DTO
private CreaturePropertiesDto creatureProperties = new CreaturePropertiesDto();
// Связанные сущности
Expand All @@ -37,25 +36,5 @@ public void hideDetails() {
this.features = null;
}
}

@Override
public String getSource() {
return this.getSourceDTO().getSource();
}

@Override
public Short getPage() {
return this.getSourceDTO().getPage();
}

@Override
public void setPage(Short page) {
this.getSourceDTO().setPage(page);
}

@Override
public void setSource(String source) {
this.getSourceDTO().setSource(source);
}
}

23 changes: 1 addition & 22 deletions src/main/java/club/ttg/dnd5/dto/species/SpeciesFeatureDto.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package club.ttg.dnd5.dto.species;

import club.ttg.dnd5.dto.base.BaseDTO;
import club.ttg.dnd5.dto.base.HasSourceDTO;
import club.ttg.dnd5.dto.base.TagDto;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -14,27 +13,7 @@
@JsonInclude(JsonInclude.Include.NON_NULL)
@Getter
@Setter
public class SpeciesFeatureDto extends BaseDTO implements HasSourceDTO {
public class SpeciesFeatureDto extends BaseDTO {
@JsonProperty("tags")
private Collection<TagDto> tags = new ArrayList<>();

@Override
public String getSource() {
return this.getSourceDTO().getSource();
}

@Override
public Short getPage() {
return this.getSourceDTO().getPage();
}

@Override
public void setPage(Short page) {
this.getSourceDTO().setPage(page);
}

@Override
public void setSource(String source) {
this.getSourceDTO().setSource(source);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public SpeciesDto save(CreateSpeciesDto createSpeciesDTO) {
Species species = new Species();
Converter.MAP_BASE_DTO_TO_ENTITY_NAME.apply(createSpeciesDTO, species);
Converter.MAP_CREATURE_PROPERTIES_DTO_TO_ENTITY.apply(createSpeciesDTO.getCreatureProperties(), species);
Converter.MAP_DTO_SOURCE_TO_ENTITY_SOURCE.apply(createSpeciesDTO, species);
Converter.MAP_DTO_SOURCE_TO_ENTITY_SOURCE.apply(createSpeciesDTO.getSourceDTO(), species);

validateAndSaveSource(species.getSource());
saveSpeciesFeatures(createSpeciesDTO, species);
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/club/ttg/dnd5/utills/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import club.ttg.dnd5.dto.base.BaseDTO;
import club.ttg.dnd5.dto.base.DetailableDTO;
import club.ttg.dnd5.dto.base.HasSourceDTO;
import club.ttg.dnd5.dto.base.NameBasedDTO;
import club.ttg.dnd5.dto.base.SourceResponse;
import club.ttg.dnd5.dto.species.CreaturePropertiesDto;
import club.ttg.dnd5.dto.species.MovementAttributes;
import club.ttg.dnd5.model.base.CreatureProperties;
Expand Down Expand Up @@ -73,8 +73,8 @@ public class Converter {
};

// Function to map DTO Source to Entity Source
public static final BiFunction<HasSourceDTO, HasSourceEntity, HasSourceEntity> MAP_DTO_SOURCE_TO_ENTITY_SOURCE = (dto, entity) -> {
String sourceAcronym = dto.getSource();
public static final BiFunction<SourceResponse, HasSourceEntity, HasSourceEntity> MAP_DTO_SOURCE_TO_ENTITY_SOURCE = (dto, entity) -> {
String sourceAcronym = dto.getName().getShortName();
Source source = entity.getSource();
if (source == null) {
source = new Source();
Expand All @@ -90,10 +90,19 @@ public class Converter {
};

// Function to map Entity Source to DTO Source
public static final BiFunction<HasSourceDTO, HasSourceEntity, HasSourceDTO> MAP_ENTITY_SOURCE_TO_DTO_SOURCE = (dto, entity) -> {
if (entity.getSource() != null) {
dto.setSource(entity.getSource().getSourceAcronym());
dto.setPage(entity.getSource().getPage());
public static final BiFunction<SourceResponse, HasSourceEntity, SourceResponse> MAP_ENTITY_SOURCE_TO_DTO_SOURCE = (dto, entity) -> {
Source source = entity.getSource();
if (source != null) {
NameBasedDTO name = new NameBasedDTO();
Book bookInfo = source.getBookInfo();
if (bookInfo != null) {
name.setEnglish(bookInfo.getEnglishName());
name.setName(bookInfo.getName());
name.setShortName(bookInfo.getSourceAcronym());
name.setAlternative(bookInfo.getAltName());
}
dto.setName(name);
dto.setPage(source.getPage());
}
return dto;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class SpeciesFeatureConverter {
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);
Converter.MAP_DTO_SOURCE_TO_ENTITY_SOURCE.apply(response.getSourceDTO(), speciesFeature);

// Convert tags from TagDto to Map<String, String>
Map<String, String> tags = response.getTags().stream()
Expand All @@ -34,7 +34,7 @@ public class SpeciesFeatureConverter {
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);
Converter.MAP_ENTITY_SOURCE_TO_DTO_SOURCE.apply(dto.getSourceDTO(), feature);

// Convert tags from Map<String, String> to a collection of TagDto
Collection<TagDto> tags = feature.getTags().entrySet().stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,54 +1,67 @@
{
"url": "forest-gnome",
"imageUrl": "gnome/forest/picture",
"imageUrl": "gnome/forest/picture.webp",
"description": "Лесные гномы живут в лесах и наделены магией иллюзий, чтобы защищаться и выживать.",
"name": {
"name": "Лесной Гном",
"english": "Forest Gnome",
"alternative": "Woodland Trickster"
},
"sourceDTO": {
"source": "PHB",
"page": 181
},
"creatureProperties": {
"size": "SMALL",
"size": "MEDIUM",
"type": "HUMANOID",
"speed": 30,
"fly": 0,
"climb": 0,
"swim": 0,
"darkVision": 60,
"sourceResponse": {
"source": "PHB",
"page": 181
"speed": {
"base": 30,
"fly": 0,
"climb": 0,
"swim": 0
}
},
"parentUrl": "gnome",
"subSpeciesUrls": [],
"features": [
{
"url": "forest-gnome/natural-illusionist",
"imageUrl": null,
"imageUrl": "gnome/forest/natural-illusionist.webp",
"description": "Лесные гномы умеют использовать Малую Иллюзию для маскировки и защиты. Вы знаете заговор Малая Иллюзия.",
"name": {
"name": "Природный Иллюзионист",
"english": "Natural Illusionist",
"alternative": null
"rus": "Природный Иллюзионист",
"eng": "Natural Illusionist",
"alt": "Woodland Trickster",
"short": "PHB"
},
"sourceDTO": {
"source": "PHB",
"page": 181
"source": {
"name": {
"rus": "Книга игрока",
"eng": "Player's Handbook",
"alt": "PHB",
"short": "PHB"
},
"page": 181,
"homebrew": true,
"thirdParty": true
},
"tags": {
"magic": "illusion",
"gnome": "racial feature"
},
"source": "PHB",
"page": 181
"tags": [
{
"name": "magic",
"value": "resistance"
},
{
"name": "gnome",
"value": "racial feature"
}
]
}
],
"source": "PHB",
"detail": true,
"page": 181
}
"name": {
"rus": "Лесной Гном",
"eng": "Forest Gnome",
"alt": "Woodland Trickster",
"short": "PHB"
},
"source": {
"name": {
"rus": "Книга игрока",
"eng": "Player's Handbook",
"alt": "PHB",
"short": "PHB"
},
"page": 181,
"homebrew": true,
"thirdParty": true
}
}
13 changes: 7 additions & 6 deletions src/main/tests/club/ttg/dnd5/utills/ConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import club.ttg.dnd5.dictionary.Size;
import club.ttg.dnd5.dictionary.beastiary.CreatureType;
import club.ttg.dnd5.dto.base.BaseDTO;
import club.ttg.dnd5.dto.base.HasSourceDTO;
import club.ttg.dnd5.dto.base.NameBasedDTO;
import club.ttg.dnd5.dto.base.SourceResponse;
import club.ttg.dnd5.dto.species.CreateSpeciesDto;
Expand Down Expand Up @@ -75,9 +74,11 @@ public void setUp() {
creatureProperties.setDarkVision(60);

SourceResponse sourceResponse = new SourceResponse();
sourceResponse.setSource("PHB");
NameBasedDTO nameBasedDTO1 = new NameBasedDTO("Книга игрока", "Player HandBook", "", "PHB");
sourceResponse.setName(nameBasedDTO1);
sourceResponse.setPage((short) 155);
speciesDTO = new SpeciesDto();
sourceResponse.setName(nameBasedDTO1);
speciesDTO.setSourceDTO(sourceResponse);

source = new Source();
Expand Down Expand Up @@ -143,20 +144,20 @@ public void testMapEntityToCreaturePropertiesDTO() {
@Test
public void testMapDtoSourceToEntitySource() {
Species species = new Species();
Converter.MAP_DTO_SOURCE_TO_ENTITY_SOURCE.apply(speciesDTO, species);
Converter.MAP_DTO_SOURCE_TO_ENTITY_SOURCE.apply(speciesDTO.getSourceDTO(), species);

assertEquals((Short) species.getSource().getPage(), speciesDTO.getSourceDTO().getPage());
assertEquals(species.getSource().getBookInfo().getSourceAcronym(), speciesDTO.getSourceDTO().getSource());
assertEquals(species.getSource().getBookInfo().getSourceAcronym(), speciesDTO.getSourceDTO().getName().getShortName());
}

@Test
public void testMapEntitySourceToDtoSource() {
Species species = new Species();
species.setSource(source);
HasSourceDTO apply = Converter.MAP_ENTITY_SOURCE_TO_DTO_SOURCE.apply(speciesDTO, species);
SourceResponse apply = Converter.MAP_ENTITY_SOURCE_TO_DTO_SOURCE.apply(speciesDTO.getSourceDTO(), species);

assertEquals((Short) species.getSource().getPage(), apply.getPage());
assertEquals(species.getSource().getBookInfo().getSourceAcronym(), apply.getSource());
assertEquals(species.getSource().getBookInfo().getSourceAcronym(), apply.getName().getShortName());
}

@Test
Expand Down

0 comments on commit 05d5407

Please sign in to comment.