lab-3 #2

Merged
Zakharov_Rostislav merged 22 commits from lab-3 into main 2024-05-23 12:01:06 +04:00
11 changed files with 97 additions and 95 deletions
Showing only changes of commit 8e13c1654e - Show all commits

View File

@ -61,10 +61,10 @@ public class LibraryApplication implements CommandLineRunner {
final var book3 = bookService.create(new BookEntity("book3", type2));
final var book4 = bookService.create(new BookEntity("book4", type2));
authorService.addBook(author1.getId(), book1.getId());
authorService.addBook(author2.getId(), book2.getId());
authorService.addBook(author1.getId(), book3.getId());
authorService.addBook(author2.getId(), book3.getId());
bookService.addAuthor(author1.getId(), book1.getId());
bookService.addAuthor(author2.getId(), book2.getId());
bookService.addAuthor(author1.getId(), book3.getId());
bookService.addAuthor(author2.getId(), book3.getId());
log.info("Create default users values");
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.authors.model.AuthorEntity;
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;
@ -27,20 +24,11 @@ import jakarta.validation.Valid;
public class AuthorController {
private final AuthorService authorService;
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.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) {
return modelMapper.map(entity, AuthorDto.class);
@ -73,11 +61,5 @@ public class AuthorController {
@DeleteMapping("/{id}")
public AuthorDto delete(@PathVariable(name = "id") Long 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;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import com.ip.library.authors.model.AuthorEntity;
import com.ip.library.books.model.BookEntity;
public interface AuthorRepository extends
CrudRepository<AuthorEntity, Long>,
PagingAndSortingRepository<AuthorEntity, Long> {
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.authors.model.AuthorEntity;
import com.ip.library.authors.repository.AuthorRepository;
import com.ip.library.books.model.BookEntity;
import com.ip.library.books.service.BookService;
@Service
public class AuthorService {
private final AuthorRepository repository;
private final BookService bookService;
public AuthorService(AuthorRepository repository, BookService bookService) {
public AuthorService(AuthorRepository repository) {
this.repository = repository;
this.bookService = bookService;
}
private void checkNameUniqueness(String name) {
@ -67,16 +63,4 @@ public class AuthorService {
repository.delete(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.core.configuration.Constants;
import com.ip.library.types.service.TypeService;
import com.ip.library.authors.service.AuthorService;
import jakarta.validation.Valid;
@ -26,21 +25,17 @@ import jakarta.validation.Valid;
public class BookController {
private final BookService bookService;
private final TypeService typeService;
private final AuthorService authorService;
private final ModelMapper modelMapper;
public BookController(BookService bookService, TypeService typeService,
AuthorService authorService, ModelMapper modelMapper) {
public BookController(BookService bookService, TypeService typeService, ModelMapper modelMapper) {
this.bookService = bookService;
this.typeService = typeService;
this.authorService = authorService;
this.modelMapper = modelMapper;
}
private BookDto toBookDto (BookEntity entity) {
BookDto bookDto = modelMapper.map(entity, BookDto.class);
bookDto.setAuthorId(bookService.getBookAuthors(
entity.getId()).stream().map(x -> x.getId()).toList());
bookDto.setAuthorId(entity.getAuthorsBooks().stream().map(x -> x.getAuthor().getId()).toList());
return bookDto;
}
@ -53,9 +48,10 @@ public class BookController {
@GetMapping
public List<BookDto> getAll(
@RequestParam(name = "typeId", defaultValue = "-1") Long typeId,
@RequestParam(name = "authorId", defaultValue = "-1") Long authorId,
@RequestParam(name = "page", defaultValue = "0") int page,
@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}")
@ -81,5 +77,12 @@ public class BookController {
@GetMapping("/{bookId}/users/number")
public int getBookSubscribersNumber(@PathVariable(name = "bookId") Long 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)
@OrderBy("id ASC")
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")
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.PagingAndSortingRepository;
import com.ip.library.authors.model.AuthorEntity;
import com.ip.library.books.model.BookEntity;
public interface BookRepository extends
@ -19,17 +18,44 @@ public interface BookRepository extends
List<BookEntity> findByTypeId(long typeId);
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(
"select count(*) as number " +
"from FavoriteEntity f " +
"where f.book.id = ?1")
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 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.repository.BookRepository;
import com.ip.library.core.error.NotFoundException;
@ -15,9 +16,11 @@ import com.ip.library.core.error.NotFoundException;
@Service
public class BookService {
private final BookRepository repository;
private final AuthorService authorService;
public BookService(BookRepository repository) {
public BookService(BookRepository repository, AuthorService authorService) {
this.repository = repository;
this.authorService = authorService;
}
private void checkNameUniqueness(String name){
@ -34,22 +37,16 @@ public class BookService {
}
@Transactional(readOnly = true)
public List<BookEntity> getAll(long typeId) {
if (typeId <= 0L) {
return getAll();
}
return repository.findByTypeId(typeId);
}
@Transactional(readOnly = true)
public List<BookEntity> getAll(long typeId, int page, int size) {
public List<BookEntity> getAll(long typeId, long authorId, int page, int size) {
PageRequest pageRequest = PageRequest.of(page, size);
if (typeId <= 0L) {
return StreamSupport.stream(
repository.findAll(pageRequest).spliterator(), false
).toList();
}
return repository.findByTypeId(typeId, pageRequest);
if (typeId <= 0L && authorId <= 0L)
return StreamSupport.stream(repository.findAll(pageRequest).spliterator(),
false).toList();
if (authorId <= 0L)
return repository.findByTypeId(typeId, pageRequest);
if (typeId <= 0L)
return repository.findByAuthorId(authorId, pageRequest);
return repository.findByAuthorIdAndTypeId(authorId, typeId, pageRequest);
}
@Transactional(readOnly = true)
@ -91,8 +88,15 @@ public class BookService {
return repository.getBookSubscribersNumber(bookId);
}
@Transactional(readOnly = true)
public List<AuthorEntity> getBookAuthors(long bookId) {
return repository.getBookAuthors(bookId);
@Transactional
public boolean addAuthor(long authorId, long 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.model.BookEntity;
import com.ip.library.books.service.BookService;
import com.ip.library.core.api.PageDto;
import com.ip.library.core.api.PageDtoMapper;
import com.ip.library.core.configuration.Constants;
@ -29,18 +28,15 @@ import jakarta.validation.Valid;
public class UserController {
private final UserService userService;
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.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());
bookDto.setAuthorId(entity.getAuthorsBooks().stream().map(x -> x.getAuthor().getId()).toList());
return bookDto;
}
@ -99,7 +95,10 @@ public class UserController {
}
@GetMapping("/{userId}/books")
public List<BookDto> getUserFavorites(@PathVariable(name = "userId") Long userId) {
return userService.getUserFavorities(userId).stream().map(this::toBookDto).toList();
public List<BookDto> getUserFavorites(
@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.Optional;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
@ -21,4 +22,11 @@ public interface UserRepository extends
"where f.user.id = ?1 " +
"order by f.book.id")
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) {
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));
}
}