Переделка UserAPI под персистентность
This commit is contained in:
parent
67feac64d8
commit
f40bda0dc0
@ -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() {
|
||||
|
@ -4,4 +4,8 @@ public class NotFoundException extends RuntimeException {
|
||||
public NotFoundException(Integer id) {
|
||||
super(String.format("Entity with id <[%s]> not found", id));
|
||||
}
|
||||
|
||||
public <T> NotFoundException(Class<T> clazz, Integer id) {
|
||||
super(String.format("%s with id [%s] not found or not exists", clazz.getSimpleName(), id));
|
||||
}
|
||||
}
|
@ -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() {
|
||||
|
@ -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<PostEntity> 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() {
|
||||
|
@ -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<UserEntity> {
|
||||
public interface UserRepository extends CrudRepository<UserEntity, Integer> {
|
||||
Optional<UserEntity> findByUsernameIgnoreCase(String username);
|
||||
}
|
||||
|
@ -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<UserEntity> 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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user