hit-server-manager/backend/src/main/java/com/hitcommunications/servermanager/services/UsersService.java

99 lines
3.5 KiB
Java

package com.hitcommunications.servermanager.services;
import com.hitcommunications.servermanager.mappers.UsersMapper;
import com.hitcommunications.servermanager.model.Users;
import com.hitcommunications.servermanager.model.dtos.NewUserDTO;
import com.hitcommunications.servermanager.model.dtos.UserDTO;
import com.hitcommunications.servermanager.repositories.UsersRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.security.crypto.password.PasswordEncoder;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
@Service
@RequiredArgsConstructor
public class UsersService {
private final UsersMapper mapper;
private final UsersRepository repo;
private final PasswordEncoder passwordEncoder;
private final String ALLOWED_DOMAIN = Arrays.asList("hittelco.com", "accesscommunications.com").toString();
public UserDTO create(NewUserDTO createDTO) throws IllegalAccessException {
String domain = getDomain(createDTO.email());
if (!ALLOWED_DOMAIN.contains(domain)) {
throw new IllegalAccessException("Email domain not allowed: " + domain);
}
repo.findByEmail(createDTO.email()).ifPresent(entity -> {
throw new RuntimeException("Email already exists: " + createDTO.email());
});
Users entity = mapper.toEntity(createDTO);
entity.setPassword(passwordEncoder.encode(createDTO.password()));
entity = repo.save(entity);
return mapper.toDTO(entity);
}
public UserDTO getById(UUID id) {
return repo.findById(id)
.map(mapper::toDTO)
.orElseThrow(() -> new RuntimeException("User not found with id: " + id));
}
public UserDTO getByUsername(String username) {
return repo.findByUsername(username)
.map(mapper::toDTO)
.orElseThrow(() -> new RuntimeException("User not found with username: " + username));
}
public UserDTO getByEmail(String email) {
return repo.findByEmail(email)
.map(mapper::toDTO)
.orElseThrow(() -> new RuntimeException("User not found with email: " + email));
}
public List<UserDTO> getAll() {
return repo.findAll()
.stream()
.map(mapper::toDTO)
.toList();
}
public UserDTO update(UUID id, NewUserDTO updateDTO) throws IllegalAccessException {
Users entity = repo.findById(id)
.orElseThrow(() -> new RuntimeException("User not found with id: " + id));
String domain = getDomain(updateDTO.email());
if (!ALLOWED_DOMAIN.contains(domain)) {
throw new IllegalAccessException("Email domain not allowed: " + domain);
}
// Check if email already exists (excluding current user)
repo.findByEmail(updateDTO.email()).ifPresent(existingUser -> {
if (!existingUser.getId().equals(id)) {
throw new RuntimeException("Email already exists: " + updateDTO.email());
}
});
mapper.partialUpdate(updateDTO, entity);
entity.setPassword(passwordEncoder.encode(updateDTO.password()));
entity = repo.save(entity);
return mapper.toDTO(entity);
}
public void delete(UUID id) {
if (!repo.existsById(id)) {
throw new RuntimeException("User not found with id: " + id);
}
repo.deleteById(id);
}
private String getDomain(String email) {
return email.substring(email.indexOf("@") + 1);
}
}