Skip to content

Commit

Permalink
- small updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ProtectorRTD committed Dec 18, 2024
1 parent dc389d2 commit b096183
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ public ResponseEntity<List<SourceBookDTO>> getBooksByType(
return ResponseEntity.ok(books);
}

@GetMapping("/books/by-book-tag-type")
@Operation(summary = "Получить книги по типу тега", description = "Возвращает книги, связанные с тегами типа TAG_BOOK")
public ResponseEntity<List<SourceBookDTO>> getBooksByBookTagType() {
List<SourceBookDTO> books = bookService.getBooksByBookTagType();
return ResponseEntity.ok(books);
}

/**
* Получение книги по акрониму источника.
*
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/club/ttg/dnd5/repository/TagRepository.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package club.ttg.dnd5.repository;

import club.ttg.dnd5.model.base.Tag;
import club.ttg.dnd5.model.base.TagType;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

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

@Repository
public interface TagRepository extends JpaRepository<Tag, Long> {
Optional<Tag> findByNameIgnoreCase(String name);
List<Tag> findByTagType(TagType tagType);
}
14 changes: 12 additions & 2 deletions src/main/java/club/ttg/dnd5/service/book/BookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import club.ttg.dnd5.model.book.TypeBook;
import club.ttg.dnd5.repository.TagRepository;
import club.ttg.dnd5.repository.book.BookRepository;
import club.ttg.dnd5.service.TagService;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -27,7 +26,6 @@
public class BookService {
private final BookRepository bookRepository;
private final TagRepository tagRepository;
private final TagService tagService;

// Создание новой книги
@Transactional
Expand Down Expand Up @@ -68,6 +66,18 @@ public List<SourceBookDTO> getBooksByTag(String tagName) {
.toList();
}

public List<SourceBookDTO> getBooksByBookTagType() {
List<Tag> tags = tagRepository.findByTagType(TagType.TAG_BOOK);

Set<Book> books = tags.stream()
.flatMap(tag -> tag.getBooks().stream())
.collect(Collectors.toSet());

return books.stream()
.map(this::convertingEntityToSourceDTO)
.collect(Collectors.toList());
}

private Book convertingCreateSourceToEntity(SourceBookDTO sourceBookDTO) {
NameBasedDTO name = sourceBookDTO.getName();
if (StringUtils.isBlank(name.getShortName())) {
Expand Down

0 comments on commit b096183

Please sign in to comment.