Skip to content

Commit

Permalink
Merge branch 'dev' into feature/item
Browse files Browse the repository at this point in the history
  • Loading branch information
Magistrus authored Feb 17, 2025
2 parents e9b1de1 + 38d4f96 commit d885590
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.List;

/**
* Различные справочники
Expand Down Expand Up @@ -312,4 +314,63 @@ public Collection<SelectOptionDto> getFeatTypes() {
public Collection<SpellcasterOptionDto> getSpellcasterTypes() {
return dictionariesService.getSpellcasterTypes();
}

@Operation(summary = "Единицы времени")
@GetMapping("/time-units")
@ApiResponse(
responseCode = "200",
content = @Content(mediaType = "application/json",
examples = @ExampleObject("""
[
{ "label": "бонусное действие", "value": "BONUS" },
{ "label": "реакция", "value": "REACTION" },
{ "label": "действие", "value": "ACTION" },
{ "label": "ход", "value": "ROUND" },
{ "label": "минута", "value": "MINUTE" },
{ "label": "час", "value": "HOUR" }
]
""")
)
)
public ResponseEntity<List<SelectOptionDto>> getTimeUnits() {
return ResponseEntity.ok(dictionariesService.getTimeUnits());
}


@Operation(summary = "Школы магии")
@GetMapping("/magic-schools")
@ApiResponse(
responseCode = "200",
content = @Content(mediaType = "application/json",
examples = @ExampleObject("""
[
{ "label": "вызов", "value": "CONJURATION" },
{ "label": "воплощение", "value": "EVOCATION" },
{ "label": "иллюзия", "value": "ILLUSION" },
{ "label": "некромантия", "value": "NECROMANCY" }
]
""")
)
)
public ResponseEntity<List<SelectOptionDto>> getMagicSchools() {
return ResponseEntity.ok(dictionariesService.getMagicSchools());
}

@Operation(summary = "Операторы сравнения")
@GetMapping("/comparison-operators")
@ApiResponse(
responseCode = "200",
content = @Content(mediaType = "application/json",
examples = @ExampleObject("""
[
{ "label": "<", "value": "LESS" },
{ "label": ">", "value": "GREATER" },
{ "label": "=", "value": "EQUAL" }
]
""")
)
)
public ResponseEntity<List<SelectOptionDto>> getComparisonOperators() {
return ResponseEntity.ok(dictionariesService.getComparisonOperators());
}
}
1 change: 1 addition & 0 deletions src/main/java/club/ttg/dnd5/dto/species/SpeciesDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public void hideDetails() {
this.subspecies = null;
this.features = null;
this.group = null;
setDescription(null);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package club.ttg.dnd5.model.spell.component;

import club.ttg.dnd5.model.spell.enums.ComparisonOperator;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
public class SpellMaterialComponent {
private String name; // Название компонента
private Integer price; // Цена (используем Integer вместо int, чтобы допустить null)
private ComparisonOperator comparison; // Оператор сравнения
private boolean consumable; // Расходуемый или нет
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package club.ttg.dnd5.model.spell.enums;

public enum ComparisonOperator {
LESS("<"),
GREATER(">"),
EQUAL("=");

private final String symbol;

ComparisonOperator(String symbol) {
this.symbol = symbol;
}

public String getSymbol() {
return symbol;
}

public static ComparisonOperator fromSymbol(String symbol) {
for (ComparisonOperator op : values()) {
if (op.symbol.equals(symbol)) {
return op;
}
}
throw new IllegalArgumentException("Invalid comparison operator: " + symbol);
}
}

40 changes: 40 additions & 0 deletions src/main/java/club/ttg/dnd5/model/spell/enums/MagicSchool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package club.ttg.dnd5.model.spell.enums;

import club.ttg.dnd5.exception.ApiException;
import org.springframework.http.HttpStatus;

public enum MagicSchool {
CONJURATION("вызов", 0),
EVOCATION("воплощение", 1),
ILLUSION("иллюзия", 2),
NECROMANCY("некромантия", 3),
ABJURATION("ограждение", 4),
ENCHANTMENT("очарование", 5),
TRANSMUTATION("преобразование", 6),
DIVINATION("прорицание", 7);

private String name;
private int code;

MagicSchool(String name, int code) {
this.name = name;
this.code = code;
}

public String getName() {
return name;
}

public int getCode() {
return code;
}

public static MagicSchool getMagicSchool(String name) {
for (MagicSchool school : values()) {
if (school.name.equalsIgnoreCase(name)) {
return school;
}
}
throw new ApiException(HttpStatus.NOT_FOUND, "Неправильное название школы магии");
}
}
42 changes: 42 additions & 0 deletions src/main/java/club/ttg/dnd5/model/spell/enums/TimeUnit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package club.ttg.dnd5.model.spell.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum TimeUnit {
BONUS("бонусное действие"),
REACTION("реакция"),
ACTION("действие"),
ROUND("ход"),
MINUTE("минута"),
HOUR("час");

private final String name;

public String getName(int number) {
return switch (this) {
case MINUTE -> formatTime(number, "минута", "минуты", "минут");
case HOUR -> formatTime(number, "час", "часа", "часов");
default -> name;
};
}

private String formatTime(int number, String singular, String dual, String plural) {
int lastDigit = number % 10;
int lastTwoDigits = number % 100;

if (lastTwoDigits >= 11 && lastTwoDigits <= 14) {
return plural;
}

if (lastDigit == 1) {
return singular;
} else if (lastDigit >= 2 && lastDigit <= 4) {
return dual;
} else {
return plural;
}
}
}
6 changes: 4 additions & 2 deletions src/main/java/club/ttg/dnd5/repository/SpeciesRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Collection;
import java.util.Optional;

@Repository
public interface SpeciesRepository extends JpaRepository<Species, String>, JpaSpecificationExecutor<Species> {
List<Species> findByParent(Species parent);
Collection<Species> findByParent(Species parent);
Optional<Species> findByNameIgnoreCase(String name);

Collection<Species> findAllByParentIsNull();
}
31 changes: 31 additions & 0 deletions src/main/java/club/ttg/dnd5/service/base/DictionariesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,45 @@
import club.ttg.dnd5.dto.select.DiceOptionDto;
import club.ttg.dnd5.dto.select.SelectOptionDto;
import club.ttg.dnd5.dto.select.SpellcasterOptionDto;
import club.ttg.dnd5.model.spell.enums.ComparisonOperator;
import club.ttg.dnd5.model.spell.enums.MagicSchool;
import club.ttg.dnd5.model.spell.enums.TimeUnit;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DictionariesService {
public List<SelectOptionDto> getTimeUnits() {
return Arrays.stream(TimeUnit.values())
.map(unit -> SelectOptionDto.builder()
.label(unit.getName())
.value(unit.name())
.build())
.collect(Collectors.toList());
}

public List<SelectOptionDto> getMagicSchools() {
return Arrays.stream(MagicSchool.values())
.map(school -> SelectOptionDto.builder()
.label(school.getName())
.value(school.name())
.build())
.collect(Collectors.toList());
}

public List<SelectOptionDto> getComparisonOperators() {
return Arrays.stream(ComparisonOperator.values())
.map(op -> SelectOptionDto.builder()
.label(op.getSymbol())
.value(op.name())
.build())
.collect(Collectors.toList());
}

private SelectOptionDto createBaseOptionDTO(String label, String value) {
return SelectOptionDto.builder()
.label(label)
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/club/ttg/dnd5/service/book/BookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ private Book convertingCreateSourceToEntity(SourceBookDTO sourceBookDTO) {
if (StringUtils.isBlank(sourceBookDTO.getUrl())) {
throw new ApiException(HttpStatus.BAD_REQUEST, "Отсутствует обязательное поле `url`");
}
if (StringUtils.isBlank(sourceBookDTO.getUrl())) {
throw new ApiException(HttpStatus.INTERNAL_SERVER_ERROR, "Должен быть указан url");
}
return Book.builder()
.bookDate(sourceBookDTO.getYear())
.sourceAcronym(name.getShortName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
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.SpeciesFeatureRepository;
import club.ttg.dnd5.repository.SpeciesRepository;
import club.ttg.dnd5.repository.TagRepository;
import club.ttg.dnd5.repository.book.BookRepository;
Expand All @@ -41,7 +41,7 @@ public class SpeciesService {
private final SpeciesRepository speciesRepository;
private final SourceRepository sourceRepository;
private final BookRepository bookRepository;
private final SpeciesFeatureRepository speciesFeatureRepository;
//private final SpeciesFeatureRepository speciesFeatureRepository;
private final TagRepository tagRepository;
// Public methods
public SpeciesDto findById(String url) {
Expand All @@ -55,7 +55,8 @@ public Boolean isExist(String url) {
}

public List<SpeciesDto> getAllSpecies() {
return speciesRepository.findAll()
// только parent и убрать лишнюю детальную информацию
return speciesRepository.findAllByParentIsNull()
.stream()
.map(species -> toDTO(species, true))
.toList();
Expand Down

0 comments on commit d885590

Please sign in to comment.