Skip to content

Commit

Permalink
Merge pull request #49 from TTG-Club/feature/create-species-v2
Browse files Browse the repository at this point in the history
Создание видов
  • Loading branch information
svifty7 authored Jan 11, 2025
2 parents eae93b1 + c44c6ac commit f14ceb7
Show file tree
Hide file tree
Showing 42 changed files with 979 additions and 585 deletions.
17 changes: 10 additions & 7 deletions src/main/java/club/ttg/dnd5/controller/book/BookController.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ public ResponseEntity<SourceBookDTO> createBook(@RequestBody SourceBookDTO sourc
}

/**
* Создание новой книги.
* Получение всех книг.
*
*
* @return список книг
*/
@PostMapping("/search")
@Operation(summary = "Создать книгу", description = "Позволяет создать новую книгу.")
public ResponseEntity<List<SourceBookDTO>> createBook() {
return ResponseEntity.ok().body(bookService.getBooks());
@PostMapping("/book/search")
@Operation(summary = "Получить книги", description = "Возвращает список книги")
public ResponseEntity<List<SourceBookDTO>> getBooksByType() {
List<SourceBookDTO> books = bookService.getAllBooks();
return ResponseEntity.ok(books);
}

/**
Expand All @@ -49,15 +52,15 @@ public ResponseEntity<List<SourceBookDTO>> createBook() {
* @param typeName имя типа книги
* @return список книг соответствующего типа
*/
@GetMapping("/search/type")
@PostMapping("/book/search/type")
@Operation(summary = "Получить книги по типу", description = "Возвращает список книг определённого типа.")
public ResponseEntity<List<SourceBookDTO>> getBooksByType(
@Parameter(description = "Имя типа книги для поиска", example = "Базовые") @RequestParam String typeName) {
List<SourceBookDTO> books = bookService.getBooksByType(typeName);
return ResponseEntity.ok(books);
}

@GetMapping("/search/by-book-tag-type")
@PostMapping("/books/by-book-tag-type")
@Operation(summary = "Получить книги по типу тега", description = "Возвращает книги, связанные с тегами типа TAG_BOOK")
public ResponseEntity<List<SourceBookDTO>> getBooksByBookTagType() {
List<SourceBookDTO> books = bookService.getBooksByBookTagType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,30 @@
public class SpeciesController {
private final SpeciesService speciesService;

/**
* Проверка существования вида по URL.
*
* @param url URL вида.
* @return 204, если вида с таким URL не существует; 409, если вид существует.
*/
@Operation(
summary = "Проверка существования вида",
description = "Возвращает 204 (No Content), если вида с указанным URL не существует, или 409 (Conflict), если вид существует."
)
@ApiResponses(value = {
@ApiResponse(responseCode = "204", description = "Вид с указанным URL не найден."),
@ApiResponse(responseCode = "409", description = "Вид с указанным URL уже существует.")
})
@RequestMapping(value = "/{url}", method = RequestMethod.OPTIONS)
public ResponseEntity<Void> handleOptions(@PathVariable("url") String url) {
boolean exists = speciesService.speciesExistsByUrl(url);
if (exists) {
return ResponseEntity.status(HttpStatus.CONFLICT).build();
} else {
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}
}

@PostMapping("/search")
@Operation(summary = "Получение всех видов", description = "Виды будут не детальные, будет возвращать списков с указанным имени и урл")
public ResponseEntity<List<SpeciesDto>> getAllSpecies() {
Expand Down
21 changes: 19 additions & 2 deletions src/main/java/club/ttg/dnd5/dictionary/Size.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import club.ttg.dnd5.dictionary.beastiary.CreatureType;
import lombok.Getter;

import java.util.EnumSet;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

@Getter
public enum Size {
Expand Down Expand Up @@ -39,6 +39,23 @@ public static Size parse(String size) {
return UNDEFINED;
}

public static String convertSizeToEntityFormat(Collection<String> sizes) {
List<Size> list = sizes.stream()
.map(size -> parse(size))
.toList();
return list.stream()
.map(Size::getName)
.collect(Collectors.joining(", "));
}

public static List<String> convertEntityFormatToDtoFormat(String entityFormat) {
return Arrays.stream(entityFormat.split(","))
.map(String::trim)
.map(Size::parse)
.map(Size::getName)
.toList();
}

public static Set<Size> getFilterSizes(){
return EnumSet.of(TINY, SMALL, MEDIUM, LARGE, HUGE, GARGANTUAN);
}
Expand Down
12 changes: 2 additions & 10 deletions src/main/java/club/ttg/dnd5/dto/base/BaseDTO.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
package club.ttg.dnd5.dto.base;

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

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
public abstract class BaseDTO {
private String url;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonProperty(value = "image")
private String imageUrl;
public abstract class BaseDTO extends BaseUrl {
@JsonProperty(value = "name")
private NameBasedDTO nameBasedDTO = new NameBasedDTO();
private String description;
@JsonProperty(value = "source")
private SourceResponse sourceDTO = new SourceResponse();
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private List<String> gallery = new ArrayList<>();
private Instant updatedAt;
private String userId;
}


20 changes: 20 additions & 0 deletions src/main/java/club/ttg/dnd5/dto/base/BaseUrl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package club.ttg.dnd5.dto.base;

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

import java.util.ArrayList;
import java.util.List;

@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)
private List<String> gallery = new ArrayList<>();
}
4 changes: 3 additions & 1 deletion src/main/java/club/ttg/dnd5/dto/base/HasNameResponse.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package club.ttg.dnd5.dto.base;

import java.util.ArrayList;

public interface HasNameResponse {
String getName();
String getEnglish();
String getAlternative();
ArrayList<String> getAlternative();
}
4 changes: 3 additions & 1 deletion src/main/java/club/ttg/dnd5/dto/base/NameBasedDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.fasterxml.jackson.annotation.JsonRootName;
import lombok.*;

import java.util.ArrayList;

@Getter
@Setter
@RequiredArgsConstructor
Expand All @@ -19,7 +21,7 @@ public class NameBasedDTO implements HasNameResponse {
private String english = "";
@JsonInclude(value = JsonInclude.Include.NON_EMPTY)
@JsonProperty(value = "alt")
private String alternative = "";
private ArrayList<String> alternative = new ArrayList<>();
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty(value = "short")
private String shortName = "";
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/club/ttg/dnd5/dto/base/SourceResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class SourceResponse implements GroupStrategy {
private NameBasedDTO name = new NameBasedDTO();
private NameBasedDTO group = new NameBasedDTO();
private Short page;
private int page;
private boolean homebrew = false;
private boolean thirdParty = false;

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/club/ttg/dnd5/dto/base/create/CreateBaseDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package club.ttg.dnd5.dto.base.create;

import club.ttg.dnd5.dto.base.BaseUrl;
import club.ttg.dnd5.dto.base.NameBasedDTO;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public abstract class CreateBaseDTO extends BaseUrl {
@JsonProperty(value = "name")
private NameBasedDTO nameBasedDTO = new NameBasedDTO();
private String description;
@JsonProperty(value = "source")
private SourceReference sourceDTO = new SourceReference();
private String userId;
}
15 changes: 15 additions & 0 deletions src/main/java/club/ttg/dnd5/dto/base/create/SourceReference.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package club.ttg.dnd5.dto.base.create;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;

@Getter
@Setter
@Builder
@RequiredArgsConstructor
@AllArgsConstructor
public class SourceReference {
@JsonProperty(namespace = "url")
private String url;
private int page;
}
1 change: 1 addition & 0 deletions src/main/java/club/ttg/dnd5/dto/book/SourceBookDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
@AllArgsConstructor
@NoArgsConstructor
public class SourceBookDTO implements HasTagDTO {
private String url;
private NameBasedDTO name;
private String description;
private LocalDate year;
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/club/ttg/dnd5/dto/species/CreateSpeciesDto.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package club.ttg.dnd5.dto.species;

import club.ttg.dnd5.dto.base.BaseDTO;
import club.ttg.dnd5.dto.base.HasTagDTO;
import club.ttg.dnd5.dto.base.create.CreateBaseDTO;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand All @@ -16,9 +17,11 @@
@Setter
@AllArgsConstructor
@RequiredArgsConstructor
public class CreateSpeciesDto extends BaseDTO implements HasTagDTO {
public class CreateSpeciesDto extends CreateBaseDTO implements HasTagDTO {
private String linkImageUrl;
private String parent;
private CreaturePropertiesDto creatureProperties = new CreaturePropertiesDto();
private Collection<String> features = new ArrayList<>();
@JsonProperty(namespace = "properties")
private CreaturePropertiesDto properties = new CreaturePropertiesDto();
private Collection<SpeciesCreateFeatureDto> features = new ArrayList<>();
private Set<String> tags = new HashSet<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
import lombok.Getter;
import lombok.Setter;

import java.util.List;

@JsonRootName(value = "properties")
@JsonInclude(JsonInclude.Include.NON_NULL)
@Getter
@Setter
public class CreaturePropertiesDto {
@JsonProperty(value = "speed")
MovementAttributes movementAttributes;
private String size;
MovementAttributes movementAttributes = new MovementAttributes();
private List<String> sizes;
private String type;
private int darkVision;
}
15 changes: 11 additions & 4 deletions src/main/java/club/ttg/dnd5/dto/species/MovementAttributes.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package club.ttg.dnd5.dto.species;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.*;

@Getter
@Setter
@Builder
@RequiredArgsConstructor
@AllArgsConstructor
public class MovementAttributes{
private int base = 30;
private final int base;
private int fly;
private int climb;
private int swim;

public MovementAttributes() {
this.base = 30;
fly = -1;
climb = -1;
swim = -1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package club.ttg.dnd5.dto.species;

import club.ttg.dnd5.dto.base.NameBasedDTO;
import club.ttg.dnd5.dto.base.create.SourceReference;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class SpeciesCreateFeatureDto {
private NameBasedDTO name = new NameBasedDTO();
private String description;
private SourceReference source = new SourceReference();
}
5 changes: 5 additions & 0 deletions src/main/java/club/ttg/dnd5/dto/species/SpeciesDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import lombok.Setter;

import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Getter
Expand All @@ -25,17 +27,20 @@ public class SpeciesDto extends BaseDTO implements DetailableDTO, GroupStrategy
// Включаем свойства существа через DTO
@JsonProperty(value = "properties")
private CreaturePropertiesDto creatureProperties = new CreaturePropertiesDto();
private String linkImageUrl;
// Связанные сущности
private LinkedSpeciesDto parent = new LinkedSpeciesDto();
private Collection<LinkedSpeciesDto> subspecies = new LinkedHashSet<>();
private Collection<SpeciesFeatureDto> features;
private NameBasedDTO group = new NameBasedDTO();
@JsonIgnore
private boolean isDetail = false;
private Set<String> tags = new HashSet<>();

@Override
public void hideDetails() {
if (!isDetail) {
linkImageUrl = null;
this.creatureProperties = null;
this.parent = null;
this.subspecies = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
import java.util.HashSet;
import java.util.Set;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Getter
@Setter
public class SpeciesFeatureDto extends BaseDTO implements HasTagDTO {
@JsonProperty("tags")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private Set<String> tags = new HashSet<>();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package club.ttg.dnd5.model.base;

import club.ttg.dnd5.dictionary.Size;
import club.ttg.dnd5.dictionary.beastiary.CreatureType;
import jakarta.persistence.Column;
import jakarta.persistence.EnumType;
Expand All @@ -13,8 +12,7 @@
@Setter
@MappedSuperclass
public abstract class CreatureProperties extends NamedEntity {
@Enumerated(EnumType.STRING)
private Size size;
private String sizes;
@Enumerated(EnumType.STRING)
private CreatureType type;
@Column(columnDefinition = "int default 30")
Expand Down
Loading

0 comments on commit f14ceb7

Please sign in to comment.