lab-3 in process
This commit is contained in:
parent
4dbd725780
commit
8ef96d2494
3
SpringApp/.vscode/settings.json
vendored
3
SpringApp/.vscode/settings.json
vendored
@ -1,4 +1,5 @@
|
|||||||
{
|
{
|
||||||
"java.compile.nullAnalysis.mode": "automatic",
|
"java.compile.nullAnalysis.mode": "automatic",
|
||||||
"java.configuration.updateBuildConfiguration": "interactive"
|
"java.configuration.updateBuildConfiguration": "interactive",
|
||||||
|
"java.dependency.packagePresentation": "hierarchical"
|
||||||
}
|
}
|
@ -33,7 +33,7 @@ public class AuthorService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public AuthorEntity get(Long id) {
|
public AuthorEntity get(long id) {
|
||||||
return repository.findById(id)
|
return repository.findById(id)
|
||||||
.orElseThrow(() -> new NotFoundException(AuthorEntity.class, id));
|
.orElseThrow(() -> new NotFoundException(AuthorEntity.class, id));
|
||||||
}
|
}
|
||||||
@ -48,7 +48,7 @@ public class AuthorService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public AuthorEntity update(Long id, AuthorEntity entity) {
|
public AuthorEntity update(long id, AuthorEntity entity) {
|
||||||
if (entity == null) {
|
if (entity == null) {
|
||||||
throw new IllegalArgumentException("Updating AuthorEntity is null");
|
throw new IllegalArgumentException("Updating AuthorEntity is null");
|
||||||
}
|
}
|
||||||
@ -59,7 +59,7 @@ public class AuthorService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public AuthorEntity delete(Long id) {
|
public AuthorEntity delete(long id) {
|
||||||
final AuthorEntity existsEntity = get(id);
|
final AuthorEntity existsEntity = get(id);
|
||||||
repository.delete(existsEntity);
|
repository.delete(existsEntity);
|
||||||
return existsEntity;
|
return existsEntity;
|
||||||
|
@ -52,7 +52,7 @@ public class BookController {
|
|||||||
public List<BookDto> getAll(
|
public List<BookDto> getAll(
|
||||||
@RequestParam(name = "typeId", defaultValue = "0") Long typeId,
|
@RequestParam(name = "typeId", defaultValue = "0") Long typeId,
|
||||||
@RequestParam(name = "authorId", defaultValue = "0") Long authorId) {
|
@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}")
|
@GetMapping("/{id}")
|
||||||
|
@ -7,6 +7,7 @@ import jakarta.validation.constraints.NotBlank;
|
|||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
|
||||||
public class BookDto {
|
public class BookDto {
|
||||||
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||||
private Long id;
|
private Long id;
|
||||||
@NotBlank
|
@NotBlank
|
||||||
private String name;
|
private String name;
|
||||||
@ -17,7 +18,6 @@ public class BookDto {
|
|||||||
@Min(1)
|
@Min(1)
|
||||||
private Long authorId;
|
private Long authorId;
|
||||||
|
|
||||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package com.ip.library.books.service;
|
package com.ip.library.books.service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.stream.StreamSupport;
|
import java.util.stream.StreamSupport;
|
||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@ -20,10 +18,23 @@ public class BookService {
|
|||||||
this.repository = repository;
|
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)
|
@Transactional(readOnly = true)
|
||||||
public List<BookEntity> getAll(Long typeId, Long authorId) {
|
public List<BookEntity> getAll() {
|
||||||
|
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<BookEntity> getAllbyFilter(long typeId, long authorId) {
|
||||||
if (typeId <= 0L && authorId <= 0L) {
|
if (typeId <= 0L && authorId <= 0L) {
|
||||||
return StreamSupport.stream(repository.findAll().spliterator(), null).toList();
|
return getAll();
|
||||||
}
|
}
|
||||||
if (typeId <= 0L){
|
if (typeId <= 0L){
|
||||||
return repository.findByAuthorId(authorId);
|
return repository.findByAuthorId(authorId);
|
||||||
@ -35,33 +46,37 @@ public class BookService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public List<BookEntity> getAll() {
|
public BookEntity get(long id) {
|
||||||
return repository.getAll();
|
return repository.findById(id)
|
||||||
}
|
.orElseThrow(() -> new NotFoundException(BookEntity.class, id));
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
|
||||||
public BookEntity get(Long id) {
|
|
||||||
return Optional.ofNullable(repository.get(id))
|
|
||||||
.orElseThrow(() -> new NotFoundException(id));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public BookEntity create(BookEntity entity) {
|
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
|
@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);
|
final BookEntity existsEntity = get(id);
|
||||||
|
checkNameUniqueness(entity.getName());
|
||||||
existsEntity.setName(entity.getName());
|
existsEntity.setName(entity.getName());
|
||||||
existsEntity.setType(entity.getType());
|
existsEntity.setType(entity.getType());
|
||||||
existsEntity.setAuthor(entity.getAuthor());
|
existsEntity.setAuthor(entity.getAuthor());
|
||||||
return repository.update(existsEntity);
|
return repository.save(existsEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public BookEntity delete(Long id) {
|
public BookEntity delete(long id) {
|
||||||
final BookEntity existsEntity = get(id);
|
final BookEntity existsEntity = get(id);
|
||||||
return repository.delete(existsEntity);
|
repository.delete(existsEntity);
|
||||||
|
return existsEntity;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ public class TypeService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public TypeEntity update(Long id, TypeEntity entity) {
|
public TypeEntity update(long id, TypeEntity entity) {
|
||||||
if (entity == null) {
|
if (entity == null) {
|
||||||
throw new IllegalArgumentException("Updating TypeEntity is null");
|
throw new IllegalArgumentException("Updating TypeEntity is null");
|
||||||
}
|
}
|
||||||
@ -58,7 +58,7 @@ public class TypeService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public TypeEntity delete(Long id) {
|
public TypeEntity delete(long id) {
|
||||||
final TypeEntity existsEntity = get(id);
|
final TypeEntity existsEntity = get(id);
|
||||||
repository.delete(existsEntity);
|
repository.delete(existsEntity);
|
||||||
return existsEntity;
|
return existsEntity;
|
||||||
|
@ -5,20 +5,23 @@ import java.util.List;
|
|||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
import jakarta.validation.constraints.NotBlank;
|
import jakarta.validation.constraints.NotBlank;
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.Size;
|
||||||
|
|
||||||
public class UserDto {
|
public class UserDto {
|
||||||
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||||
private Long id;
|
private Long id;
|
||||||
@NotBlank
|
@NotBlank
|
||||||
|
@Size(min = 5, max = 20)
|
||||||
private String name;
|
private String name;
|
||||||
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||||
@NotBlank
|
@NotBlank
|
||||||
|
@Size(min = 5, max = 20)
|
||||||
private String password;
|
private String password;
|
||||||
@NotBlank
|
|
||||||
private String role;
|
|
||||||
@NotNull
|
|
||||||
private List<Long> books;
|
|
||||||
|
|
||||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||||
|
@NotBlank
|
||||||
|
@Size(min = 4, max = 20)
|
||||||
|
private String role;
|
||||||
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
@ -35,7 +38,6 @@ public class UserDto {
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
|
||||||
public String getPassword() {
|
public String getPassword() {
|
||||||
return password;
|
return password;
|
||||||
}
|
}
|
||||||
@ -44,7 +46,6 @@ public class UserDto {
|
|||||||
this.password = password;
|
this.password = password;
|
||||||
}
|
}
|
||||||
|
|
||||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
|
||||||
public String getRole() {
|
public String getRole() {
|
||||||
return role;
|
return role;
|
||||||
}
|
}
|
||||||
@ -52,13 +53,4 @@ public class UserDto {
|
|||||||
public void setRole(String role) {
|
public void setRole(String role) {
|
||||||
this.role = 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,7 +1,8 @@
|
|||||||
package com.ip.library.users.model;
|
package com.ip.library.users.model;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.HashSet;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import com.ip.library.books.model.BookEntity;
|
import com.ip.library.books.model.BookEntity;
|
||||||
import com.ip.library.core.model.BaseEntity;
|
import com.ip.library.core.model.BaseEntity;
|
||||||
@ -15,21 +16,19 @@ import jakarta.persistence.Table;
|
|||||||
public class UserEntity extends BaseEntity {
|
public class UserEntity extends BaseEntity {
|
||||||
@Column(nullable = false, unique = true, length = 20)
|
@Column(nullable = false, unique = true, length = 20)
|
||||||
private String login;
|
private String login;
|
||||||
@Column(nullable = false, unique = true, length = 20)
|
@Column(nullable = false, unique = false, length = 20)
|
||||||
private String password;
|
private String password;
|
||||||
@Column(nullable = false, unique = true, length = 20)
|
@Column(nullable = false, unique = false, length = 20)
|
||||||
private String role;
|
private String role = "user";
|
||||||
private List<BookEntity> books;
|
private Set<BookEntity> books = new HashSet<>();
|
||||||
|
|
||||||
public UserEntity() {
|
public UserEntity() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
public UserEntity(String name, String password, String role, List<BookEntity> books) {
|
public UserEntity(String login, String password) {
|
||||||
this.login = name;
|
this.login = login;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
this.role = role;
|
|
||||||
this.books = books;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getLogin() {
|
public String getLogin() {
|
||||||
@ -56,11 +55,11 @@ public class UserEntity extends BaseEntity {
|
|||||||
this.role = role;
|
this.role = role;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<BookEntity> getBooks() {
|
public Set<BookEntity> getBooks() {
|
||||||
return books;
|
return books;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setBooks(List<BookEntity> books) {
|
public void setBooks(Set<BookEntity> books) {
|
||||||
this.books = books;
|
this.books = books;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
package com.ip.library.users.repository;
|
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;
|
import com.ip.library.users.model.UserEntity;
|
||||||
|
|
||||||
@Repository
|
public interface UserRepository extends CrudRepository<UserEntity, Long> {
|
||||||
public class UserRepository extends MapRepository<UserEntity> {
|
Optional<UserEntity> findByLoginIgnoreCase(String login);
|
||||||
}
|
}
|
||||||
|
@ -3,8 +3,10 @@ package com.ip.library.users.service;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.stream.StreamSupport;
|
||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import com.ip.library.books.model.BookEntity;
|
import com.ip.library.books.model.BookEntity;
|
||||||
import com.ip.library.books.service.BookService;
|
import com.ip.library.books.service.BookService;
|
||||||
@ -21,58 +23,77 @@ public class UserService {
|
|||||||
this.repository = repository;
|
this.repository = repository;
|
||||||
this.bookService = bookService;
|
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() {
|
public List<UserEntity> getAll() {
|
||||||
return repository.getAll();
|
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public UserEntity get(Long id) {
|
@Transactional(readOnly = true)
|
||||||
return Optional.ofNullable(repository.get(id))
|
public UserEntity get(long id) {
|
||||||
.orElseThrow(() -> new NotFoundException(id));
|
return repository.findById(id)
|
||||||
|
.orElseThrow(() -> new NotFoundException(UserEntity.class, id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
public UserEntity create(UserEntity entity) {
|
public UserEntity create(UserEntity entity) {
|
||||||
entity.setRole("user");
|
checkLoginUniqueness(entity.getLogin());
|
||||||
entity.setBooks(new ArrayList<>());
|
return repository.save(entity);
|
||||||
return repository.create(entity);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public UserEntity update(Long id, UserEntity entity) {
|
@Transactional
|
||||||
|
public UserEntity update(long id, UserEntity entity) {
|
||||||
final UserEntity existsEntity = get(id);
|
final UserEntity existsEntity = get(id);
|
||||||
|
checkLoginUniqueness(entity.getLogin());
|
||||||
existsEntity.setLogin(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);
|
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);
|
final UserEntity existsEntity = get(id);
|
||||||
existsEntity.setRole("admin");
|
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);
|
final UserEntity existsEntity = get(id);
|
||||||
existsEntity.setRole("user");
|
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);
|
final UserEntity existsEntity = get(id);
|
||||||
existsEntity.setPassword(newPassword);
|
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 UserEntity existsEntity = get(id);
|
||||||
final BookEntity book = bookService.get(bookId);
|
final BookEntity book = bookService.get(bookId);
|
||||||
return existsEntity.getBooks().add(book);
|
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 UserEntity existsEntity = get(id);
|
||||||
final BookEntity book = bookService.get(bookId);
|
final BookEntity book = bookService.get(bookId);
|
||||||
return existsEntity.getBooks().remove(book);
|
return existsEntity.getBooks().remove(book);
|
||||||
|
Loading…
Reference in New Issue
Block a user