commit
9ae508347f
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
SpringApp/data.mv.db
|
||||
SpringApp/library/data.mv.db
|
||||
SpringApp/library/data.trace.db
|
2
SpringApp/.vscode/launch.json
vendored
2
SpringApp/.vscode/launch.json
vendored
@ -7,7 +7,7 @@
|
||||
"cwd": "${workspaceFolder}",
|
||||
"mainClass": "com.ip.library.LibraryApplication",
|
||||
"projectName": "library",
|
||||
"args": "",
|
||||
"args": "--populate",
|
||||
"envFile": "${workspaceFolder}/.env"
|
||||
}
|
||||
]
|
||||
|
@ -1,12 +1,23 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
id 'org.springframework.boot' version '3.2.2'
|
||||
id 'org.springframework.boot' version '3.2.4'
|
||||
id 'io.spring.dependency-management' version '1.1.4'
|
||||
}
|
||||
|
||||
group = 'com.ip'
|
||||
version = '0.0.1-SNAPSHOT'
|
||||
|
||||
defaultTasks 'bootRun'
|
||||
|
||||
jar {
|
||||
enabled = false
|
||||
}
|
||||
|
||||
bootJar {
|
||||
archiveFileName = String.format('%s-%s.jar', rootProject.name, version)
|
||||
}
|
||||
|
||||
assert System.properties['java.specification.version'] == '17' || '21'
|
||||
java {
|
||||
sourceCompatibility = '17'
|
||||
}
|
||||
@ -17,10 +28,12 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-validation'
|
||||
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0'
|
||||
implementation 'org.modelmapper:modelmapper:3.2.0'
|
||||
|
||||
developmentOnly 'org.springframework.boot:spring-boot-devtools'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||
implementation 'com.h2database:h2:2.2.224'
|
||||
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
}
|
||||
|
@ -1,5 +1,10 @@
|
||||
package com.ip.library;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.modelmapper.internal.util.Objects;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -43,24 +48,37 @@ public class LibraryApplication implements CommandLineRunner {
|
||||
public void run(String... args) throws Exception {
|
||||
if (args.length > 0 && args[0].equals("--populate")) {
|
||||
log.info("Create default types values");
|
||||
final var type1 = typeService.create(new TypeEntity(null, "type1"));
|
||||
final var type2 = typeService.create(new TypeEntity(null, "type2"));
|
||||
final var type1 = typeService.create(new TypeEntity("type1"));
|
||||
final var type2 = typeService.create(new TypeEntity("type2"));
|
||||
|
||||
log.info("Create default authors values");
|
||||
final var author1 = authorService.create(new AuthorEntity(null, "author1"));
|
||||
final var author2 = authorService.create(new AuthorEntity(null, "author2"));
|
||||
final var author1 = authorService.create(new AuthorEntity("author1"));
|
||||
final var author2 = authorService.create(new AuthorEntity("author2"));
|
||||
|
||||
log.info("Create default books values");
|
||||
final var book1 = bookService.create(new BookEntity(null, "book1", type1, author1));
|
||||
final var book2 = bookService.create(new BookEntity(null, "book2", type1, author2));
|
||||
final var book3 = bookService.create(new BookEntity(null, "book3", type2, author1));
|
||||
final var book4 = bookService.create(new BookEntity(null, "book4", type2, author2));
|
||||
final var book1 = bookService.create(new BookEntity("book1", type1));
|
||||
final var book2 = bookService.create(new BookEntity("book2", type1));
|
||||
final var book3 = bookService.create(new BookEntity("book3", type2));
|
||||
final var book4 = bookService.create(new BookEntity("book4", type2));
|
||||
|
||||
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(null, "user1", "123", null, null));
|
||||
final var user2 = userService.create(new UserEntity(null, "user2", "123", null, null ));
|
||||
final var admin1 = userService.create(new UserEntity(null, "admin1", "123", null, null));
|
||||
final var user1 = userService.create(new UserEntity("user1", "123"));
|
||||
final var user2 = userService.create(new UserEntity("user2", "123"));
|
||||
final var admin1 = userService.create(new UserEntity("admin1", "123"));
|
||||
|
||||
userService.giveAdminRole(admin1.getId());
|
||||
|
||||
userService.addFavorite(user1.getId(), book1.getId());
|
||||
userService.addFavorite(user1.getId(), book2.getId());
|
||||
userService.addFavorite(user1.getId(), book3.getId());
|
||||
userService.addFavorite(user1.getId(), book4.getId());
|
||||
userService.addFavorite(user2.getId(), book1.getId());
|
||||
userService.addFavorite(user2.getId(), book2.getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ import com.ip.library.authors.service.AuthorService;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
|
||||
@RestController
|
||||
@RequestMapping(Constants.API_URL + "/author")
|
||||
public class AuthorController {
|
||||
|
@ -3,13 +3,15 @@ package com.ip.library.authors.api;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.Size;
|
||||
|
||||
public class AuthorDto {
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
private Long id;
|
||||
@NotBlank
|
||||
@Size(min = 5, max = 20)
|
||||
private String name;
|
||||
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
@ -1,18 +1,34 @@
|
||||
package com.ip.library.authors.model;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
import com.ip.library.authors_books.model.AuthorsBooksEntity;
|
||||
import com.ip.library.books.model.BookEntity;
|
||||
import com.ip.library.core.model.BaseEntity;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.OrderBy;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "authors")
|
||||
public class AuthorEntity extends BaseEntity {
|
||||
@Column(nullable = false, unique = true, length = 20)
|
||||
private String name;
|
||||
@OneToMany(mappedBy = "author", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
@OrderBy("id ASC")
|
||||
private Set<AuthorsBooksEntity> authorsBooks = new HashSet<>();
|
||||
|
||||
public AuthorEntity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public AuthorEntity(Long id, String name) {
|
||||
super(id);
|
||||
public AuthorEntity(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@ -24,6 +40,22 @@ public class AuthorEntity extends BaseEntity {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Set<AuthorsBooksEntity> getAuthorsBooks() {
|
||||
return authorsBooks;
|
||||
}
|
||||
|
||||
public void setBooks(Set<AuthorsBooksEntity> authorsBooks) {
|
||||
this.authorsBooks = authorsBooks;
|
||||
}
|
||||
|
||||
public boolean addBook(BookEntity book) {
|
||||
AuthorsBooksEntity entity = new AuthorsBooksEntity(this, book);
|
||||
boolean result = authorsBooks.add(entity);
|
||||
if (!book.getAuthorsBooks().contains(entity))
|
||||
book.getAuthorsBooks().add(entity);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, name);
|
||||
@ -36,8 +68,9 @@ public class AuthorEntity extends BaseEntity {
|
||||
if (obj == null || getClass() != obj.getClass())
|
||||
return false;
|
||||
final AuthorEntity other = (AuthorEntity) obj;
|
||||
return Objects.equals(other.getId(), id)
|
||||
&& Objects.equals(other.getName(), name);
|
||||
return
|
||||
Objects.equals(other.getId(), id)
|
||||
&& Objects.equals(other.getName(), name);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,10 +1,14 @@
|
||||
package com.ip.library.authors.repository;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
|
||||
import com.ip.library.authors.model.AuthorEntity;
|
||||
import com.ip.library.core.repository.MapRepository;
|
||||
|
||||
@Repository
|
||||
public class AuthorRepository extends MapRepository<AuthorEntity> {
|
||||
public interface AuthorRepository extends
|
||||
CrudRepository<AuthorEntity, Long>,
|
||||
PagingAndSortingRepository<AuthorEntity, Long> {
|
||||
Optional<AuthorEntity> findByNameIgnoreCase(String name);
|
||||
}
|
||||
|
@ -1,9 +1,10 @@
|
||||
package com.ip.library.authors.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.ip.library.core.error.NotFoundException;
|
||||
import com.ip.library.authors.model.AuthorEntity;
|
||||
@ -17,27 +18,49 @@ public class AuthorService {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
private void checkNameUniqueness(String name) {
|
||||
if (repository.findByNameIgnoreCase(name).isPresent()) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("Author with name %s already exists", name)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<AuthorEntity> getAll() {
|
||||
return repository.getAll();
|
||||
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
|
||||
}
|
||||
|
||||
public AuthorEntity get(Long id) {
|
||||
return Optional.ofNullable(repository.get(id))
|
||||
.orElseThrow(() -> new NotFoundException(id));
|
||||
@Transactional(readOnly = true)
|
||||
public AuthorEntity get(long id) {
|
||||
return repository.findById(id)
|
||||
.orElseThrow(() -> new NotFoundException(AuthorEntity.class, id));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public AuthorEntity create(AuthorEntity entity) {
|
||||
return repository.create(entity);
|
||||
if (entity == null) {
|
||||
throw new IllegalArgumentException("Creating AuthorEntity is null");
|
||||
}
|
||||
checkNameUniqueness(entity.getName());
|
||||
return repository.save(entity);
|
||||
}
|
||||
|
||||
public AuthorEntity update(Long id, AuthorEntity entity) {
|
||||
@Transactional
|
||||
public AuthorEntity update(long id, AuthorEntity entity) {
|
||||
if (entity == null) {
|
||||
throw new IllegalArgumentException("Updating AuthorEntity is null");
|
||||
}
|
||||
final AuthorEntity existsEntity = get(id);
|
||||
checkNameUniqueness(entity.getName());
|
||||
existsEntity.setName(entity.getName());
|
||||
return repository.update(existsEntity);
|
||||
return repository.save(existsEntity);
|
||||
}
|
||||
|
||||
public AuthorEntity delete(Long id) {
|
||||
@Transactional
|
||||
public AuthorEntity delete(long id) {
|
||||
final AuthorEntity existsEntity = get(id);
|
||||
return repository.delete(existsEntity);
|
||||
repository.delete(existsEntity);
|
||||
return existsEntity;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,81 @@
|
||||
package com.ip.library.authors_books.model;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import com.ip.library.authors.model.AuthorEntity;
|
||||
import com.ip.library.books.model.BookEntity;
|
||||
|
||||
import jakarta.persistence.EmbeddedId;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.MapsId;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "authors_books")
|
||||
public class AuthorsBooksEntity {
|
||||
@EmbeddedId
|
||||
private AuthorsBooksId id = new AuthorsBooksId();
|
||||
@ManyToOne
|
||||
@MapsId("authorId")
|
||||
@JoinColumn(name = "author_id")
|
||||
private AuthorEntity author;
|
||||
@ManyToOne
|
||||
@MapsId("bookId")
|
||||
@JoinColumn(name = "book_id")
|
||||
private BookEntity book;
|
||||
|
||||
public AuthorsBooksEntity() {}
|
||||
|
||||
public AuthorsBooksEntity(AuthorEntity author, BookEntity book) {
|
||||
this.author = author;
|
||||
this.book = book;
|
||||
}
|
||||
|
||||
public void setId(AuthorsBooksId id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public AuthorsBooksId getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setAuthor(AuthorEntity author) {
|
||||
this.author = author;
|
||||
if (!author.getAuthorsBooks().contains(this))
|
||||
author.getAuthorsBooks().add(this);
|
||||
}
|
||||
|
||||
public AuthorEntity getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setBook(BookEntity book) {
|
||||
this.book = book;
|
||||
if (!book.getAuthorsBooks().contains(this))
|
||||
book.getAuthorsBooks().add(this);
|
||||
}
|
||||
|
||||
public BookEntity getBook() {
|
||||
return book;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, author.getId(), book.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null || getClass() != obj.getClass())
|
||||
return false;
|
||||
final AuthorsBooksEntity other = (AuthorsBooksEntity) obj;
|
||||
return
|
||||
Objects.equals(other.getId(), id) &&
|
||||
Objects.equals(other.getAuthor().getId(), author.getId()) &&
|
||||
Objects.equals(other.getBook().getId(), book.getId());
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
package com.ip.library.authors_books.model;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
import jakarta.persistence.Embeddable;
|
||||
|
||||
@Embeddable
|
||||
public class AuthorsBooksId {
|
||||
private Long authorId;
|
||||
private Long bookId;
|
||||
|
||||
public AuthorsBooksId() {}
|
||||
|
||||
public AuthorsBooksId(Long authorId, Long bookId) {
|
||||
this.authorId = authorId;
|
||||
this.bookId = bookId;
|
||||
}
|
||||
|
||||
public Long getAuthorId() {
|
||||
return authorId;
|
||||
}
|
||||
|
||||
public void setAuthorId(Long authorId) {
|
||||
this.authorId = authorId;
|
||||
}
|
||||
|
||||
public Long getBookId() {
|
||||
return bookId;
|
||||
}
|
||||
|
||||
public void setBookId(Long bookId) {
|
||||
this.bookId = bookId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(
|
||||
Optional.ofNullable(authorId).orElse(-1L),
|
||||
Optional.ofNullable(bookId).orElse(-1L)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null || getClass() != obj.getClass())
|
||||
return false;
|
||||
AuthorsBooksId other = (AuthorsBooksId) obj;
|
||||
return
|
||||
Objects.equals(other.authorId, authorId) &&
|
||||
Objects.equals(other.bookId, bookId);
|
||||
}
|
||||
}
|
@ -17,61 +17,72 @@ 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;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(Constants.API_URL + "/books")
|
||||
@RequestMapping(Constants.API_URL + "/book")
|
||||
public class BookController {
|
||||
private final BookService itemService;
|
||||
private final BookService bookService;
|
||||
private final TypeService typeService;
|
||||
private final AuthorService authorService;
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
public BookController(BookService itemService, TypeService typeService,
|
||||
AuthorService authorService, ModelMapper modelMapper) {
|
||||
this.itemService = itemService;
|
||||
public BookController(BookService bookService, TypeService typeService, ModelMapper modelMapper) {
|
||||
this.bookService = bookService;
|
||||
this.typeService = typeService;
|
||||
this.authorService = authorService;
|
||||
this.modelMapper = modelMapper;
|
||||
}
|
||||
|
||||
private BookDto toDto(BookEntity entity) {
|
||||
return modelMapper.map(entity, BookDto.class);
|
||||
}
|
||||
private BookDto toBookDto (BookEntity entity) {
|
||||
BookDto bookDto = modelMapper.map(entity, BookDto.class);
|
||||
bookDto.setAuthorId(entity.getAuthorsBooks().stream().map(x -> x.getAuthor().getId()).toList());
|
||||
return bookDto;
|
||||
}
|
||||
|
||||
private BookEntity toEntity(BookDto dto) {
|
||||
final BookEntity entity = modelMapper.map(dto, BookEntity.class);
|
||||
entity.setType(typeService.get(dto.getTypeId()));
|
||||
entity.setAuthor(authorService.get(dto.getAuthorId()));
|
||||
return entity;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<BookDto> getAll(
|
||||
@RequestParam(name = "typeId", defaultValue = "0") Long typeId,
|
||||
@RequestParam(name = "authorId", defaultValue = "0") Long authorId) {
|
||||
return itemService.getAll(typeId, authorId).stream().map(this::toDto).toList();
|
||||
@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, authorId, page, size).stream().map(this::toBookDto).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public BookDto get(@PathVariable(name = "id") Long id) {
|
||||
return toDto(itemService.get(id));
|
||||
return toBookDto(bookService.get(id));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public BookDto create(@RequestBody @Valid BookDto dto) {
|
||||
return toDto(itemService.create(toEntity(dto)));
|
||||
return toBookDto(bookService.create(toEntity(dto)));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public BookDto update(@PathVariable(name = "id") Long id, @RequestBody BookDto dto) {
|
||||
return toDto(itemService.update(id, toEntity(dto)));
|
||||
return toBookDto(bookService.update(id, toEntity(dto)));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public BookDto delete(@PathVariable(name = "id") Long id) {
|
||||
return toDto(itemService.delete(id));
|
||||
return toBookDto(bookService.delete(id));
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package com.ip.library.books.api;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
@ -7,6 +9,7 @@ import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public class BookDto {
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
private Long id;
|
||||
@NotBlank
|
||||
private String name;
|
||||
@ -14,10 +17,8 @@ public class BookDto {
|
||||
@Min(1)
|
||||
private Long typeId;
|
||||
@NotNull
|
||||
@Min(1)
|
||||
private Long authorId;
|
||||
private List<Long> authorsId;
|
||||
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
@ -26,6 +27,14 @@ public class BookDto {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getTypeId() {
|
||||
return typeId;
|
||||
}
|
||||
@ -34,11 +43,11 @@ public class BookDto {
|
||||
this.typeId = typeId;
|
||||
}
|
||||
|
||||
public Long getAuthorId() {
|
||||
return authorId;
|
||||
public List<Long> getAuthorsId() {
|
||||
return authorsId;
|
||||
}
|
||||
|
||||
public void setAuthorId(Long authorId) {
|
||||
this.authorId = authorId;
|
||||
public void setAuthorId(List<Long> authorsId) {
|
||||
this.authorsId = authorsId;
|
||||
}
|
||||
}
|
||||
|
@ -1,25 +1,46 @@
|
||||
package com.ip.library.books.model;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
import com.ip.library.core.model.BaseEntity;
|
||||
import com.ip.library.types.model.TypeEntity;
|
||||
import com.ip.library.authors.model.AuthorEntity;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.OrderBy;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
import com.ip.library.authors.model.AuthorEntity;
|
||||
import com.ip.library.authors_books.model.AuthorsBooksEntity;
|
||||
|
||||
@Entity
|
||||
@Table(name = "books")
|
||||
public class BookEntity extends BaseEntity {
|
||||
@Column(nullable = false, unique = true, length = 50)
|
||||
private String name;
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "type_id", nullable = false)
|
||||
@OrderBy("id ASC")
|
||||
private TypeEntity type;
|
||||
private AuthorEntity author;
|
||||
@OneToMany(mappedBy = "book", cascade = CascadeType.ALL,
|
||||
orphanRemoval = true, fetch = FetchType.EAGER)
|
||||
@OrderBy("id ASC")
|
||||
private Set<AuthorsBooksEntity> authorsBooks = new HashSet<>();
|
||||
|
||||
public BookEntity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public BookEntity(Long id, String name, TypeEntity type, AuthorEntity author) {
|
||||
super(id);
|
||||
public BookEntity(String name, TypeEntity type) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
@ -38,17 +59,25 @@ public class BookEntity extends BaseEntity {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public AuthorEntity getAuthor() {
|
||||
return author;
|
||||
public Set<AuthorsBooksEntity> getAuthorsBooks() {
|
||||
return authorsBooks;
|
||||
}
|
||||
|
||||
public void setAuthor(AuthorEntity author) {
|
||||
this.author = author;
|
||||
public void setAuthors(Set<AuthorsBooksEntity> authorsBooks) {
|
||||
this.authorsBooks = authorsBooks;
|
||||
}
|
||||
|
||||
public boolean addAuthor(AuthorEntity author) {
|
||||
AuthorsBooksEntity entity = new AuthorsBooksEntity(author, this);
|
||||
boolean result = authorsBooks.add(entity);
|
||||
if (!author.getAuthorsBooks().contains(entity))
|
||||
author.getAuthorsBooks().add(entity);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, type, author);
|
||||
return Objects.hash(id, name, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -58,9 +87,9 @@ public class BookEntity extends BaseEntity {
|
||||
if (obj == null || getClass() != obj.getClass())
|
||||
return false;
|
||||
final BookEntity other = (BookEntity) obj;
|
||||
return Objects.equals(other.getId(), id)
|
||||
&& Objects.equals(other.getName(), name)
|
||||
&& Objects.equals(other.getType(), type)
|
||||
&& Objects.equals(other.getAuthor(), author);
|
||||
return
|
||||
Objects.equals(other.getId(), id)
|
||||
&& Objects.equals(other.getName(), name)
|
||||
&& Objects.equals(other.getType(), type);
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,58 @@
|
||||
package com.ip.library.books.repository;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
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;
|
||||
|
||||
import com.ip.library.books.model.BookEntity;
|
||||
import com.ip.library.core.repository.MapRepository;
|
||||
|
||||
@Repository
|
||||
public class BookRepository extends MapRepository<BookEntity> {
|
||||
public interface BookRepository extends
|
||||
CrudRepository<BookEntity, Long>,
|
||||
PagingAndSortingRepository<BookEntity, Long> {
|
||||
Optional<BookEntity> findByNameIgnoreCase(String name);
|
||||
|
||||
List<BookEntity> findByTypeId(long typeId);
|
||||
|
||||
Page<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")
|
||||
Page<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")
|
||||
Page<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);
|
||||
}
|
||||
|
@ -1,11 +1,15 @@
|
||||
package com.ip.library.books.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
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;
|
||||
@ -13,49 +17,98 @@ 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;
|
||||
}
|
||||
|
||||
public List<BookEntity> getAll(Long typeId, Long authorId) {
|
||||
List<BookEntity> result = repository.getAll();
|
||||
if (!Objects.equals(typeId, 0L)){
|
||||
result = result.stream()
|
||||
.filter(item -> item.getType().getId().equals(typeId))
|
||||
.toList();
|
||||
private void checkNameUniqueness(String name){
|
||||
if (repository.findByNameIgnoreCase(name).isPresent()) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("Book with name %s already exists", name)
|
||||
);
|
||||
}
|
||||
if (!Objects.equals(authorId, 0L)){
|
||||
result = result.stream()
|
||||
.filter(item -> item.getAuthor().getId().equals(authorId))
|
||||
.toList();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public List<BookEntity> getAll() {
|
||||
return repository.getAll();
|
||||
@Transactional(readOnly = true)
|
||||
public List<BookEntity> getAll(){
|
||||
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
|
||||
}
|
||||
|
||||
public BookEntity get(Long id) {
|
||||
return Optional.ofNullable(repository.get(id))
|
||||
.orElseThrow(() -> new NotFoundException(id));
|
||||
@Transactional(readOnly = true)
|
||||
public Page<BookEntity> getAll(long typeId, long authorId, int page, int size) {
|
||||
PageRequest pageRequest = PageRequest.of(page, size);
|
||||
if (typeId <= 0L && authorId <= 0L)
|
||||
return repository.findAll(pageRequest);
|
||||
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)
|
||||
public List<BookEntity> getAll(long typeId, long authorId) {
|
||||
if (typeId <= 0L && authorId <= 0L)
|
||||
return StreamSupport.stream(repository.findAll().spliterator(),
|
||||
false).toList();
|
||||
if (authorId <= 0L)
|
||||
return repository.findByTypeId(typeId);
|
||||
if (typeId <= 0L)
|
||||
return repository.findByAuthorId(authorId);
|
||||
return repository.findByAuthorIdAndTypeId(authorId, typeId);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public BookEntity get(long id) {
|
||||
return repository.findById(id)
|
||||
.orElseThrow(() -> new NotFoundException(BookEntity.class, id));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public BookEntity create(BookEntity entity) {
|
||||
return repository.create(entity);
|
||||
if (entity == null) {
|
||||
throw new IllegalArgumentException("Creating BookEntity is null");
|
||||
}
|
||||
checkNameUniqueness(entity.getName());
|
||||
return repository.save(entity);
|
||||
}
|
||||
|
||||
public BookEntity update(Long id, BookEntity entity) {
|
||||
@Transactional
|
||||
public BookEntity update(long id, BookEntity entity) {
|
||||
if (entity == null) {
|
||||
throw new IllegalArgumentException("Updating BookEntity is null");
|
||||
}
|
||||
final BookEntity existsEntity = get(id);
|
||||
checkNameUniqueness(entity.getName());
|
||||
existsEntity.setName(entity.getName());
|
||||
existsEntity.setType(entity.getType());
|
||||
existsEntity.setAuthor(entity.getAuthor());
|
||||
return repository.update(existsEntity);
|
||||
return repository.save(existsEntity);
|
||||
}
|
||||
|
||||
public BookEntity delete(Long id) {
|
||||
@Transactional
|
||||
public BookEntity delete(long id) {
|
||||
final BookEntity existsEntity = get(id);
|
||||
return repository.delete(existsEntity);
|
||||
repository.delete(existsEntity);
|
||||
return existsEntity;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public int getBookSubscribersNumber(long bookId) {
|
||||
return repository.getBookSubscribersNumber(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);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,97 @@
|
||||
package com.ip.library.core.api;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PageDto<D> {
|
||||
private List<D> items = new ArrayList<>();
|
||||
private int itemsCount;
|
||||
private int currentPage;
|
||||
private int currentSize;
|
||||
private int totalPages;
|
||||
private long totalItems;
|
||||
private boolean isFirst;
|
||||
private boolean isLast;
|
||||
private boolean hasNext;
|
||||
private boolean hasPrevious;
|
||||
|
||||
public List<D> getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
public void setItems(List<D> items) {
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
public int getItemsCount() {
|
||||
return itemsCount;
|
||||
}
|
||||
|
||||
public void setItemsCount(int itemsCount) {
|
||||
this.itemsCount = itemsCount;
|
||||
}
|
||||
|
||||
public int getCurrentPage() {
|
||||
return currentPage;
|
||||
}
|
||||
|
||||
public void setCurrentPage(int currentPage) {
|
||||
this.currentPage = currentPage;
|
||||
}
|
||||
|
||||
public int getCurrentSize() {
|
||||
return currentSize;
|
||||
}
|
||||
|
||||
public void setCurrentSize(int currentSize) {
|
||||
this.currentSize = currentSize;
|
||||
}
|
||||
|
||||
public int getTotalPages() {
|
||||
return totalPages;
|
||||
}
|
||||
|
||||
public void setTotalPages(int totalPages) {
|
||||
this.totalPages = totalPages;
|
||||
}
|
||||
|
||||
public long getTotalItems() {
|
||||
return totalItems;
|
||||
}
|
||||
|
||||
public void setTotalItems(long totalItems) {
|
||||
this.totalItems = totalItems;
|
||||
}
|
||||
|
||||
public boolean isFirst() {
|
||||
return isFirst;
|
||||
}
|
||||
|
||||
public void setFirst(boolean isFirst) {
|
||||
this.isFirst = isFirst;
|
||||
}
|
||||
|
||||
public boolean isLast() {
|
||||
return isLast;
|
||||
}
|
||||
|
||||
public void setLast(boolean isLast) {
|
||||
this.isLast = isLast;
|
||||
}
|
||||
|
||||
public boolean isHasNext() {
|
||||
return hasNext;
|
||||
}
|
||||
|
||||
public void setHasNext(boolean hasNext) {
|
||||
this.hasNext = hasNext;
|
||||
}
|
||||
|
||||
public boolean isHasPrevious() {
|
||||
return hasPrevious;
|
||||
}
|
||||
|
||||
public void setHasPrevious(boolean hasPrevious) {
|
||||
this.hasPrevious = hasPrevious;
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.ip.library.core.api;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
|
||||
public class PageDtoMapper {
|
||||
private PageDtoMapper() {}
|
||||
|
||||
public static <D, E> PageDto<D> toDto(Page<E> page, Function<E, D> mapper) {
|
||||
final PageDto<D> dto = new PageDto<>();
|
||||
dto.setItems(page.getContent().stream().map(mapper::apply).toList());
|
||||
dto.setItemsCount(page.getNumberOfElements());
|
||||
dto.setCurrentPage(page.getNumber());
|
||||
dto.setCurrentSize(page.getSize());
|
||||
dto.setTotalPages(page.getTotalPages());
|
||||
dto.setTotalItems(page.getTotalElements());
|
||||
dto.setFirst(page.isFirst());
|
||||
dto.setLast(page.isLast());
|
||||
dto.setHasNext(page.hasNext());
|
||||
dto.setHasPrevious(page.hasPrevious());
|
||||
return dto;
|
||||
}
|
||||
}
|
@ -1,8 +1,11 @@
|
||||
package com.ip.library.core.configuration;
|
||||
|
||||
public class Constants {
|
||||
public static final String SEQUENCE_NAME = "hibernate_sequence";
|
||||
|
||||
public static final String API_URL = "/api/1.0";
|
||||
|
||||
private Constants() {
|
||||
}
|
||||
public static final String DEFAULT_PAGE_SIZE = "5";
|
||||
|
||||
private Constants() {}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package com.ip.library.core.error;
|
||||
|
||||
public class NotFoundException extends RuntimeException {
|
||||
public NotFoundException(Long id) {
|
||||
super(String.format("Entity with id [%s] is not found or not exists", id));
|
||||
public <T> NotFoundException(Class<T> clazz, Long id) {
|
||||
super(String.format("%s with id [%s] is not found or not exists", clazz.getSimpleName(), id));
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +1,23 @@
|
||||
package com.ip.library.core.model;
|
||||
|
||||
import com.ip.library.core.configuration.Constants;
|
||||
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.MappedSuperclass;
|
||||
import jakarta.persistence.SequenceGenerator;
|
||||
|
||||
@MappedSuperclass
|
||||
public abstract class BaseEntity {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = Constants.SEQUENCE_NAME)
|
||||
@SequenceGenerator(name = Constants.SEQUENCE_NAME, sequenceName = Constants.SEQUENCE_NAME, allocationSize = 1)
|
||||
protected Long id;
|
||||
|
||||
protected BaseEntity() {
|
||||
}
|
||||
|
||||
protected BaseEntity(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
@ -1,17 +0,0 @@
|
||||
package com.ip.library.core.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface CommonRepository<E, T> {
|
||||
List<E> getAll();
|
||||
|
||||
E get(T id);
|
||||
|
||||
E create(E entity);
|
||||
|
||||
E update(E entity);
|
||||
|
||||
E delete(E entity);
|
||||
|
||||
void deleteAll();
|
||||
}
|
@ -1,57 +0,0 @@
|
||||
package com.ip.library.core.repository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import com.ip.library.core.model.BaseEntity;
|
||||
|
||||
public abstract class MapRepository<E extends BaseEntity> implements CommonRepository<E, Long> {
|
||||
private final Map<Long, E> entities = new TreeMap<>();
|
||||
private Long lastId = 0L;
|
||||
|
||||
protected MapRepository() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<E> getAll() {
|
||||
return entities.values().stream().toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E get(Long id) {
|
||||
return entities.get(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public E create(E entity) {
|
||||
lastId++;
|
||||
entity.setId(lastId);
|
||||
entities.put(lastId, entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E update(E entity) {
|
||||
if (get(entity.getId()) == null) {
|
||||
return null;
|
||||
}
|
||||
entities.put(entity.getId(), entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E delete(E entity) {
|
||||
if (get(entity.getId()) == null) {
|
||||
return null;
|
||||
}
|
||||
entities.remove(entity.getId());
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteAll() {
|
||||
lastId = 0L;
|
||||
entities.clear();
|
||||
}
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
package com.ip.library.favorites.model;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import com.ip.library.books.model.BookEntity;
|
||||
import com.ip.library.users.model.UserEntity;
|
||||
|
||||
import jakarta.persistence.EmbeddedId;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.MapsId;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "favorites")
|
||||
public class FavoriteEntity {
|
||||
@EmbeddedId
|
||||
private UserBookId id = new UserBookId();
|
||||
@ManyToOne
|
||||
@MapsId("userId")
|
||||
@JoinColumn(name = "user_id")
|
||||
private UserEntity user;
|
||||
@ManyToOne
|
||||
@MapsId("bookId")
|
||||
@JoinColumn(name = "book_id")
|
||||
private BookEntity book;
|
||||
|
||||
public FavoriteEntity() {}
|
||||
|
||||
public FavoriteEntity(UserEntity user, BookEntity book) {
|
||||
this.user = user;
|
||||
this.book = book;
|
||||
}
|
||||
|
||||
public void setId(UserBookId id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public UserBookId getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setUser(UserEntity user) {
|
||||
this.user = user;
|
||||
if (!user.getFavorites().contains(this))
|
||||
user.getFavorites().add(this);
|
||||
}
|
||||
|
||||
public UserEntity getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setBook(BookEntity book) {
|
||||
this.book = book;
|
||||
}
|
||||
|
||||
public BookEntity getBook() {
|
||||
return book;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, user.getId(), book.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null || getClass() != obj.getClass())
|
||||
return false;
|
||||
final FavoriteEntity other = (FavoriteEntity) obj;
|
||||
return
|
||||
Objects.equals(other.getId(), id) &&
|
||||
Objects.equals(other.getUser().getId(), user.getId()) &&
|
||||
Objects.equals(other.getBook().getId(), book.getId());
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
package com.ip.library.favorites.model;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
import jakarta.persistence.Embeddable;
|
||||
|
||||
@Embeddable
|
||||
public class UserBookId {
|
||||
private Long userId;
|
||||
private Long bookId;
|
||||
|
||||
public UserBookId() {}
|
||||
|
||||
public UserBookId(Long userId, Long bookId) {
|
||||
this.userId = userId;
|
||||
this.bookId = bookId;
|
||||
}
|
||||
|
||||
public void setUserId(Long userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public Long getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setBookId(Long bookId) {
|
||||
this.bookId = bookId;
|
||||
}
|
||||
|
||||
public Long getBookId() {
|
||||
return bookId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(
|
||||
Optional.ofNullable(userId).orElse(-1L),
|
||||
Optional.ofNullable(bookId).orElse(-1L)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null || getClass() != obj.getClass())
|
||||
return false;
|
||||
UserBookId other = (UserBookId) obj;
|
||||
return
|
||||
Objects.equals(other.userId, userId) &&
|
||||
Objects.equals(other.bookId, bookId);
|
||||
}
|
||||
}
|
@ -3,13 +3,15 @@ package com.ip.library.types.api;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.Size;
|
||||
|
||||
public class TypeDto {
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
private Long id;
|
||||
@NotBlank
|
||||
@Size(min = 5, max = 20)
|
||||
private String name;
|
||||
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
@ -4,15 +4,21 @@ import java.util.Objects;
|
||||
|
||||
import com.ip.library.core.model.BaseEntity;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "types")
|
||||
public class TypeEntity extends BaseEntity {
|
||||
@Column(nullable = false, unique = true, length = 20)
|
||||
private String name;
|
||||
|
||||
public TypeEntity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public TypeEntity(Long id, String name) {
|
||||
super(id);
|
||||
public TypeEntity(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@ -36,8 +42,9 @@ public class TypeEntity extends BaseEntity {
|
||||
if (obj == null || getClass() != obj.getClass())
|
||||
return false;
|
||||
final TypeEntity other = (TypeEntity) obj;
|
||||
return Objects.equals(other.getId(), id)
|
||||
&& Objects.equals(other.getName(), name);
|
||||
return
|
||||
Objects.equals(other.getId(), id)
|
||||
&& Objects.equals(other.getName(), name);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,10 +1,11 @@
|
||||
package com.ip.library.types.repository;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import com.ip.library.core.repository.MapRepository;
|
||||
import com.ip.library.types.model.TypeEntity;
|
||||
|
||||
@Repository
|
||||
public class TypeRepository extends MapRepository<TypeEntity> {
|
||||
public interface TypeRepository extends CrudRepository<TypeEntity, Long> {
|
||||
Optional<TypeEntity> findByNameIgnoreCase(String name);
|
||||
}
|
||||
|
@ -1,9 +1,10 @@
|
||||
package com.ip.library.types.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.ip.library.core.error.NotFoundException;
|
||||
import com.ip.library.types.model.TypeEntity;
|
||||
@ -17,27 +18,49 @@ public class TypeService {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
private void checkNameUniqueness(String name){
|
||||
if (repository.findByNameIgnoreCase(name).isPresent()) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("Type with name %s already exists", name)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<TypeEntity> getAll() {
|
||||
return repository.getAll();
|
||||
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
|
||||
}
|
||||
|
||||
public TypeEntity get(Long id) {
|
||||
return Optional.ofNullable(repository.get(id))
|
||||
.orElseThrow(() -> new NotFoundException(id));
|
||||
@Transactional(readOnly = true)
|
||||
public TypeEntity get(long id) {
|
||||
return repository.findById(id)
|
||||
.orElseThrow(() -> new NotFoundException(TypeEntity.class, id));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public TypeEntity create(TypeEntity entity) {
|
||||
return repository.create(entity);
|
||||
if (entity == null) {
|
||||
throw new IllegalArgumentException("Creating TypeEntity is null");
|
||||
}
|
||||
checkNameUniqueness(entity.getName());
|
||||
return repository.save(entity);
|
||||
}
|
||||
|
||||
public TypeEntity update(Long id, TypeEntity entity) {
|
||||
@Transactional
|
||||
public TypeEntity update(long id, TypeEntity entity) {
|
||||
if (entity == null) {
|
||||
throw new IllegalArgumentException("Updating TypeEntity is null");
|
||||
}
|
||||
final TypeEntity existsEntity = get(id);
|
||||
checkNameUniqueness(entity.getName());
|
||||
existsEntity.setName(entity.getName());
|
||||
return repository.update(existsEntity);
|
||||
return repository.save(existsEntity);
|
||||
}
|
||||
|
||||
public TypeEntity delete(Long id) {
|
||||
@Transactional
|
||||
public TypeEntity delete(long id) {
|
||||
final TypeEntity existsEntity = get(id);
|
||||
return repository.delete(existsEntity);
|
||||
repository.delete(existsEntity);
|
||||
return existsEntity;
|
||||
}
|
||||
}
|
||||
|
@ -10,8 +10,13 @@ import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
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.core.api.PageDto;
|
||||
import com.ip.library.core.api.PageDtoMapper;
|
||||
import com.ip.library.core.configuration.Constants;
|
||||
import com.ip.library.users.model.UserEntity;
|
||||
import com.ip.library.users.service.UserService;
|
||||
@ -29,51 +34,71 @@ public class UserController {
|
||||
this.modelMapper = modelMapper;
|
||||
}
|
||||
|
||||
private UserDto toDto(UserEntity entity) {
|
||||
private BookDto toBookDto (BookEntity entity) {
|
||||
BookDto bookDto = modelMapper.map(entity, BookDto.class);
|
||||
bookDto.setAuthorId(entity.getAuthorsBooks().stream().map(x -> x.getAuthor().getId()).toList());
|
||||
return bookDto;
|
||||
}
|
||||
|
||||
private UserDto toUserDto(UserEntity entity) {
|
||||
return modelMapper.map(entity, UserDto.class);
|
||||
}
|
||||
|
||||
private UserEntity toEntity(UserDto dto) {
|
||||
private UserEntity toUserEntity(UserDto dto) {
|
||||
return modelMapper.map(dto, UserEntity.class);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<UserDto> getAll() {
|
||||
return userService.getAll().stream().map(this::toDto).toList();
|
||||
public PageDto<UserDto> getAll(
|
||||
@RequestParam(name = "page", defaultValue = "0") int page,
|
||||
@RequestParam(name = "size", defaultValue = Constants.DEFAULT_PAGE_SIZE) int size) {
|
||||
return PageDtoMapper.toDto(userService.getAll(page, size), this::toUserDto);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public UserDto get(@PathVariable(name = "id") Long id) {
|
||||
return toDto(userService.get(id));
|
||||
return toUserDto(userService.get(id));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public UserDto create(@RequestBody @Valid UserDto dto) {
|
||||
return toDto(userService.create(toEntity(dto)));
|
||||
return toUserDto(userService.create(toUserEntity(dto)));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public UserDto update(@PathVariable(name = "id") Long id, @RequestBody UserDto dto) {
|
||||
return toDto(userService.update(id, toEntity(dto)));
|
||||
return toUserDto(userService.update(id, toUserEntity(dto)));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public UserDto delete(@PathVariable(name = "id") Long id) {
|
||||
return toDto(userService.delete(id));
|
||||
return toUserDto(userService.delete(id));
|
||||
}
|
||||
|
||||
@PutMapping("password/{id}")
|
||||
@PutMapping("/password/{id}")
|
||||
public UserDto changePassword(@PathVariable(name = "id") Long id, @RequestBody String newPassword) {
|
||||
return toDto(userService.changePassword(id, newPassword));
|
||||
return toUserDto(userService.changePassword(id, newPassword));
|
||||
}
|
||||
|
||||
@GetMapping("{id}/books/{bookId}")
|
||||
public boolean addBook(@PathVariable(name = "id") Long id, @PathVariable(name = "bookId") Long bookId) {
|
||||
return userService.addBook(id, bookId);
|
||||
@DeleteMapping("/{userId}/books/{bookId}")
|
||||
public boolean removeFavorite(
|
||||
@PathVariable(name = "userId") Long userId,
|
||||
@PathVariable(name = "bookId") Long bookId) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@DeleteMapping("{id}/books/{bookId}")
|
||||
public boolean removeBook(@PathVariable(name = "id") Long id, @PathVariable(name = "bookId") Long bookId) {
|
||||
return userService.removeBook(id, bookId);
|
||||
@GetMapping("/{userId}/books/{bookId}")
|
||||
public boolean addFavorite(
|
||||
@PathVariable(name = "userId") Long userId,
|
||||
@PathVariable(name = "bookId") Long bookId) {
|
||||
return userService.addFavorite(userId, bookId);
|
||||
}
|
||||
|
||||
@GetMapping("/{userId}/books")
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -1,24 +1,25 @@
|
||||
package com.ip.library.users.api;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.validation.constraints.Size;
|
||||
|
||||
public class UserDto {
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
private Long id;
|
||||
@NotBlank
|
||||
private String name;
|
||||
@Size(min = 5, max = 20)
|
||||
private String login;
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
@NotBlank
|
||||
@Size(min = 5, max = 20)
|
||||
private String password;
|
||||
@NotBlank
|
||||
private String role;
|
||||
@NotNull
|
||||
private List<Long> books;
|
||||
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
@NotBlank
|
||||
@Size(min = 4, max = 20)
|
||||
private String role;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
@ -27,15 +28,14 @@ public class UserDto {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
public String getLogin() {
|
||||
return login;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
public void setLogin(String name) {
|
||||
this.login = name;
|
||||
}
|
||||
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
@ -44,7 +44,6 @@ public class UserDto {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
public String getRole() {
|
||||
return role;
|
||||
}
|
||||
@ -52,13 +51,4 @@ public class UserDto {
|
||||
public void setRole(String role) {
|
||||
this.role = role;
|
||||
}
|
||||
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
public List<Long> getBooks() {
|
||||
return books;
|
||||
}
|
||||
|
||||
public void setBooks(List<Long> books) {
|
||||
this.books = books;
|
||||
}
|
||||
}
|
||||
|
@ -1,35 +1,52 @@
|
||||
package com.ip.library.users.model;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.HashSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
import com.ip.library.books.model.BookEntity;
|
||||
import com.ip.library.core.model.BaseEntity;
|
||||
import com.ip.library.favorites.model.FavoriteEntity;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.OrderBy;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class UserEntity extends BaseEntity {
|
||||
private String name;
|
||||
@Column(nullable = false, unique = true, length = 20)
|
||||
private String login;
|
||||
@Column(nullable = false, unique = false, length = 20)
|
||||
private String password;
|
||||
private String role;
|
||||
private List<BookEntity> books;
|
||||
@Column(nullable = false, unique = false, length = 20)
|
||||
private String role = "user";
|
||||
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
@OrderBy("id ASC")
|
||||
private Set<FavoriteEntity> favorites = new HashSet<>();
|
||||
|
||||
public UserEntity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public UserEntity(Long id, String name, String password, String role, List<BookEntity> books) {
|
||||
super(id);
|
||||
this.name = name;
|
||||
public UserEntity(String login, String password) {
|
||||
this.login = login;
|
||||
this.password = password;
|
||||
this.role = role;
|
||||
this.books = books;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
public UserEntity(String login) {
|
||||
this.login = login;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
public String getLogin() {
|
||||
return login;
|
||||
}
|
||||
|
||||
public void setLogin(String name) {
|
||||
this.login = name;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
@ -48,17 +65,25 @@ public class UserEntity extends BaseEntity {
|
||||
this.role = role;
|
||||
}
|
||||
|
||||
public List<BookEntity> getBooks() {
|
||||
return books;
|
||||
public Set<FavoriteEntity> getFavorites() {
|
||||
return favorites;
|
||||
}
|
||||
|
||||
public void setBooks(List<BookEntity> books) {
|
||||
this.books = books;
|
||||
public void setFavorites(Set<FavoriteEntity> favorites) {
|
||||
this.favorites = favorites;
|
||||
}
|
||||
|
||||
public boolean addBook(BookEntity book) {
|
||||
return favorites.add(new FavoriteEntity(this, book));
|
||||
}
|
||||
|
||||
public boolean removeBook(BookEntity book) {
|
||||
return favorites.remove(new FavoriteEntity(this, book));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, name, password, role);
|
||||
return Objects.hash(id, login, password, role);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -68,10 +93,10 @@ public class UserEntity extends BaseEntity {
|
||||
if (obj == null || getClass() != obj.getClass())
|
||||
return false;
|
||||
final UserEntity other = (UserEntity) obj;
|
||||
return Objects.equals(other.getId(), id)
|
||||
&& Objects.equals(other.getName(), name)
|
||||
&& Objects.equals(other.getPassword(), password)
|
||||
&& Objects.equals(other.getRole(), role)
|
||||
&& Objects.equals(other.getBooks(), books);
|
||||
return
|
||||
Objects.equals(other.getId(), id)
|
||||
&& Objects.equals(other.getLogin(), login)
|
||||
&& Objects.equals(other.getPassword(), password)
|
||||
&& Objects.equals(other.getRole(), role);
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,33 @@
|
||||
package com.ip.library.users.repository;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import com.ip.library.core.repository.MapRepository;
|
||||
import org.springframework.data.domain.Page;
|
||||
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;
|
||||
|
||||
import com.ip.library.books.model.BookEntity;
|
||||
import com.ip.library.users.model.UserEntity;
|
||||
|
||||
@Repository
|
||||
public class UserRepository extends MapRepository<UserEntity> {
|
||||
public interface UserRepository extends
|
||||
CrudRepository<UserEntity, Long>,
|
||||
PagingAndSortingRepository<UserEntity, Long> {
|
||||
Optional<UserEntity> findByLoginIgnoreCase(String login);
|
||||
|
||||
@Query(
|
||||
"select f.book " +
|
||||
"from FavoriteEntity f " +
|
||||
"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 Page<BookEntity> getUserFavorities(Long userId, Pageable pageable);
|
||||
}
|
||||
|
@ -1,10 +1,12 @@
|
||||
package com.ip.library.users.service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.ip.library.books.model.BookEntity;
|
||||
import com.ip.library.books.service.BookService;
|
||||
@ -15,66 +17,93 @@ import com.ip.library.users.repository.UserRepository;
|
||||
@Service
|
||||
public class UserService {
|
||||
private final UserRepository repository;
|
||||
private final BookService bookService;
|
||||
private final BookService bookService;
|
||||
|
||||
public UserService(UserRepository repository, BookService bookService) {
|
||||
this.repository = repository;
|
||||
this.bookService = bookService;
|
||||
}
|
||||
|
||||
private void checkLoginUniqueness(String name){
|
||||
if (repository.findByLoginIgnoreCase(name).isPresent()) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("Type with name %s already exists", name)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<UserEntity> getAll() {
|
||||
return repository.getAll();
|
||||
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
|
||||
}
|
||||
|
||||
public UserEntity get(Long id) {
|
||||
return Optional.ofNullable(repository.get(id))
|
||||
.orElseThrow(() -> new NotFoundException(id));
|
||||
@Transactional(readOnly = true)
|
||||
public Page<UserEntity> getAll(int page, int size) {
|
||||
return repository.findAll(PageRequest.of(page, size));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public UserEntity get(long id) {
|
||||
return repository.findById(id)
|
||||
.orElseThrow(() -> new NotFoundException(UserEntity.class, id));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public UserEntity create(UserEntity entity) {
|
||||
entity.setRole("user");
|
||||
entity.setBooks(new ArrayList<>());
|
||||
return repository.create(entity);
|
||||
checkLoginUniqueness(entity.getLogin());
|
||||
return repository.save(entity);
|
||||
}
|
||||
|
||||
public UserEntity update(Long id, UserEntity entity) {
|
||||
@Transactional
|
||||
public UserEntity update(long id, UserEntity entity) {
|
||||
final UserEntity existsEntity = get(id);
|
||||
existsEntity.setName(entity.getName());
|
||||
return repository.update(existsEntity);
|
||||
checkLoginUniqueness(entity.getLogin());
|
||||
existsEntity.setLogin(entity.getLogin());
|
||||
return repository.save(existsEntity);
|
||||
}
|
||||
|
||||
public UserEntity delete(Long id) {
|
||||
@Transactional
|
||||
public UserEntity delete(long id) {
|
||||
final UserEntity existsEntity = get(id);
|
||||
return repository.delete(existsEntity);
|
||||
repository.delete(existsEntity);
|
||||
return existsEntity;
|
||||
}
|
||||
|
||||
public UserEntity giveAdminRole(Long id) {
|
||||
@Transactional
|
||||
public UserEntity giveAdminRole(long id) {
|
||||
final UserEntity existsEntity = get(id);
|
||||
existsEntity.setRole("admin");
|
||||
return repository.update(existsEntity);
|
||||
return repository.save(existsEntity);
|
||||
}
|
||||
|
||||
public UserEntity giveUserRole(Long id) {
|
||||
@Transactional
|
||||
public UserEntity giveUserRole(long id) {
|
||||
final UserEntity existsEntity = get(id);
|
||||
existsEntity.setRole("user");
|
||||
return repository.update(existsEntity);
|
||||
return repository.save(existsEntity);
|
||||
}
|
||||
|
||||
public UserEntity changePassword(Long id, String newPassword) {
|
||||
@Transactional
|
||||
public UserEntity changePassword(long id, String newPassword) {
|
||||
final UserEntity existsEntity = get(id);
|
||||
existsEntity.setPassword(newPassword);
|
||||
return repository.update(existsEntity);
|
||||
return repository.save(existsEntity);
|
||||
}
|
||||
|
||||
public boolean addBook(Long id, Long bookId) {
|
||||
final UserEntity existsEntity = get(id);
|
||||
@Transactional
|
||||
public boolean addFavorite(long userId, long bookId) {
|
||||
final UserEntity existsUser = get(userId);
|
||||
final BookEntity book = bookService.get(bookId);
|
||||
return existsEntity.getBooks().add(book);
|
||||
return existsUser.addBook(book);
|
||||
}
|
||||
|
||||
public boolean removeBook(Long id, Long bookId) {
|
||||
final UserEntity existsEntity = get(id);
|
||||
final BookEntity book = bookService.get(bookId);
|
||||
return existsEntity.getBooks().remove(book);
|
||||
@Transactional(readOnly = true)
|
||||
public List<BookEntity> getUserFavorities (long userId) {
|
||||
return repository.getUserFavorities(userId);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Page<BookEntity> getUserFavorities (long userId, int page, int size) {
|
||||
return repository.getUserFavorities(userId, PageRequest.of(page, size));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,8 @@
|
||||
# Dev H2 JPA Settings
|
||||
spring.datasource.url=jdbc:h2:file:./data
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=password
|
||||
spring.datasource.driver-class-name=org.h2.Driver
|
||||
|
||||
# H2 console
|
||||
spring.h2.console.enabled=true
|
@ -0,0 +1,9 @@
|
||||
# Logger settings
|
||||
# Available levels are: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
|
||||
logging.level.com.example.demo=INFO
|
||||
|
||||
# Prod PostgreSQL JPA Settings
|
||||
spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/demo-app
|
||||
spring.datasource.username=postgres
|
||||
spring.datasource.password=postgres
|
||||
spring.datasource.driver-class-name=org.postgresql.Driver
|
@ -1 +1,21 @@
|
||||
# Server
|
||||
spring.main.banner-mode=off
|
||||
server.port=8080
|
||||
|
||||
# Logger settings
|
||||
# Available levels are: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
|
||||
logging.level.com.example.demo=DEBUG
|
||||
|
||||
# Set default profile
|
||||
spring.profiles.default=dev
|
||||
|
||||
# Common JPA Settings
|
||||
spring.jpa.hibernate.ddl-auto=create
|
||||
spring.jpa.open-in-view=false
|
||||
# spring.jpa.show-sql=true
|
||||
# spring.jpa.properties.hibernate.format_sql=true
|
||||
|
||||
# Liquibase Settings
|
||||
spring.liquibase.enabled=true
|
||||
spring.liquibase.drop-first=false
|
||||
spring.liquibase.change-log=classpath:db/changelog.xml
|
@ -6,6 +6,7 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
|
||||
import com.ip.library.core.error.NotFoundException;
|
||||
import com.ip.library.authors.model.AuthorEntity;
|
||||
@ -25,9 +26,9 @@ class AuthorsTests {
|
||||
@BeforeEach
|
||||
void createData() {
|
||||
removeData();
|
||||
authorService.create(new AuthorEntity(null, "author1"));
|
||||
authorService.create(new AuthorEntity(null, "author2"));
|
||||
entity = authorService.create(new AuthorEntity(null, "author3"));
|
||||
authorService.create(new AuthorEntity("author1"));
|
||||
authorService.create(new AuthorEntity("author2"));
|
||||
entity = authorService.create(new AuthorEntity("author3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -44,14 +45,11 @@ class AuthorsTests {
|
||||
|
||||
@Test
|
||||
void updateTest() {
|
||||
final Long testId;
|
||||
if (entity.getId() != 1L)
|
||||
testId = 1L;
|
||||
else testId = 2L;
|
||||
final Long oldId = entity.getId();
|
||||
final String testName = entity.getName() + "TEST";
|
||||
entity = authorService.update(entity.getId(), new AuthorEntity(testId, testName));
|
||||
entity = authorService.update(oldId, new AuthorEntity(testName));
|
||||
Assertions.assertEquals(3, authorService.getAll().size());
|
||||
Assertions.assertNotEquals(testId, entity.getId());
|
||||
Assertions.assertEquals(oldId, entity.getId());
|
||||
Assertions.assertEquals(testName, entity.getName());
|
||||
}
|
||||
|
||||
@ -59,8 +57,32 @@ class AuthorsTests {
|
||||
void deleteTest() {
|
||||
authorService.delete(entity.getId());
|
||||
Assertions.assertEquals(2, authorService.getAll().size());
|
||||
final AuthorEntity newEntity = authorService.create(new AuthorEntity(null, entity.getName()));
|
||||
final AuthorEntity newEntity = authorService.create(new AuthorEntity(entity.getName()));
|
||||
Assertions.assertEquals(3, authorService.getAll().size());
|
||||
Assertions.assertNotEquals(entity.getId(), newEntity.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullNameTest() {
|
||||
Assertions.assertThrows(
|
||||
DataIntegrityViolationException.class,
|
||||
() -> authorService.create(new AuthorEntity(null))
|
||||
);
|
||||
Assertions.assertThrows(
|
||||
DataIntegrityViolationException.class,
|
||||
() -> authorService.update(entity.getId(), new AuthorEntity(null))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void uniqueNameTest() {
|
||||
Assertions.assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> authorService.create(new AuthorEntity(entity.getName()))
|
||||
);
|
||||
Assertions.assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> authorService.update(entity.getId(), new AuthorEntity(entity.getName()))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,14 @@
|
||||
package com.ip.library;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
|
||||
import com.ip.library.core.error.NotFoundException;
|
||||
import com.ip.library.types.model.TypeEntity;
|
||||
@ -23,9 +26,11 @@ class BooksTests {
|
||||
private TypeService typeService;
|
||||
@Autowired
|
||||
private AuthorService authorService;
|
||||
private BookEntity book;
|
||||
private TypeEntity type;
|
||||
private AuthorEntity author;
|
||||
private BookEntity book1;
|
||||
private BookEntity book2;
|
||||
private BookEntity book3;
|
||||
private TypeEntity type1;
|
||||
private AuthorEntity author1;
|
||||
|
||||
@AfterEach
|
||||
void removeData() {
|
||||
@ -37,52 +42,103 @@ class BooksTests {
|
||||
@BeforeEach
|
||||
void createData() {
|
||||
removeData();
|
||||
type = typeService.create(new TypeEntity(null, "type1"));
|
||||
var type2 = typeService.create(new TypeEntity(null, "type2"));
|
||||
author = authorService.create(new AuthorEntity(null, "author1"));
|
||||
var author2 = authorService.create(new AuthorEntity(null, "author2"));
|
||||
bookService.create(new BookEntity(null, "book1", type, author2));
|
||||
bookService.create(new BookEntity(null, "book2", type2, author));
|
||||
book = bookService.create(new BookEntity(null, "book3", type, author));
|
||||
type1 = typeService.create(new TypeEntity("type1"));
|
||||
var type2 = typeService.create(new TypeEntity("type2"));
|
||||
author1 = authorService.create(new AuthorEntity("author1"));
|
||||
var author2 = authorService.create(new AuthorEntity("author2"));
|
||||
book1 = bookService.create(new BookEntity("book1", type1));
|
||||
book2 = bookService.create(new BookEntity("book2", type1));
|
||||
book3 = bookService.create(new BookEntity("book3", type2));
|
||||
bookService.addAuthor(author1.getId(), book1.getId());
|
||||
bookService.addAuthor(author1.getId(), book3.getId());
|
||||
bookService.addAuthor(author2.getId(), book1.getId());
|
||||
bookService.addAuthor(author2.getId(), book2.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createTest() {
|
||||
Assertions.assertEquals(3, bookService.getAll().size());
|
||||
Assertions.assertEquals("book3", book.getName());
|
||||
Assertions.assertEquals(type, book.getType());
|
||||
Assertions.assertEquals(author, book.getAuthor());
|
||||
Assertions.assertEquals("book1", book1.getName());
|
||||
Assertions.assertEquals(type1, book1.getType());
|
||||
Assertions.assertEquals(0, bookService.getBookSubscribersNumber(book1.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getTest() {
|
||||
Assertions.assertEquals(book, bookService.get(book.getId()));
|
||||
Assertions.assertEquals(book1, bookService.get(book1.getId()));
|
||||
Assertions.assertThrows(NotFoundException.class, () -> bookService.get(0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateTest() {
|
||||
final Long testId;
|
||||
if (book.getId() != 1L)
|
||||
testId = 1L;
|
||||
else testId = 2L;
|
||||
final String testName = book.getName() + "TEST";
|
||||
final TypeEntity testType = typeService.create(new TypeEntity(null, book.getType().getName() + "TEST"));
|
||||
final AuthorEntity testAuthor = authorService.create(new AuthorEntity(null, book.getAuthor().getName() + "TEST"));
|
||||
book = bookService.update(book.getId(), new BookEntity(testId, testName, testType, testAuthor));
|
||||
final String testName = book1.getName() + "TEST";
|
||||
final TypeEntity testType = typeService.create(
|
||||
new TypeEntity(book1.getType().getName() + "TEST"));
|
||||
book1 = bookService.update(book1.getId(), new BookEntity(testName, testType));
|
||||
Assertions.assertEquals(3, bookService.getAll().size());
|
||||
Assertions.assertNotEquals(testId, book.getId());
|
||||
Assertions.assertEquals(testName, book.getName());
|
||||
Assertions.assertEquals(testType, book.getType());
|
||||
Assertions.assertEquals(testAuthor, book.getAuthor());
|
||||
Assertions.assertEquals(testName, book1.getName());
|
||||
Assertions.assertEquals(testType, book1.getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteTest() {
|
||||
bookService.delete(book.getId());
|
||||
bookService.delete(book1.getId());
|
||||
Assertions.assertEquals(2, bookService.getAll().size());
|
||||
final BookEntity newEntity = bookService.create(new BookEntity(null, book.getName(), book.getType(), book.getAuthor()));
|
||||
final BookEntity newEntity = bookService.create(
|
||||
new BookEntity(book1.getName(), book1.getType()));
|
||||
Assertions.assertEquals(3, bookService.getAll().size());
|
||||
Assertions.assertNotEquals(book.getId(), newEntity.getId());
|
||||
Assertions.assertNotEquals(book1.getId(), newEntity.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullNameTest() {
|
||||
Assertions.assertThrows(
|
||||
DataIntegrityViolationException.class,
|
||||
() -> bookService.create(new BookEntity(null, book1.getType()))
|
||||
);
|
||||
Assertions.assertThrows(
|
||||
DataIntegrityViolationException.class,
|
||||
() -> bookService.update(book1.getId(), new BookEntity(null, book1.getType()))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullTypeTest() {
|
||||
Assertions.assertThrows(
|
||||
DataIntegrityViolationException.class,
|
||||
() -> bookService.create(new BookEntity(book1.getName() + "TEST", null))
|
||||
);
|
||||
Assertions.assertThrows(
|
||||
DataIntegrityViolationException.class,
|
||||
() -> bookService.update(book1.getId(), new BookEntity(book1.getName() + "TEST", null))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void uniqueNameTest() {
|
||||
Assertions.assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> bookService.create(new BookEntity(book1.getName(), book1.getType()))
|
||||
);
|
||||
Assertions.assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> bookService.update(book1.getId(), new BookEntity(book1.getName(), book1.getType()))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getByFilterTest() {
|
||||
//filter by type
|
||||
List<BookEntity> list = bookService.getAll(type1.getId(), -1L);
|
||||
Assertions.assertEquals(2, list.size());
|
||||
Assertions.assertTrue(list.contains(book1) && list.contains(book2));
|
||||
//filter by author
|
||||
list = bookService.getAll(-1L, author1.getId());
|
||||
Assertions.assertEquals(2, list.size());
|
||||
Assertions.assertTrue(list.contains(book1) && list.contains(book3));
|
||||
//filter by type and author
|
||||
list = bookService.getAll(type1.getId(), author1.getId());
|
||||
Assertions.assertEquals(1, list.size());
|
||||
Assertions.assertTrue(list.contains(book1));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,66 @@
|
||||
package com.ip.library;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import com.ip.library.books.model.BookEntity;
|
||||
import com.ip.library.books.service.BookService;
|
||||
import com.ip.library.types.model.TypeEntity;
|
||||
import com.ip.library.types.service.TypeService;
|
||||
import com.ip.library.users.model.UserEntity;
|
||||
import com.ip.library.users.service.UserService;
|
||||
|
||||
@SpringBootTest
|
||||
class FavoritesTests {
|
||||
@Autowired
|
||||
private BookService bookService;
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
@Autowired
|
||||
private TypeService typeService;
|
||||
private UserEntity user1;
|
||||
private UserEntity user2;
|
||||
private BookEntity book1;
|
||||
private BookEntity book2;
|
||||
|
||||
@AfterEach
|
||||
void removeData() {
|
||||
userService.getAll().forEach(item -> userService.delete(item.getId()));
|
||||
bookService.getAll().forEach(item -> bookService.delete(item.getId()));
|
||||
typeService.getAll().forEach(item -> typeService.delete(item.getId()));
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void createData() {
|
||||
removeData();
|
||||
TypeEntity type = typeService.create(new TypeEntity("type1"));
|
||||
book1 = bookService.create(new BookEntity("book1", type));
|
||||
book2 = bookService.create(new BookEntity("book2", type));
|
||||
user1 = userService.create(new UserEntity("user3", "aqw2sed45"));
|
||||
user2 = userService.create(new UserEntity("user1", "123"));
|
||||
userService.create(new UserEntity("user2", "456"));
|
||||
userService.addFavorite(user1.getId(), book2.getId());
|
||||
userService.addFavorite(user2.getId(), book1.getId());
|
||||
userService.addFavorite(user2.getId(), book2.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getBookSubscribersNumberTest() {
|
||||
Assertions.assertEquals(1, bookService.getBookSubscribersNumber(book1.getId()));
|
||||
Assertions.assertEquals(2, bookService.getBookSubscribersNumber(book2.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getUserFavoritiesTest() {
|
||||
List<BookEntity> list = userService.getUserFavorities(user2.getId());
|
||||
Assertions.assertEquals(2, list.size());
|
||||
Assertions.assertTrue(list.contains(book1));
|
||||
Assertions.assertTrue(list.contains(book2));
|
||||
}
|
||||
}
|
@ -1,64 +0,0 @@
|
||||
package com.ip.library;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
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.service.BookService;
|
||||
import com.ip.library.types.model.TypeEntity;
|
||||
import com.ip.library.users.model.UserEntity;
|
||||
import com.ip.library.types.service.TypeService;
|
||||
import com.ip.library.users.service.UserService;
|
||||
|
||||
@SpringBootTest
|
||||
class FavoritiesTests {
|
||||
@Autowired
|
||||
private BookService bookService;
|
||||
@Autowired
|
||||
private TypeService typeService;
|
||||
@Autowired
|
||||
private AuthorService authorService;
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
private UserEntity user;
|
||||
private BookEntity book1;
|
||||
private BookEntity book2;
|
||||
|
||||
@AfterEach
|
||||
void removeData() {
|
||||
authorService.getAll().forEach(item -> authorService.delete(item.getId()));
|
||||
bookService.getAll().forEach(item -> bookService.delete(item.getId()));
|
||||
typeService.getAll().forEach(item -> typeService.delete(item.getId()));
|
||||
userService.getAll().forEach(item -> userService.delete(item.getId()));
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void createData() {
|
||||
removeData();
|
||||
var type = typeService.create(new TypeEntity(null, "type1"));
|
||||
var author = authorService.create(new AuthorEntity(null, "author1"));
|
||||
user = userService.create(new UserEntity(null, "user", "password", null, null));
|
||||
book1 = bookService.create(new BookEntity(null, "book1", type, author));
|
||||
book2 = bookService.create(new BookEntity(null, "book2", type, author));
|
||||
}
|
||||
|
||||
@Test
|
||||
void addAndRemoveBooksTest() {
|
||||
Assertions.assertEquals(0, user.getBooks().size());
|
||||
userService.addBook(user.getId(), book1.getId());
|
||||
Assertions.assertEquals(1, user.getBooks().size());
|
||||
userService.addBook(user.getId(), book2.getId());
|
||||
Assertions.assertEquals(2, user.getBooks().size());
|
||||
Assertions.assertEquals(2, user.getBooks().size());
|
||||
userService.removeBook(user.getId(), book1.getId());
|
||||
Assertions.assertEquals(1, user.getBooks().size());
|
||||
userService.removeBook(user.getId(), book2.getId());
|
||||
Assertions.assertEquals(0, user.getBooks().size());
|
||||
}
|
||||
}
|
@ -6,6 +6,7 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
|
||||
import com.ip.library.core.error.NotFoundException;
|
||||
import com.ip.library.types.model.TypeEntity;
|
||||
@ -25,9 +26,9 @@ class TypesTests {
|
||||
@BeforeEach
|
||||
void createData() {
|
||||
removeData();
|
||||
typeService.create(new TypeEntity(null, "type1"));
|
||||
typeService.create(new TypeEntity(null, "type2"));
|
||||
entity = typeService.create(new TypeEntity(null, "type3"));
|
||||
typeService.create(new TypeEntity("type1"));
|
||||
typeService.create(new TypeEntity("type2"));
|
||||
entity = typeService.create(new TypeEntity("type3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -39,19 +40,17 @@ class TypesTests {
|
||||
@Test
|
||||
void getTest() {
|
||||
Assertions.assertEquals(entity, typeService.get(entity.getId()));
|
||||
Assertions.assertThrows(NotFoundException.class, () -> typeService.get(0L));
|
||||
Assertions.assertThrows(
|
||||
NotFoundException.class, () -> typeService.get(0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateTest() {
|
||||
final Long testId;
|
||||
if (entity.getId() != 1L)
|
||||
testId = 1L;
|
||||
else testId = 2L;
|
||||
final Long oldId = entity.getId();
|
||||
final String testName = entity.getName() + "TEST";
|
||||
entity = typeService.update(entity.getId(), new TypeEntity(testId, testName));
|
||||
entity = typeService.update(oldId, new TypeEntity(testName));
|
||||
Assertions.assertEquals(3, typeService.getAll().size());
|
||||
Assertions.assertNotEquals(testId, entity.getId());
|
||||
Assertions.assertEquals(oldId, entity.getId());
|
||||
Assertions.assertEquals(testName, entity.getName());
|
||||
}
|
||||
|
||||
@ -59,8 +58,33 @@ class TypesTests {
|
||||
void deleteTest() {
|
||||
typeService.delete(entity.getId());
|
||||
Assertions.assertEquals(2, typeService.getAll().size());
|
||||
final TypeEntity newEntity = typeService.create(new TypeEntity(null, entity.getName()));
|
||||
final TypeEntity newEntity = typeService.create(
|
||||
new TypeEntity(entity.getName()));
|
||||
Assertions.assertEquals(3, typeService.getAll().size());
|
||||
Assertions.assertNotEquals(entity.getId(), newEntity.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullNameTest() {
|
||||
Assertions.assertThrows(
|
||||
DataIntegrityViolationException.class,
|
||||
() -> typeService.create(new TypeEntity(null))
|
||||
);
|
||||
Assertions.assertThrows(
|
||||
DataIntegrityViolationException.class,
|
||||
() -> typeService.update(entity.getId(), new TypeEntity(null))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void uniqueNameTest() {
|
||||
Assertions.assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> typeService.create(new TypeEntity(entity.getName()))
|
||||
);
|
||||
Assertions.assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> typeService.update(entity.getId(), new TypeEntity(entity.getName()))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +0,0 @@
|
||||
package com.ip.library;
|
||||
|
||||
public class UserEntity {
|
||||
|
||||
}
|
@ -1,66 +1,43 @@
|
||||
package com.ip.library;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
|
||||
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.service.BookService;
|
||||
import com.ip.library.core.error.NotFoundException;
|
||||
import com.ip.library.types.model.TypeEntity;
|
||||
import com.ip.library.types.service.TypeService;
|
||||
import com.ip.library.users.model.UserEntity;
|
||||
import com.ip.library.users.service.UserService;
|
||||
|
||||
@SpringBootTest
|
||||
class UsersTests {
|
||||
@Autowired
|
||||
private BookService bookService;
|
||||
@Autowired
|
||||
private TypeService typeService;
|
||||
@Autowired
|
||||
private AuthorService authorService;
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
private UserEntity user;
|
||||
private BookEntity book;
|
||||
|
||||
@AfterEach
|
||||
void removeData() {
|
||||
authorService.getAll().forEach(item -> authorService.delete(item.getId()));
|
||||
bookService.getAll().forEach(item -> bookService.delete(item.getId()));
|
||||
typeService.getAll().forEach(item -> typeService.delete(item.getId()));
|
||||
userService.getAll().forEach(item -> userService.delete(item.getId()));
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void createData() {
|
||||
removeData();
|
||||
var type1 = typeService.create(new TypeEntity(null, "type1"));
|
||||
var author1 = authorService.create(new AuthorEntity(null, "author1"));
|
||||
book = bookService.create(new BookEntity(null, "book1", type1, author1));
|
||||
List<BookEntity> books = new ArrayList<BookEntity>();
|
||||
books.add(book);
|
||||
userService.create(new UserEntity(null, "user1", "123", "user", null));
|
||||
userService.create(new UserEntity(null, "user2", "456", "user", null));
|
||||
user = userService.create(new UserEntity(null, "user3", "aqw2sed45", "admin", books));
|
||||
userService.create(new UserEntity("user1", "123"));
|
||||
userService.create(new UserEntity("user2", "456"));
|
||||
user = userService.create(new UserEntity("user3", "aqw2sed45"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createTest() {
|
||||
Assertions.assertEquals(3, userService.getAll().size());
|
||||
Assertions.assertEquals("user3", user.getName());
|
||||
Assertions.assertEquals("user3", user.getLogin());
|
||||
Assertions.assertEquals("aqw2sed45", user.getPassword());
|
||||
Assertions.assertEquals("user", user.getRole());
|
||||
Assertions.assertEquals(0, user.getBooks().size());
|
||||
Assertions.assertEquals(0, user.getFavorites().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -72,31 +49,23 @@ class UsersTests {
|
||||
|
||||
@Test
|
||||
void updateTest() {
|
||||
final Long testId;
|
||||
if (user.getId() != 1L)
|
||||
testId = 1L;
|
||||
else testId = 2L;
|
||||
final String testName = user.getName() + "TEST";
|
||||
final Long oldId = user.getId();
|
||||
final String testName = user.getLogin() + "TEST";
|
||||
final String testPassword = user.getPassword() + "TEST";
|
||||
final String testRole = "admin";
|
||||
List<BookEntity> testBooks = new ArrayList<BookEntity>();
|
||||
testBooks.add(book);
|
||||
user = userService.update(user.getId(), new UserEntity(testId, testName,
|
||||
testPassword, testRole, testBooks));
|
||||
user = userService.update(oldId, new UserEntity(testName, testPassword));
|
||||
Assertions.assertEquals(3, userService.getAll().size());
|
||||
Assertions.assertNotEquals(testId, user.getId());
|
||||
Assertions.assertEquals(testName, user.getName());
|
||||
Assertions.assertEquals(oldId, user.getId());
|
||||
Assertions.assertEquals(testName, user.getLogin());
|
||||
Assertions.assertNotEquals(testPassword, user.getPassword());
|
||||
Assertions.assertNotEquals(testRole, user.getRole());
|
||||
Assertions.assertEquals(0, user.getBooks().size());
|
||||
Assertions.assertEquals(0, userService.getUserFavorities(user.getId()).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteTest() {
|
||||
userService.delete(user.getId());
|
||||
Assertions.assertEquals(2, userService.getAll().size());
|
||||
final UserEntity newEntity = userService.create(new UserEntity(null,
|
||||
user.getName(), user.getPassword(), null, null));
|
||||
final UserEntity newEntity = userService.create(
|
||||
new UserEntity(user.getLogin(), user.getPassword()));
|
||||
Assertions.assertEquals(3, userService.getAll().size());
|
||||
Assertions.assertNotEquals(user.getId(), newEntity.getId());
|
||||
}
|
||||
@ -104,16 +73,52 @@ class UsersTests {
|
||||
@Test
|
||||
void changePasswordTest() {
|
||||
String newPassword = user.getPassword() + "TEST";
|
||||
userService.changePassword(user.getId(), newPassword);
|
||||
user = userService.changePassword(user.getId(), newPassword);
|
||||
Assertions.assertEquals(newPassword, user.getPassword());
|
||||
}
|
||||
|
||||
@Test
|
||||
void changeRoleTest() {
|
||||
Assertions.assertEquals("user", user.getRole());
|
||||
userService.giveAdminRole(user.getId());
|
||||
user = userService.giveAdminRole(user.getId());
|
||||
Assertions.assertEquals("admin", user.getRole());
|
||||
userService.giveUserRole(user.getId());
|
||||
user = userService.giveUserRole(user.getId());
|
||||
Assertions.assertEquals("user", user.getRole());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullLoginTest() {
|
||||
Assertions.assertThrows(
|
||||
DataIntegrityViolationException.class,
|
||||
() -> userService.create(new UserEntity(null, user.getPassword()))
|
||||
);
|
||||
Assertions.assertThrows(
|
||||
DataIntegrityViolationException.class,
|
||||
() -> userService.update(user.getId(), new UserEntity(null))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullPasswordTest() {
|
||||
Assertions.assertThrows(
|
||||
DataIntegrityViolationException.class,
|
||||
() -> userService.create(new UserEntity(user.getLogin() + "TEST", null))
|
||||
);
|
||||
Assertions.assertThrows(
|
||||
DataIntegrityViolationException.class,
|
||||
() -> userService.changePassword(user.getId(), null)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void uniqueLoginTest() {
|
||||
Assertions.assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> userService.create(new UserEntity(user.getLogin(), user.getPassword()))
|
||||
);
|
||||
Assertions.assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> userService.update(user.getId(), new UserEntity(user.getLogin()))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user