diff --git a/SpringApp/.vscode/settings.json b/SpringApp/.vscode/settings.json index 572b0c0..26462a9 100644 --- a/SpringApp/.vscode/settings.json +++ b/SpringApp/.vscode/settings.json @@ -1,4 +1,5 @@ { "java.compile.nullAnalysis.mode": "automatic", - "java.configuration.updateBuildConfiguration": "interactive" + "java.configuration.updateBuildConfiguration": "interactive", + "java.dependency.packagePresentation": "hierarchical" } \ No newline at end of file diff --git a/SpringApp/library/src/main/java/com/ip/library/authors/service/AuthorService.java b/SpringApp/library/src/main/java/com/ip/library/authors/service/AuthorService.java index 800ff31..9ebe2cb 100644 --- a/SpringApp/library/src/main/java/com/ip/library/authors/service/AuthorService.java +++ b/SpringApp/library/src/main/java/com/ip/library/authors/service/AuthorService.java @@ -33,7 +33,7 @@ public class AuthorService { } @Transactional(readOnly = true) - public AuthorEntity get(Long id) { + public AuthorEntity get(long id) { return repository.findById(id) .orElseThrow(() -> new NotFoundException(AuthorEntity.class, id)); } @@ -48,7 +48,7 @@ public class AuthorService { } @Transactional - public AuthorEntity update(Long id, AuthorEntity entity) { + public AuthorEntity update(long id, AuthorEntity entity) { if (entity == null) { throw new IllegalArgumentException("Updating AuthorEntity is null"); } @@ -59,7 +59,7 @@ public class AuthorService { } @Transactional - public AuthorEntity delete(Long id) { + public AuthorEntity delete(long id) { final AuthorEntity existsEntity = get(id); repository.delete(existsEntity); return existsEntity; diff --git a/SpringApp/library/src/main/java/com/ip/library/books/api/BookController.java b/SpringApp/library/src/main/java/com/ip/library/books/api/BookController.java index 7ecb7a9..9aea2bd 100644 --- a/SpringApp/library/src/main/java/com/ip/library/books/api/BookController.java +++ b/SpringApp/library/src/main/java/com/ip/library/books/api/BookController.java @@ -52,7 +52,7 @@ public class BookController { public List 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(); + return itemService.getAllbyFilter(typeId, authorId).stream().map(this::toDto).toList(); } @GetMapping("/{id}") diff --git a/SpringApp/library/src/main/java/com/ip/library/books/api/BookDto.java b/SpringApp/library/src/main/java/com/ip/library/books/api/BookDto.java index c8e54e9..db6aad7 100644 --- a/SpringApp/library/src/main/java/com/ip/library/books/api/BookDto.java +++ b/SpringApp/library/src/main/java/com/ip/library/books/api/BookDto.java @@ -7,6 +7,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; @@ -17,7 +18,6 @@ public class BookDto { @Min(1) private Long authorId; - @JsonProperty(access = JsonProperty.Access.READ_ONLY) public Long getId() { return id; } diff --git a/SpringApp/library/src/main/java/com/ip/library/books/service/BookService.java b/SpringApp/library/src/main/java/com/ip/library/books/service/BookService.java index 5aa71b9..18e8f2e 100644 --- a/SpringApp/library/src/main/java/com/ip/library/books/service/BookService.java +++ b/SpringApp/library/src/main/java/com/ip/library/books/service/BookService.java @@ -1,8 +1,6 @@ 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.stereotype.Service; @@ -20,10 +18,23 @@ public class BookService { this.repository = repository; } + private void checkNameUniqueness(String name){ + if (repository.findByNameIgnoreCase(name).isPresent()) { + throw new IllegalArgumentException( + String.format("Book with name %s already exists", name) + ); + } + } + @Transactional(readOnly = true) - public List getAll(Long typeId, Long authorId) { + public List getAll() { + return StreamSupport.stream(repository.findAll().spliterator(), false).toList(); + } + + @Transactional(readOnly = true) + public List getAllbyFilter(long typeId, long authorId) { if (typeId <= 0L && authorId <= 0L) { - return StreamSupport.stream(repository.findAll().spliterator(), null).toList(); + return getAll(); } if (typeId <= 0L){ return repository.findByAuthorId(authorId); @@ -35,33 +46,37 @@ public class BookService { } @Transactional(readOnly = true) - public List getAll() { - return repository.getAll(); - } - - @Transactional(readOnly = true) - public BookEntity get(Long id) { - return Optional.ofNullable(repository.get(id)) - .orElseThrow(() -> new NotFoundException(id)); + 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); } @Transactional - public BookEntity update(Long id, BookEntity entity) { + 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); } @Transactional - public BookEntity delete(Long id) { + public BookEntity delete(long id) { final BookEntity existsEntity = get(id); - return repository.delete(existsEntity); + repository.delete(existsEntity); + return existsEntity; } } diff --git a/SpringApp/library/src/main/java/com/ip/library/types/service/TypeService.java b/SpringApp/library/src/main/java/com/ip/library/types/service/TypeService.java index 526a337..82eff00 100644 --- a/SpringApp/library/src/main/java/com/ip/library/types/service/TypeService.java +++ b/SpringApp/library/src/main/java/com/ip/library/types/service/TypeService.java @@ -47,7 +47,7 @@ public class TypeService { } @Transactional - public TypeEntity update(Long id, TypeEntity entity) { + public TypeEntity update(long id, TypeEntity entity) { if (entity == null) { throw new IllegalArgumentException("Updating TypeEntity is null"); } @@ -58,7 +58,7 @@ public class TypeService { } @Transactional - public TypeEntity delete(Long id) { + public TypeEntity delete(long id) { final TypeEntity existsEntity = get(id); repository.delete(existsEntity); return existsEntity; diff --git a/SpringApp/library/src/main/java/com/ip/library/users/api/UserDto.java b/SpringApp/library/src/main/java/com/ip/library/users/api/UserDto.java index c5070f7..a2854df 100644 --- a/SpringApp/library/src/main/java/com/ip/library/users/api/UserDto.java +++ b/SpringApp/library/src/main/java/com/ip/library/users/api/UserDto.java @@ -5,20 +5,23 @@ 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 + @Size(min = 5, max = 20) private String name; + @JsonProperty(access = JsonProperty.Access.READ_ONLY) @NotBlank + @Size(min = 5, max = 20) private String password; - @NotBlank - private String role; - @NotNull - private List books; - @JsonProperty(access = JsonProperty.Access.READ_ONLY) + @NotBlank + @Size(min = 4, max = 20) + private String role; + public Long getId() { return id; } @@ -35,7 +38,6 @@ public class UserDto { this.name = name; } - @JsonProperty(access = JsonProperty.Access.READ_ONLY) public String getPassword() { return password; } @@ -44,7 +46,6 @@ public class UserDto { this.password = password; } - @JsonProperty(access = JsonProperty.Access.READ_ONLY) public String getRole() { return role; } @@ -52,13 +53,4 @@ public class UserDto { public void setRole(String role) { this.role = role; } - - @JsonProperty(access = JsonProperty.Access.READ_ONLY) - public List getBooks() { - return books; - } - - public void setBooks(List books) { - this.books = books; - } } diff --git a/SpringApp/library/src/main/java/com/ip/library/users/model/UserEntity.java b/SpringApp/library/src/main/java/com/ip/library/users/model/UserEntity.java index c2eaac9..69fc764 100644 --- a/SpringApp/library/src/main/java/com/ip/library/users/model/UserEntity.java +++ b/SpringApp/library/src/main/java/com/ip/library/users/model/UserEntity.java @@ -1,7 +1,8 @@ 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; @@ -15,21 +16,19 @@ import jakarta.persistence.Table; public class UserEntity extends BaseEntity { @Column(nullable = false, unique = true, length = 20) private String login; - @Column(nullable = false, unique = true, length = 20) + @Column(nullable = false, unique = false, length = 20) private String password; - @Column(nullable = false, unique = true, length = 20) - private String role; - private List books; + @Column(nullable = false, unique = false, length = 20) + private String role = "user"; + private Set books = new HashSet<>(); public UserEntity() { super(); } - public UserEntity(String name, String password, String role, List books) { - this.login = name; + public UserEntity(String login, String password) { + this.login = login; this.password = password; - this.role = role; - this.books = books; } public String getLogin() { @@ -56,11 +55,11 @@ public class UserEntity extends BaseEntity { this.role = role; } - public List getBooks() { + public Set getBooks() { return books; } - public void setBooks(List books) { + public void setBooks(Set books) { this.books = books; } diff --git a/SpringApp/library/src/main/java/com/ip/library/users/repository/UserRepository.java b/SpringApp/library/src/main/java/com/ip/library/users/repository/UserRepository.java index 65e1f8f..d1f4470 100644 --- a/SpringApp/library/src/main/java/com/ip/library/users/repository/UserRepository.java +++ b/SpringApp/library/src/main/java/com/ip/library/users/repository/UserRepository.java @@ -1,10 +1,11 @@ package com.ip.library.users.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.users.model.UserEntity; -@Repository -public class UserRepository extends MapRepository { +public interface UserRepository extends CrudRepository { + Optional findByLoginIgnoreCase(String login); } diff --git a/SpringApp/library/src/main/java/com/ip/library/users/service/UserService.java b/SpringApp/library/src/main/java/com/ip/library/users/service/UserService.java index e7d4f28..190503c 100644 --- a/SpringApp/library/src/main/java/com/ip/library/users/service/UserService.java +++ b/SpringApp/library/src/main/java/com/ip/library/users/service/UserService.java @@ -3,8 +3,10 @@ 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.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import com.ip.library.books.model.BookEntity; import com.ip.library.books.service.BookService; @@ -21,58 +23,77 @@ public class UserService { 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 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 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); + checkLoginUniqueness(entity.getLogin()); existsEntity.setLogin(entity.getLogin()); - return repository.update(existsEntity); + 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) { + @Transactional + public boolean addBook(long id, long bookId) { final UserEntity existsEntity = get(id); final BookEntity book = bookService.get(bookId); return existsEntity.getBooks().add(book); } - public boolean removeBook(Long id, Long bookId) { + @Transactional + public boolean removeBook(long id, long bookId) { final UserEntity existsEntity = get(id); final BookEntity book = bookService.get(bookId); return existsEntity.getBooks().remove(book);