forked from mtusi2026/mtusi2026lab2
106 lines
3.5 KiB
Java
106 lines
3.5 KiB
Java
package org.example.service;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.example.config.UserContext;
|
|
import org.example.dto.create.CreateUserDto;
|
|
import org.example.dto.update.UpdateUserDto;
|
|
import org.example.exception.NotFoundException;
|
|
import org.example.exception.UnauthorizedException;
|
|
import org.example.exception.UserAlreadyExistsException;
|
|
import org.example.model.User;
|
|
import org.example.repository.UserRepository;
|
|
import org.springframework.dao.DataIntegrityViolationException;
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import java.util.UUID;
|
|
|
|
@Service
|
|
@RequiredArgsConstructor
|
|
public class UserService {
|
|
|
|
private final UserRepository userRepository;
|
|
private final PasswordEncoder passwordEncoder;
|
|
|
|
@Transactional
|
|
public User createUser(CreateUserDto userObject) {
|
|
if (userRepository.existsByUsername(userObject.getUsername())) {
|
|
throw new UserAlreadyExistsException("Пользователь с таким username уже существует: " + userObject.getUsername());
|
|
}
|
|
try {
|
|
User user = User.builder()
|
|
.username(userObject.getUsername())
|
|
.displayName(userObject.getDisplayName())
|
|
.password(passwordEncoder.encode(userObject.getPassword()))
|
|
.build();
|
|
return userRepository.save(user);
|
|
} catch (DataIntegrityViolationException e){
|
|
throw new UserAlreadyExistsException(
|
|
"Пользователь с таким username уже есть: " + userObject.getUsername());
|
|
}
|
|
}
|
|
|
|
@Transactional(readOnly = true)
|
|
public User getCurrentUser() {
|
|
User user = UserContext.getCurrentUser();
|
|
if (user == null) {
|
|
throw new UnauthorizedException("Unauthorized");
|
|
}
|
|
return user;
|
|
}
|
|
|
|
@Transactional(readOnly = true)
|
|
public User getUserById(UUID id) {
|
|
return userRepository.findById(id)
|
|
.orElseThrow(() -> new NotFoundException("Пользователь с id: " + id + " не найден."));
|
|
}
|
|
|
|
@Transactional
|
|
public User updateUser(UUID id, UpdateUserDto updateObject){
|
|
User existing = getUserById(id);
|
|
existing.setDisplayName(updateObject.getDisplayName());
|
|
return userRepository.save(existing);
|
|
}
|
|
|
|
@Transactional
|
|
public User updateUser(String id, UpdateUserDto updateObject) {
|
|
return updateUser(parseUuid(id), updateObject);
|
|
}
|
|
|
|
@Transactional
|
|
public void deleteUser(UUID id) {
|
|
if (!userRepository.existsById(id)) {
|
|
throw new NotFoundException("Пользователь с id: " + id + " не найден");
|
|
}
|
|
userRepository.deleteById(id);
|
|
}
|
|
|
|
@Transactional
|
|
public void deleteUser(String id) {
|
|
deleteUser(parseUuid(id));
|
|
}
|
|
|
|
@Transactional(readOnly = true)
|
|
public boolean exists(UUID id) {
|
|
return userRepository.existsById(id);
|
|
}
|
|
|
|
|
|
@Transactional(readOnly = true)
|
|
public boolean exists(String id) {
|
|
try {
|
|
return exists(parseUuid(id));
|
|
}catch (IllegalArgumentException e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private static UUID parseUuid(String id) {
|
|
try {
|
|
return UUID.fromString(id);
|
|
} catch (IllegalArgumentException e) {
|
|
throw new NotFoundException("Пользователь с id: " + id + " не найден");
|
|
}
|
|
}
|
|
} |