lab-3 tasks were done but some mistakes

This commit is contained in:
Zakharov_Rostislav 2024-04-26 13:53:06 +04:00
parent 928639bd98
commit 8e13c1654e
11 changed files with 97 additions and 95 deletions

View File

@ -61,10 +61,10 @@ public class LibraryApplication implements CommandLineRunner {
final var book3 = bookService.create(new BookEntity("book3", type2)); final var book3 = bookService.create(new BookEntity("book3", type2));
final var book4 = bookService.create(new BookEntity("book4", type2)); final var book4 = bookService.create(new BookEntity("book4", type2));
authorService.addBook(author1.getId(), book1.getId()); bookService.addAuthor(author1.getId(), book1.getId());
authorService.addBook(author2.getId(), book2.getId()); bookService.addAuthor(author2.getId(), book2.getId());
authorService.addBook(author1.getId(), book3.getId()); bookService.addAuthor(author1.getId(), book3.getId());
authorService.addBook(author2.getId(), book3.getId()); bookService.addAuthor(author2.getId(), book3.getId());
log.info("Create default users values"); log.info("Create default users values");
final var user1 = userService.create(new UserEntity("user1", "123")); final var user1 = userService.create(new UserEntity("user1", "123"));

View File

@ -15,9 +15,6 @@ import org.springframework.web.bind.annotation.RestController;
import com.ip.library.core.configuration.Constants; import com.ip.library.core.configuration.Constants;
import com.ip.library.authors.model.AuthorEntity; import com.ip.library.authors.model.AuthorEntity;
import com.ip.library.authors.service.AuthorService; import com.ip.library.authors.service.AuthorService;
import com.ip.library.books.api.BookDto;
import com.ip.library.books.model.BookEntity;
import com.ip.library.books.service.BookService;
import jakarta.validation.Valid; import jakarta.validation.Valid;
@ -27,19 +24,10 @@ import jakarta.validation.Valid;
public class AuthorController { public class AuthorController {
private final AuthorService authorService; private final AuthorService authorService;
private final ModelMapper modelMapper; private final ModelMapper modelMapper;
private final BookService bookService;
public AuthorController(AuthorService authorService, ModelMapper modelMapper, BookService bookService) { public AuthorController(AuthorService authorService, ModelMapper modelMapper) {
this.authorService = authorService; this.authorService = authorService;
this.modelMapper = modelMapper; this.modelMapper = modelMapper;
this.bookService = bookService;
}
private BookDto toBookDto (BookEntity entity) {
BookDto bookDto = modelMapper.map(entity, BookDto.class);
bookDto.setAuthorId(bookService.getBookAuthors(
entity.getId()).stream().map(x -> x.getId()).toList());
return bookDto;
} }
private AuthorDto toDto(AuthorEntity entity) { private AuthorDto toDto(AuthorEntity entity) {
@ -74,10 +62,4 @@ public class AuthorController {
public AuthorDto delete(@PathVariable(name = "id") Long id) { public AuthorDto delete(@PathVariable(name = "id") Long id) {
return toDto(authorService.delete(id)); return toDto(authorService.delete(id));
} }
@GetMapping("/{id}/books")
public List<BookDto> getAuthorBooks(@PathVariable(name = "id") Long id) {
return authorService.getAuthorBooks(id).stream().map(this::toBookDto).toList();
}
} }

View File

@ -1,24 +1,14 @@
package com.ip.library.authors.repository; package com.ip.library.authors.repository;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.PagingAndSortingRepository;
import com.ip.library.authors.model.AuthorEntity; import com.ip.library.authors.model.AuthorEntity;
import com.ip.library.books.model.BookEntity;
public interface AuthorRepository extends public interface AuthorRepository extends
CrudRepository<AuthorEntity, Long>, CrudRepository<AuthorEntity, Long>,
PagingAndSortingRepository<AuthorEntity, Long> { PagingAndSortingRepository<AuthorEntity, Long> {
Optional<AuthorEntity> findByNameIgnoreCase(String name); Optional<AuthorEntity> findByNameIgnoreCase(String name);
@Query(
"select ab.book " +
"from AuthorsBooksEntity ab " +
"where ab.author.id = ?1 " +
"order by ab.book.id"
)
List<BookEntity> getAuthorBooks(Long authorId);
} }

View File

@ -9,17 +9,13 @@ import org.springframework.transaction.annotation.Transactional;
import com.ip.library.core.error.NotFoundException; import com.ip.library.core.error.NotFoundException;
import com.ip.library.authors.model.AuthorEntity; import com.ip.library.authors.model.AuthorEntity;
import com.ip.library.authors.repository.AuthorRepository; import com.ip.library.authors.repository.AuthorRepository;
import com.ip.library.books.model.BookEntity;
import com.ip.library.books.service.BookService;
@Service @Service
public class AuthorService { public class AuthorService {
private final AuthorRepository repository; private final AuthorRepository repository;
private final BookService bookService;
public AuthorService(AuthorRepository repository, BookService bookService) { public AuthorService(AuthorRepository repository) {
this.repository = repository; this.repository = repository;
this.bookService = bookService;
} }
private void checkNameUniqueness(String name) { private void checkNameUniqueness(String name) {
@ -67,16 +63,4 @@ public class AuthorService {
repository.delete(existsEntity); repository.delete(existsEntity);
return existsEntity; return existsEntity;
} }
@Transactional
public List<BookEntity> getAuthorBooks(long authorId) {
return repository.getAuthorBooks(authorId);
}
@Transactional
public boolean addBook(long authorId, long bookId) {
final AuthorEntity existsAuthor = get(authorId);
final BookEntity book = bookService.get(bookId);
return existsAuthor.addBook(book);
}
} }

View File

@ -17,7 +17,6 @@ import com.ip.library.books.model.BookEntity;
import com.ip.library.books.service.BookService; import com.ip.library.books.service.BookService;
import com.ip.library.core.configuration.Constants; import com.ip.library.core.configuration.Constants;
import com.ip.library.types.service.TypeService; import com.ip.library.types.service.TypeService;
import com.ip.library.authors.service.AuthorService;
import jakarta.validation.Valid; import jakarta.validation.Valid;
@ -26,21 +25,17 @@ import jakarta.validation.Valid;
public class BookController { public class BookController {
private final BookService bookService; private final BookService bookService;
private final TypeService typeService; private final TypeService typeService;
private final AuthorService authorService;
private final ModelMapper modelMapper; private final ModelMapper modelMapper;
public BookController(BookService bookService, TypeService typeService, public BookController(BookService bookService, TypeService typeService, ModelMapper modelMapper) {
AuthorService authorService, ModelMapper modelMapper) {
this.bookService = bookService; this.bookService = bookService;
this.typeService = typeService; this.typeService = typeService;
this.authorService = authorService;
this.modelMapper = modelMapper; this.modelMapper = modelMapper;
} }
private BookDto toBookDto (BookEntity entity) { private BookDto toBookDto (BookEntity entity) {
BookDto bookDto = modelMapper.map(entity, BookDto.class); BookDto bookDto = modelMapper.map(entity, BookDto.class);
bookDto.setAuthorId(bookService.getBookAuthors( bookDto.setAuthorId(entity.getAuthorsBooks().stream().map(x -> x.getAuthor().getId()).toList());
entity.getId()).stream().map(x -> x.getId()).toList());
return bookDto; return bookDto;
} }
@ -53,9 +48,10 @@ public class BookController {
@GetMapping @GetMapping
public List<BookDto> getAll( public List<BookDto> getAll(
@RequestParam(name = "typeId", defaultValue = "-1") Long typeId, @RequestParam(name = "typeId", defaultValue = "-1") Long typeId,
@RequestParam(name = "authorId", defaultValue = "-1") Long authorId,
@RequestParam(name = "page", defaultValue = "0") int page, @RequestParam(name = "page", defaultValue = "0") int page,
@RequestParam(name = "size", defaultValue = Constants.DEFAULT_PAGE_SIZE) int size) { @RequestParam(name = "size", defaultValue = Constants.DEFAULT_PAGE_SIZE) int size) {
return bookService.getAll(typeId).stream().map(this::toBookDto).toList(); return bookService.getAll(typeId, authorId, page, size).stream().map(this::toBookDto).toList();
} }
@GetMapping("/{id}") @GetMapping("/{id}")
@ -82,4 +78,11 @@ public class BookController {
public int getBookSubscribersNumber(@PathVariable(name = "bookId") Long bookId) { public int getBookSubscribersNumber(@PathVariable(name = "bookId") Long bookId) {
return bookService.getBookSubscribersNumber(bookId); return bookService.getBookSubscribersNumber(bookId);
} }
@GetMapping("/{bookId}/author/{authorId}")
public boolean addAuthor(
@PathVariable(name = "bookId") Long bookId,
@PathVariable(name = "authorId") Long authorId) {
return bookService.addAuthor(authorId, bookId);
}
} }

View File

@ -32,7 +32,8 @@ public class BookEntity extends BaseEntity {
@JoinColumn(name = "type_id", nullable = false) @JoinColumn(name = "type_id", nullable = false)
@OrderBy("id ASC") @OrderBy("id ASC")
private TypeEntity type; private TypeEntity type;
@OneToMany(mappedBy = "book", cascade = CascadeType.ALL, orphanRemoval = true) @OneToMany(mappedBy = "book", cascade = CascadeType.ALL,
orphanRemoval = true, fetch = FetchType.EAGER)
@OrderBy("id ASC") @OrderBy("id ASC")
private Set<AuthorsBooksEntity> authorsBooks = new HashSet<>(); private Set<AuthorsBooksEntity> authorsBooks = new HashSet<>();

View File

@ -8,7 +8,6 @@ import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.PagingAndSortingRepository;
import com.ip.library.authors.model.AuthorEntity;
import com.ip.library.books.model.BookEntity; import com.ip.library.books.model.BookEntity;
public interface BookRepository extends public interface BookRepository extends
@ -20,16 +19,43 @@ public interface BookRepository extends
List<BookEntity> findByTypeId(long typeId, Pageable pageable); List<BookEntity> findByTypeId(long typeId, Pageable pageable);
@Query(
"select ab.book " +
"from AuthorsBooksEntity ab " +
"where ab.author.id = ?1 " +
"order by ab.book.id"
)
List<BookEntity> findByAuthorId(Long authorId);
@Query(
"select ab.book " +
"from AuthorsBooksEntity ab " +
"where ab.author.id = ?1 " +
"order by ab.book.id"
)
List<BookEntity> findByAuthorId(Long authorId, Pageable pageable);
@Query(
"select ab.book " +
"from AuthorsBooksEntity ab " +
"where ab.author.id = ?1 and " +
"ab.book.type.id = ?2 " +
"order by ab.book.id"
)
List<BookEntity> findByAuthorIdAndTypeId(Long authorId, Long typeId);
@Query(
"select ab.book " +
"from AuthorsBooksEntity ab " +
"where ab.author.id = ?1 and " +
"ab.book.type.id = ?2 " +
"order by ab.book.id"
)
List<BookEntity> findByAuthorIdAndTypeId(Long authorId, Long typeId, Pageable pageable);
@Query( @Query(
"select count(*) as number " + "select count(*) as number " +
"from FavoriteEntity f " + "from FavoriteEntity f " +
"where f.book.id = ?1") "where f.book.id = ?1")
int getBookSubscribersNumber(long bookId); int getBookSubscribersNumber(long bookId);
@Query(
"select ab.author " +
"from AuthorsBooksEntity ab " +
"where ab.book.id = ?1 " +
"order by ab.author.id"
)
List<AuthorEntity> getBookAuthors(Long bookId);
} }

View File

@ -8,6 +8,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import com.ip.library.authors.model.AuthorEntity; import com.ip.library.authors.model.AuthorEntity;
import com.ip.library.authors.service.AuthorService;
import com.ip.library.books.model.BookEntity; import com.ip.library.books.model.BookEntity;
import com.ip.library.books.repository.BookRepository; import com.ip.library.books.repository.BookRepository;
import com.ip.library.core.error.NotFoundException; import com.ip.library.core.error.NotFoundException;
@ -15,9 +16,11 @@ import com.ip.library.core.error.NotFoundException;
@Service @Service
public class BookService { public class BookService {
private final BookRepository repository; private final BookRepository repository;
private final AuthorService authorService;
public BookService(BookRepository repository) { public BookService(BookRepository repository, AuthorService authorService) {
this.repository = repository; this.repository = repository;
this.authorService = authorService;
} }
private void checkNameUniqueness(String name){ private void checkNameUniqueness(String name){
@ -34,22 +37,16 @@ public class BookService {
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public List<BookEntity> getAll(long typeId) { public List<BookEntity> getAll(long typeId, long authorId, int page, int size) {
if (typeId <= 0L) {
return getAll();
}
return repository.findByTypeId(typeId);
}
@Transactional(readOnly = true)
public List<BookEntity> getAll(long typeId, int page, int size) {
PageRequest pageRequest = PageRequest.of(page, size); PageRequest pageRequest = PageRequest.of(page, size);
if (typeId <= 0L) { if (typeId <= 0L && authorId <= 0L)
return StreamSupport.stream( return StreamSupport.stream(repository.findAll(pageRequest).spliterator(),
repository.findAll(pageRequest).spliterator(), false false).toList();
).toList(); if (authorId <= 0L)
}
return repository.findByTypeId(typeId, pageRequest); return repository.findByTypeId(typeId, pageRequest);
if (typeId <= 0L)
return repository.findByAuthorId(authorId, pageRequest);
return repository.findByAuthorIdAndTypeId(authorId, typeId, pageRequest);
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
@ -91,8 +88,15 @@ public class BookService {
return repository.getBookSubscribersNumber(bookId); return repository.getBookSubscribersNumber(bookId);
} }
@Transactional(readOnly = true) @Transactional
public List<AuthorEntity> getBookAuthors(long bookId) { public boolean addAuthor(long authorId, long bookId) {
return repository.getBookAuthors(bookId); final AuthorEntity existsAuthor = authorService.get(authorId);
final BookEntity book = get(bookId);
return existsAuthor.addBook(book);
}
@Transactional
public List<BookEntity> findByAuthorId(long authorId) {
return repository.findByAuthorId(authorId);
} }
} }

View File

@ -15,7 +15,6 @@ import org.springframework.web.bind.annotation.RestController;
import com.ip.library.books.api.BookDto; import com.ip.library.books.api.BookDto;
import com.ip.library.books.model.BookEntity; import com.ip.library.books.model.BookEntity;
import com.ip.library.books.service.BookService;
import com.ip.library.core.api.PageDto; import com.ip.library.core.api.PageDto;
import com.ip.library.core.api.PageDtoMapper; import com.ip.library.core.api.PageDtoMapper;
import com.ip.library.core.configuration.Constants; import com.ip.library.core.configuration.Constants;
@ -29,18 +28,15 @@ import jakarta.validation.Valid;
public class UserController { public class UserController {
private final UserService userService; private final UserService userService;
private final ModelMapper modelMapper; private final ModelMapper modelMapper;
private final BookService bookService;
public UserController(UserService userService, ModelMapper modelMapper, BookService bookService) { public UserController(UserService userService, ModelMapper modelMapper) {
this.userService = userService; this.userService = userService;
this.modelMapper = modelMapper; this.modelMapper = modelMapper;
this.bookService = bookService;
} }
private BookDto toBookDto (BookEntity entity) { private BookDto toBookDto (BookEntity entity) {
BookDto bookDto = modelMapper.map(entity, BookDto.class); BookDto bookDto = modelMapper.map(entity, BookDto.class);
bookDto.setAuthorId(bookService.getBookAuthors( bookDto.setAuthorId(entity.getAuthorsBooks().stream().map(x -> x.getAuthor().getId()).toList());
entity.getId()).stream().map(x -> x.getId()).toList());
return bookDto; return bookDto;
} }
@ -99,7 +95,10 @@ public class UserController {
} }
@GetMapping("/{userId}/books") @GetMapping("/{userId}/books")
public List<BookDto> getUserFavorites(@PathVariable(name = "userId") Long userId) { public List<BookDto> getUserFavorites(
return userService.getUserFavorities(userId).stream().map(this::toBookDto).toList(); @PathVariable(name = "userId") Long userId,
@RequestParam(name = "page", defaultValue = "0") int page,
@RequestParam(name = "size", defaultValue = Constants.DEFAULT_PAGE_SIZE) int size) {
return userService.getUserFavorities(userId, page, size).stream().map(this::toBookDto).toList();
} }
} }

View File

@ -3,6 +3,7 @@ package com.ip.library.users.repository;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.PagingAndSortingRepository;
@ -21,4 +22,11 @@ public interface UserRepository extends
"where f.user.id = ?1 " + "where f.user.id = ?1 " +
"order by f.book.id") "order by f.book.id")
public List<BookEntity> getUserFavorities(Long userId); public List<BookEntity> getUserFavorities(Long userId);
@Query(
"select f.book " +
"from FavoriteEntity f " +
"where f.user.id = ?1 " +
"order by f.book.id")
public List<BookEntity> getUserFavorities(Long userId, Pageable pageable);
} }

View File

@ -108,4 +108,9 @@ public class UserService {
public List<BookEntity> getUserFavorities (long userId) { public List<BookEntity> getUserFavorities (long userId) {
return repository.getUserFavorities(userId); return repository.getUserFavorities(userId);
} }
@Transactional(readOnly = true)
public List<BookEntity> getUserFavorities (long userId, int page, int size) {
return repository.getUserFavorities(userId, PageRequest.of(page, size));
}
} }