refactor structure of user to feature based structure
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
package SuMoC.backend.user.service;
|
||||
|
||||
import SuMoC.backend.user.models.Friendship;
|
||||
import SuMoC.backend.user.models.User;
|
||||
import SuMoC.backend.user.dtos.MinimalUserDTO;
|
||||
import SuMoC.backend.user.enums.FriendShipStatus;
|
||||
import SuMoC.backend.user.enums.RoleName;
|
||||
import SuMoC.backend.user.enums.VisibilityPrivacy;
|
||||
import SuMoC.backend.user.UserRepository;
|
||||
import jakarta.transaction.Transactional;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class UserService {
|
||||
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
public UserService(UserRepository userRepository) {
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
public MinimalUserDTO getUser(long id) {
|
||||
User sender = getSender();
|
||||
|
||||
User requested = userRepository.findById(id).get();
|
||||
|
||||
// Prüfen, ob eine Freundschaft zum Sender existiert
|
||||
FriendShipStatus status = requested.getFriendships().stream()
|
||||
.filter(f -> sender.equals(f.getFriend()) || sender.equals(f.getUser()))
|
||||
.map(Friendship::getStatus)
|
||||
.findFirst()
|
||||
.orElse(null); // nimmt die erste gefundene Freundschaft
|
||||
|
||||
if (status == FriendShipStatus.BLOCKED) throw new RuntimeException("Kein Zugriff auf dieses Profil");
|
||||
|
||||
//TODO: Think about private profile visibility heuristics
|
||||
// if (requested.getVisibilityPrivacy() == VisibilityPrivacy.PRIVATE && !status.equals(FriendShipStatus.ACCEPTED)) {
|
||||
// throw new RuntimeException("Kein Zugriff auf dieses Profil");
|
||||
// }
|
||||
|
||||
switch (status) {
|
||||
case ACCEPTED -> {
|
||||
return new MinimalUserDTO(requested.getId(), requested.getPrivateName());
|
||||
}
|
||||
|
||||
case BLOCKED -> {
|
||||
throw new RuntimeException("Kein Zugriff auf dieses Profil");
|
||||
}
|
||||
|
||||
default -> {
|
||||
return new MinimalUserDTO(requested.getId(), requested.getPublicName());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void deleteUser(Long id) {
|
||||
User sender = getSender();
|
||||
|
||||
if (!sender.getId().equals(id) && !sender.getRoles().contains(RoleName.ADMIN)) {
|
||||
|
||||
throw new RuntimeException("Ein Nutzer kann nur sein eigenes Profil löschen.");
|
||||
}
|
||||
// User is admin or deleting own profile
|
||||
userRepository.deleteById(id);
|
||||
}
|
||||
|
||||
public User getSender() {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
return userRepository.findByEmail(authentication.getName()).get();
|
||||
}
|
||||
|
||||
public void changePassword(Long id, String newPassword) {
|
||||
User sender = getSender();
|
||||
if (!sender.getId().equals(id)) {
|
||||
throw new RuntimeException("Ein Nutzer kann nur sein eigenes Passwort ändern.");
|
||||
}
|
||||
// TODO: Add Email Notification for password change
|
||||
sender.setPassword(newPassword);
|
||||
userRepository.save(sender);
|
||||
}
|
||||
|
||||
public void changeEmail(Long id, String newEmail) {
|
||||
|
||||
User sender = getSender();
|
||||
if (!sender.getId().equals(id)) {
|
||||
throw new RuntimeException("Ein Nutzer kann nur seine eigene E-Mail-Adresse ändern.");
|
||||
}
|
||||
// TODO: Add Email Notification for password change
|
||||
sender.setEmail(newEmail);
|
||||
userRepository.save(sender);
|
||||
}
|
||||
|
||||
public void changeVisibilityPrivacy(VisibilityPrivacy visibilityPrivacy) {
|
||||
User user = getSender();
|
||||
user.setVisibilityPrivacy(visibilityPrivacy);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user