Добавление реплик и балансировщик миграции отдельно

This commit is contained in:
Dmitry
2026-10-26 20:58:48 +03:00
parent 0ab3a341aa
commit 189f880651
66 changed files with 2851 additions and 0 deletions
@@ -0,0 +1,126 @@
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.ForbiddenException;
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;
}
private User requireCurrentUser() {
User user = UserContext.getCurrentUser();
if (user == null) {
throw new UnauthorizedException("Unauthorized");
}
return user;
}
@Transactional(readOnly = true)
public User getUserById(UUID id) {
requireCurrentUser();
return userRepository.findById(id)
.orElseThrow(() -> new NotFoundException("Пользователь не найден"));
}
@Transactional
public User updateUser(UUID id, UpdateUserDto dto) {
User me = requireCurrentUser();
if (!me.getId().equals(id)) {
throw new ForbiddenException("Forbidden");
}
User user = userRepository.findById(id)
.orElseThrow(() -> new NotFoundException("Пользователь не найден"));
user.setDisplayName(dto.getDisplayName());
return userRepository.save(user);
}
@Transactional
public void deleteUser(UUID id) {
User me = requireCurrentUser();
if (!me.getId().equals(id)) {
throw new ForbiddenException("Forbidden");
}
if (!userRepository.existsById(id)) {
throw new NotFoundException("Пользователь не найден");
}
userRepository.deleteById(id);
}
@Transactional
public User updateUser(String id, UpdateUserDto updateObject) {
return updateUser(parseUuid(id), updateObject);
}
@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 + " не найден");
}
}
}