From f40bda0dc02304c522bd3c6c8e0fcb5b0b8ee3d6 Mon Sep 17 00:00:00 2001 From: "ns.potapov" Date: Mon, 15 Apr 2024 17:15:44 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=BA=D0=B0=20UserAPI=20=D0=BF=D0=BE=D0=B4=20=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D1=81=D0=B8=D1=81=D1=82=D0=B5=D0=BD=D1=82=D0=BD=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/configurations/Constants.java | 2 + .../core/errors/NotFoundException.java | 4 ++ .../nekontakte/core/model/BaseEntity.java | 10 +++++ .../nekontakte/users/model/UserEntity.java | 45 ++++++++++++------- .../users/repository/UserRepository.java | 8 ++-- .../nekontakte/users/service/UserService.java | 40 +++++++++++++---- 6 files changed, 80 insertions(+), 29 deletions(-) diff --git a/nekontakte/src/main/java/com/example/nekontakte/core/configurations/Constants.java b/nekontakte/src/main/java/com/example/nekontakte/core/configurations/Constants.java index 0a8f534..13b6e8c 100644 --- a/nekontakte/src/main/java/com/example/nekontakte/core/configurations/Constants.java +++ b/nekontakte/src/main/java/com/example/nekontakte/core/configurations/Constants.java @@ -1,6 +1,8 @@ package com.example.nekontakte.core.configurations; public class Constants { + public static final String SEQUENCE_NAME = "hibernate_sequence"; + public static final String API_URL = "/api"; private Constants() { diff --git a/nekontakte/src/main/java/com/example/nekontakte/core/errors/NotFoundException.java b/nekontakte/src/main/java/com/example/nekontakte/core/errors/NotFoundException.java index 964c525..ce5b143 100644 --- a/nekontakte/src/main/java/com/example/nekontakte/core/errors/NotFoundException.java +++ b/nekontakte/src/main/java/com/example/nekontakte/core/errors/NotFoundException.java @@ -4,4 +4,8 @@ public class NotFoundException extends RuntimeException { public NotFoundException(Integer id) { super(String.format("Entity with id <[%s]> not found", id)); } + + public NotFoundException(Class clazz, Integer id) { + super(String.format("%s with id [%s] not found or not exists", clazz.getSimpleName(), id)); + } } \ No newline at end of file diff --git a/nekontakte/src/main/java/com/example/nekontakte/core/model/BaseEntity.java b/nekontakte/src/main/java/com/example/nekontakte/core/model/BaseEntity.java index 77ce2a6..334fa19 100644 --- a/nekontakte/src/main/java/com/example/nekontakte/core/model/BaseEntity.java +++ b/nekontakte/src/main/java/com/example/nekontakte/core/model/BaseEntity.java @@ -1,6 +1,16 @@ package com.example.nekontakte.core.model; +import com.example.nekontakte.core.configurations.Constants; + +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.SequenceGenerator; +import jakarta.persistence.Id; + public class BaseEntity { + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = Constants.SEQUENCE_NAME) + @SequenceGenerator(name = Constants.SEQUENCE_NAME, sequenceName = Constants.SEQUENCE_NAME, allocationSize = 1) public Integer id; protected BaseEntity() { diff --git a/nekontakte/src/main/java/com/example/nekontakte/users/model/UserEntity.java b/nekontakte/src/main/java/com/example/nekontakte/users/model/UserEntity.java index ff6d164..541e066 100644 --- a/nekontakte/src/main/java/com/example/nekontakte/users/model/UserEntity.java +++ b/nekontakte/src/main/java/com/example/nekontakte/users/model/UserEntity.java @@ -2,44 +2,55 @@ package com.example.nekontakte.users.model; import java.util.Date; import java.util.Objects; +import java.util.HashSet; +import java.util.Set; import com.example.nekontakte.core.model.BaseEntity; +import com.example.nekontakte.posts.model.PostEntity; +import jakarta.persistence.CascadeType; +import jakarta.persistence.Column; +import jakarta.persistence.OneToMany; +import jakarta.persistence.OrderBy; +import jakarta.persistence.Table; + +@Table(name = "users") public class UserEntity extends BaseEntity { - + @Column(nullable = false) private String name; + @Column(nullable = false) private String surname; + @Column(nullable = false) private Date birthday; private String city; private String avatarImg; + @Column(nullable = false, unique = true) private String username; + @Column(nullable = false) private String password; + @Column(nullable = false) private boolean isAdmin; private String status; + @OneToMany(mappedBy = "user", cascade = CascadeType.ALL) + @OrderBy("id ASC") + private Set posts = new HashSet<>(); public UserEntity() { } public UserEntity( - Integer id, - String username, - String password, - boolean isAdmin, String name, String surname, Date birthday, - String city, - String avatarImg, - String status) { - super(id); - this.username = username; - this.password = password; - this.name = name; - this.surname = surname; - this.birthday = birthday; - this.city = city; - this.avatarImg = avatarImg; - this.status = status; + String username, + String password, + boolean isAdmin) { + setName(name); + setSurname(surname); + setBirthday(birthday); + setUsername(username); + setPassword(password); + setIsAdmin(isAdmin); } public String getName() { diff --git a/nekontakte/src/main/java/com/example/nekontakte/users/repository/UserRepository.java b/nekontakte/src/main/java/com/example/nekontakte/users/repository/UserRepository.java index 0a2be53..b2c88db 100644 --- a/nekontakte/src/main/java/com/example/nekontakte/users/repository/UserRepository.java +++ b/nekontakte/src/main/java/com/example/nekontakte/users/repository/UserRepository.java @@ -1,10 +1,10 @@ package com.example.nekontakte.users.repository; -import org.springframework.stereotype.Repository; +import java.util.Optional; -import com.example.nekontakte.core.repository.MapRepository; +import org.springframework.data.repository.CrudRepository; import com.example.nekontakte.users.model.UserEntity; -@Repository -public class UserRepository extends MapRepository { +public interface UserRepository extends CrudRepository { + Optional findByUsernameIgnoreCase(String username); } diff --git a/nekontakte/src/main/java/com/example/nekontakte/users/service/UserService.java b/nekontakte/src/main/java/com/example/nekontakte/users/service/UserService.java index 6327ba2..131617a 100644 --- a/nekontakte/src/main/java/com/example/nekontakte/users/service/UserService.java +++ b/nekontakte/src/main/java/com/example/nekontakte/users/service/UserService.java @@ -1,7 +1,7 @@ package com.example.nekontakte.users.service; import java.util.List; -import java.util.Optional; +import java.util.stream.StreamSupport; import org.springframework.stereotype.Service; @@ -9,28 +9,50 @@ import com.example.nekontakte.core.errors.NotFoundException; import com.example.nekontakte.users.model.UserEntity; import com.example.nekontakte.users.repository.UserRepository; +import org.springframework.transaction.annotation.Transactional; + @Service public class UserService { private final UserRepository repository; + private boolean checkUsernameIsExist(String username) { + return !repository.findByUsernameIgnoreCase(username).isPresent(); + } + public UserService(UserRepository repository) { this.repository = repository; } + @Transactional(readOnly = true) public List getAll() { - return repository.getAll(); + return StreamSupport.stream(repository.findAll().spliterator(), false).toList(); } + @Transactional(readOnly = true) public UserEntity get(Integer id) { - return Optional.ofNullable(repository.get(id)).orElseThrow(() -> new NotFoundException(id)); + return repository.findById(id).orElseThrow(() -> new NotFoundException(UserEntity.class, id)); } + @Transactional public UserEntity create(UserEntity entity) { - return repository.create(entity); + if (entity == null) { + throw new IllegalArgumentException("Entity is null"); + } + if (checkUsernameIsExist(entity.getUsername())) { + throw new IllegalArgumentException( + String.format("User with username %s is already exists", entity.getUsername())); + } + return repository.save(entity); } + @Transactional public UserEntity update(Integer id, UserEntity entity) { final UserEntity existsentity = get(id); + var usersWithSameUsername = repository.findByUsernameIgnoreCase(entity.getUsername()); + if (usersWithSameUsername.get().getId() != existsentity.getId()) { + throw new IllegalArgumentException( + String.format("User with username %s is already exists", entity.getUsername())); + } existsentity.setUsername(entity.getUsername()); existsentity.setPassword(entity.getPassword()); existsentity.setIsAdmin(entity.getIsAdmin()); @@ -40,12 +62,14 @@ public class UserService { existsentity.setBirthday(entity.getBirthday()); existsentity.setCity(entity.getCity()); existsentity.setStatus(entity.getStatus()); - return repository.update(existsentity); + repository.save(existsentity); + return existsentity; } + @Transactional public UserEntity delete(Integer id) { - final UserEntity existsentity = get(id); - return repository.delete(existsentity); + final UserEntity existsEntity = get(id); + repository.delete(existsEntity); + return existsEntity; } - } \ No newline at end of file