lab-3 in process

This commit is contained in:
Zakharov_Rostislav 2024-04-11 14:56:45 +04:00
parent 4dbd725780
commit 8ef96d2494
10 changed files with 105 additions and 76 deletions

View File

@ -1,4 +1,5 @@
{
"java.compile.nullAnalysis.mode": "automatic",
"java.configuration.updateBuildConfiguration": "interactive"
"java.configuration.updateBuildConfiguration": "interactive",
"java.dependency.packagePresentation": "hierarchical"
}

View File

@ -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;

View File

@ -52,7 +52,7 @@ public class BookController {
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();
return itemService.getAllbyFilter(typeId, authorId).stream().map(this::toDto).toList();
}
@GetMapping("/{id}")

View File

@ -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;
}

View File

@ -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<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) {
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<BookEntity> 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;
}
}

View File

@ -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;

View File

@ -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<Long> 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<Long> getBooks() {
return books;
}
public void setBooks(List<Long> books) {
this.books = books;
}
}

View File

@ -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<BookEntity> books;
@Column(nullable = false, unique = false, length = 20)
private String role = "user";
private Set<BookEntity> books = new HashSet<>();
public UserEntity() {
super();
}
public UserEntity(String name, String password, String role, List<BookEntity> 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<BookEntity> getBooks() {
public Set<BookEntity> getBooks() {
return books;
}
public void setBooks(List<BookEntity> books) {
public void setBooks(Set<BookEntity> books) {
this.books = books;
}

View File

@ -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<UserEntity> {
public interface UserRepository extends CrudRepository<UserEntity, Long> {
Optional<UserEntity> findByLoginIgnoreCase(String login);
}

View File

@ -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<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 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);