Improved admin user rest API semantics
This commit is contained in:
parent
40cc234b83
commit
0037f07559
@ -3,6 +3,7 @@ package org.ros.chatto.controller;
|
|||||||
import java.security.Principal;
|
import java.security.Principal;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
|
|
||||||
@ -13,6 +14,7 @@ import org.ros.chatto.dto.ChatMessageDTO;
|
|||||||
import org.ros.chatto.dto.ReencryptionDTO;
|
import org.ros.chatto.dto.ReencryptionDTO;
|
||||||
import org.ros.chatto.service.AdminService;
|
import org.ros.chatto.service.AdminService;
|
||||||
import org.ros.chatto.service.ChatService;
|
import org.ros.chatto.service.ChatService;
|
||||||
|
import org.ros.chatto.service.UserDTOSpec;
|
||||||
import org.ros.chatto.service.UserService;
|
import org.ros.chatto.service.UserService;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
@ -22,6 +24,7 @@ import org.springframework.web.bind.annotation.PathVariable;
|
|||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
@ -62,14 +65,21 @@ public class AdminRESTController {
|
|||||||
return chatMessageDTOs;
|
return chatMessageDTOs;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/regular-users")
|
|
||||||
public List<String> getAllRegularUsers() {
|
|
||||||
return userService.getAllRegularUsers();
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/get/users")
|
@GetMapping("/get/users")
|
||||||
public List<AdminUserDTO> getAllOtherUsers(Principal principal) {
|
public List<AdminUserDTO> getUsers(Principal principal,
|
||||||
return adminService.getOtherUsers(principal.getName());
|
@RequestParam Optional<String> select) {
|
||||||
|
if (select.isPresent()) {
|
||||||
|
switch (select.get()) {
|
||||||
|
case "all":
|
||||||
|
return adminService.getUsers("", UserDTOSpec.ALL_USERS);
|
||||||
|
|
||||||
|
case "regular":
|
||||||
|
return adminService.getUsers("", UserDTOSpec.REGULAR_USERS);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return adminService.getUsers(principal.getName(),
|
||||||
|
UserDTOSpec.OTHER_USERS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/delete/users/{userName}")
|
@DeleteMapping("/delete/users/{userName}")
|
||||||
|
@ -0,0 +1,25 @@
|
|||||||
|
package org.ros.chatto.repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.ros.chatto.dto.AdminUserDTO;
|
||||||
|
import org.ros.chatto.model.ChatUser;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface AdminUserRepository extends JpaRepository<ChatUser, Long> {
|
||||||
|
|
||||||
|
@Query("select new org.ros.chatto.dto.AdminUserDTO(u.userID, u.userName, ur.role.name, u.joinDate )"
|
||||||
|
+ " from ChatUser u join u.userRoles ur where u.userName != ?1")
|
||||||
|
public List<AdminUserDTO> getOtherUsers(String principal);
|
||||||
|
|
||||||
|
@Query("select new org.ros.chatto.dto.AdminUserDTO(u.userID, u.userName, ur.role.name, u.joinDate )"
|
||||||
|
+ " from ChatUser u join u.userRoles ur ")
|
||||||
|
public List<AdminUserDTO> getAllUsers();
|
||||||
|
|
||||||
|
@Query("select new org.ros.chatto.dto.AdminUserDTO(u.userID, u.userName, ur.role.name, u.joinDate )"
|
||||||
|
+ " from ChatUser u join u.userRoles ur where ur.role.roleID = 2")
|
||||||
|
public List<AdminUserDTO> getRegularUsers();
|
||||||
|
}
|
@ -19,8 +19,4 @@ public interface UserRepository extends JpaRepository<ChatUser, Long> {
|
|||||||
|
|
||||||
@Query("select count(u) from ChatUser u")
|
@Query("select count(u) from ChatUser u")
|
||||||
public Long totalUsers();
|
public Long totalUsers();
|
||||||
|
|
||||||
@Query("select new org.ros.chatto.dto.AdminUserDTO(u.userID, u.userName, ur.role.name, u.joinDate )"
|
|
||||||
+ " from ChatUser u join u.userRoles ur ")
|
|
||||||
public List<AdminUserDTO> getOtherUsers(String principal);
|
|
||||||
}
|
}
|
||||||
|
@ -7,8 +7,10 @@ import com.spencerwi.either.Result;
|
|||||||
|
|
||||||
import org.ros.chatto.dto.AdminUserDTO;
|
import org.ros.chatto.dto.AdminUserDTO;
|
||||||
import org.ros.chatto.model.ChatUser;
|
import org.ros.chatto.model.ChatUser;
|
||||||
|
import org.ros.chatto.repository.AdminUserRepository;
|
||||||
import org.ros.chatto.repository.UserRepository;
|
import org.ros.chatto.repository.UserRepository;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@ -16,11 +18,22 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
@Transactional
|
||||||
public class AdminService {
|
public class AdminService {
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
|
private final AdminUserRepository adminUserRepository;
|
||||||
|
|
||||||
public List<AdminUserDTO> getOtherUsers(String principal) {
|
public List<AdminUserDTO> getUsers(String principal, UserDTOSpec spec) {
|
||||||
return userRepository.getOtherUsers(principal);
|
switch (spec) {
|
||||||
|
case OTHER_USERS:
|
||||||
|
return adminUserRepository.getOtherUsers(principal);
|
||||||
|
case ALL_USERS:
|
||||||
|
return adminUserRepository.getAllUsers();
|
||||||
|
case REGULAR_USERS:
|
||||||
|
return adminUserRepository.getRegularUsers();
|
||||||
|
default:
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Result<Void> deleteUser(String userName) {
|
public Result<Void> deleteUser(String userName) {
|
||||||
|
5
src/main/java/org/ros/chatto/service/UserDTOSpec.java
Normal file
5
src/main/java/org/ros/chatto/service/UserDTOSpec.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package org.ros.chatto.service;
|
||||||
|
|
||||||
|
public enum UserDTOSpec {
|
||||||
|
ALL_USERS, OTHER_USERS, REGULAR_USERS
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user