A self hosted chat application with end-to-end encrypted messaging.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.4 KiB

  1. package org.ros.chatto.service;
  2. import java.util.List;
  3. import java.util.Optional;
  4. import com.spencerwi.either.Result;
  5. import org.ros.chatto.dto.AdminUserDTO;
  6. import org.ros.chatto.model.ChatUser;
  7. import org.ros.chatto.repository.AdminUserRepository;
  8. import org.ros.chatto.repository.UserRepository;
  9. import org.springframework.stereotype.Service;
  10. import org.springframework.transaction.annotation.Transactional;
  11. import lombok.RequiredArgsConstructor;
  12. import lombok.extern.slf4j.Slf4j;
  13. @Service
  14. @RequiredArgsConstructor
  15. @Slf4j
  16. @Transactional
  17. public class AdminService {
  18. private final UserRepository userRepository;
  19. private final AdminUserRepository adminUserRepository;
  20. public List<AdminUserDTO> getUsers(String principal, UserDTOSpec spec) {
  21. switch (spec) {
  22. case OTHER_USERS:
  23. return adminUserRepository.getOtherUsers(principal);
  24. case ALL_USERS:
  25. return adminUserRepository.getAllUsers();
  26. case REGULAR_USERS:
  27. return adminUserRepository.getRegularUsers();
  28. default:
  29. throw new UnsupportedOperationException();
  30. }
  31. }
  32. public Result<Void> deleteUser(String userName) {
  33. Optional<ChatUser> user = userRepository.findByUserName(userName);
  34. return Result.attempt(() -> user.get()).map((u) -> {
  35. userRepository.delete(u);
  36. return (Void) null;
  37. });
  38. }
  39. }