Skip to content

Commit

Permalink
Update APIs for query user and add collaborator
Browse files Browse the repository at this point in the history
We update the API for querying user: we add a parameter to determine
getting the user's information by username, email or id. We also update
the API for adding collaborator: we add a parameter to determine the
collaborator's information by username, email or id.
  • Loading branch information
Kaiser-Yang committed Oct 22, 2024
1 parent 0ee82e0 commit 263e7b8
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 113 deletions.
8 changes: 1 addition & 7 deletions src/main/java/edu/cmipt/gcs/constant/ApiPathConstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,8 @@ public class ApiPathConstant {
REPOSITORY_API_PREFIX + "/repository-name";
public static final String REPOSITORY_PAGE_COLLABORATOR_API_PATH =
REPOSITORY_API_PREFIX + "/page/collaborator";
public static final String REPOSITORY_ADD_COLLABORATOR_API_PREFIX =
public static final String REPOSITORY_ADD_COLLABORATOR_API_PATH =
REPOSITORY_API_PREFIX + "/add-collaborator";
public static final String REPOSITORY_ADD_COLLABORATOR_BY_NAME_API_PATH =
REPOSITORY_ADD_COLLABORATOR_API_PREFIX + "/name";
public static final String REPOSITORY_ADD_COLLABORATOR_BY_EMAIL_API_PATH =
REPOSITORY_ADD_COLLABORATOR_API_PREFIX + "/email";
public static final String REPOSITORY_ADD_COLLABORATOR_BY_ID_API_PATH =
REPOSITORY_ADD_COLLABORATOR_API_PREFIX + "/id";
public static final String REPOSITORY_REMOVE_COLLABORATION_API_PATH =
REPOSITORY_API_PREFIX + "/remove-collaborator";

Expand Down
126 changes: 33 additions & 93 deletions src/main/java/edu/cmipt/gcs/controller/RepositoryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,9 @@ public void checkRepositoryNameValidity(
}
}

@PostMapping(ApiPathConstant.REPOSITORY_ADD_COLLABORATOR_BY_NAME_API_PATH)
@PostMapping(ApiPathConstant.REPOSITORY_ADD_COLLABORATOR_API_PATH)
@Operation(
summary = "Add a collaborator by names",
summary = "Add a collaborator",
description = "Add a collaborator to the repository",
tags = {"Repository", "Post Method"})
@Parameters({
Expand All @@ -251,51 +251,16 @@ public void checkRepositoryNameValidity(
in = ParameterIn.QUERY,
schema = @Schema(implementation = Long.class)),
@Parameter(
name = "collaboratorName",
description = "Collaborator's name",
required = true,
in = ParameterIn.QUERY,
schema = @Schema(implementation = String.class))
})
@ApiResponses({
@ApiResponse(responseCode = "200", description = "Collaborator added successfully"),
@ApiResponse(responseCode = "403", description = "Access denied"),
@ApiResponse(responseCode = "404", description = "Collaborator or repository not found")
})
public void addCollaboratorByName(
@RequestParam("repositoryId") Long repositoryId,
@RequestParam("collaboratorName") String collaboratorName,
@RequestHeader(HeaderParameter.ACCESS_TOKEN) String accessToken) {
QueryWrapper<UserPO> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("username", collaboratorName);
if (!userService.exists(queryWrapper)) {
throw new GenericException(ErrorCodeEnum.USER_NOT_FOUND, collaboratorName);
}
Long userId = userService.getOne(queryWrapper).getId();
addCollaboratorById(repositoryId, userId, accessToken);
}

@PostMapping(ApiPathConstant.REPOSITORY_ADD_COLLABORATOR_BY_EMAIL_API_PATH)
@Operation(
summary = "Add a collaborator by email",
description = "Add a collaborator to the repository",
tags = {"Repository", "Post Method"})
@Parameters({
@Parameter(
name = HeaderParameter.ACCESS_TOKEN,
description = "Access token",
required = true,
in = ParameterIn.HEADER,
schema = @Schema(implementation = String.class)),
@Parameter(
name = "repositoryId",
description = "Repository ID",
name = "collaborator",
description = "Collaborator's Information",
example = "admin",
required = true,
in = ParameterIn.QUERY,
schema = @Schema(implementation = Long.class)),
@Parameter(
name = "collaboratorEmail",
description = "Collaborator's email",
name = "collaboratorType",
description = "Collaborator's Type. The value can be 'id', 'username' or 'email'",
example = "username",
required = true,
in = ParameterIn.QUERY,
schema = @Schema(implementation = String.class))
Expand All @@ -305,56 +270,31 @@ public void addCollaboratorByName(
@ApiResponse(responseCode = "403", description = "Access denied"),
@ApiResponse(responseCode = "404", description = "Collaborator or repository not found")
})
public void addCollaboratorByEmail(
public void addCollaborator(
@RequestParam("repositoryId") Long repositoryId,
@RequestParam("collaboratorEmail") String collaboratorEmail,
@RequestParam("collaborator") String collaborator,
@RequestParam("collaboratorType") String collaboratorType,
@RequestHeader(HeaderParameter.ACCESS_TOKEN) String accessToken) {
QueryWrapper<UserPO> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("email", collaboratorEmail);
if (!userService.exists(queryWrapper)) {
throw new GenericException(ErrorCodeEnum.USER_NOT_FOUND, collaboratorEmail);
if (!collaboratorType.equals("id")
&& !collaboratorType.equals("username")
&& !collaboratorType.equals("email")) {
throw new GenericException(ErrorCodeEnum.MESSAGE_CONVERSION_ERROR);
}
Long userId = userService.getOne(queryWrapper).getId();
addCollaboratorById(repositoryId, userId, accessToken);
}

@PostMapping(ApiPathConstant.REPOSITORY_ADD_COLLABORATOR_BY_ID_API_PATH)
@Operation(
summary = "Add a collaborator by id",
description = "Add a collaborator to the repository",
tags = {"Repository", "Post Method"})
@Parameters({
@Parameter(
name = HeaderParameter.ACCESS_TOKEN,
description = "Access token",
required = true,
in = ParameterIn.HEADER,
schema = @Schema(implementation = String.class)),
@Parameter(
name = "repositoryId",
description = "Repository ID",
required = true,
in = ParameterIn.QUERY,
schema = @Schema(implementation = Long.class)),
@Parameter(
name = "collaboratorId",
description = "Collaborator's ID",
required = true,
in = ParameterIn.QUERY,
schema = @Schema(implementation = Long.class))
})
@ApiResponses({
@ApiResponse(responseCode = "200", description = "Collaborator added successfully"),
@ApiResponse(responseCode = "403", description = "Access denied"),
@ApiResponse(responseCode = "404", description = "Collaborator or repository not found")
})
public void addCollaboratorById(
@RequestParam("repositoryId") Long repositoryId,
@RequestParam("collaboratorId") Long collaboratorId,
@RequestHeader(HeaderParameter.ACCESS_TOKEN) String accessToken) {
if (userService.getById(collaboratorId) == null) {
throw new GenericException(ErrorCodeEnum.USER_NOT_FOUND, collaboratorId);
QueryWrapper<UserPO> userQueryWrapper = new QueryWrapper<>();
if (collaboratorType.equals("id")) {
try {
userQueryWrapper.eq(collaboratorType, Long.valueOf(collaborator));
} catch (Exception e) {
throw new GenericException(ErrorCodeEnum.MESSAGE_CONVERSION_ERROR);
}
} else {
userQueryWrapper.eq(collaboratorType, collaborator);
}
UserPO user = userService.getOne(userQueryWrapper);
if (user == null) {
throw new GenericException(ErrorCodeEnum.USER_NOT_FOUND, collaborator);
}
Long collaboratorId = user.getId();
RepositoryPO repository = repositoryService.getById(repositoryId);
if (repository == null) {
throw new GenericException(ErrorCodeEnum.REPOSITORY_NOT_FOUND, repositoryId);
Expand All @@ -376,10 +316,10 @@ public void addCollaboratorById(
repositoryId);
throw new GenericException(ErrorCodeEnum.ILLOGICAL_OPERATION);
}
QueryWrapper<UserCollaborateRepositoryPO> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("collaborator_id", collaboratorId);
queryWrapper.eq("repository_id", repositoryId);
if (userCollaborateRepositoryService.exists(queryWrapper)) {
QueryWrapper<UserCollaborateRepositoryPO> collaborationQueryWrapper = new QueryWrapper<>();
collaborationQueryWrapper.eq("collaborator_id", collaboratorId);
collaborationQueryWrapper.eq("repository_id", repositoryId);
if (userCollaborateRepositoryService.exists(collaborationQueryWrapper)) {
logger.error(
"Collaborator[{}] already exists in repository[{}]",
collaboratorId,
Expand Down
32 changes: 25 additions & 7 deletions src/main/java/edu/cmipt/gcs/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public class UserController {

@GetMapping(ApiPathConstant.USER_GET_USER_API_PATH)
@Operation(
summary = "Get user by name",
description = "Get user information by user name",
summary = "Get a user",
description = "Get a user's information",
tags = {"User", "Get Method"})
@Parameters({
@Parameter(
Expand All @@ -68,11 +68,18 @@ public class UserController {
in = ParameterIn.HEADER,
schema = @Schema(implementation = String.class)),
@Parameter(
name = "username",
description = "User name",
name = "user",
description = "User's Information",
example = "admin",
required = true,
in = ParameterIn.QUERY,
schema = @Schema(implementation = String.class)),
@Parameter(
name = "userType",
description = "User's Type. The value can be 'id', 'username' or 'email'",
example = "username",
required = true,
in = ParameterIn.QUERY,
schema = @Schema(implementation = String.class))
})
@ApiResponses({
Expand All @@ -82,11 +89,22 @@ public class UserController {
description = "User not found",
content = @Content(schema = @Schema(implementation = ErrorVO.class)))
})
public UserVO getUser(@RequestParam("username") String username) {
public UserVO getUser(@RequestParam("user") String user, @RequestParam("userType") String userType) {
if (!userType.equals("id") && !userType.equals("username") && !userType.equals("email")) {
throw new GenericException(ErrorCodeEnum.MESSAGE_CONVERSION_ERROR);
}
QueryWrapper<UserPO> wrapper = new QueryWrapper<UserPO>();
wrapper.eq("username", username);
if (userType.equals("id")) {
try {
wrapper.eq(userType, Long.valueOf(user));
} catch (Exception e) {
throw new GenericException(ErrorCodeEnum.MESSAGE_CONVERSION_ERROR);
}
} else {
wrapper.eq(userType, user);
}
if (!userService.exists(wrapper)) {
throw new GenericException(ErrorCodeEnum.USER_NOT_FOUND, username);
throw new GenericException(ErrorCodeEnum.USER_NOT_FOUND, user);
}
return new UserVO(userService.getOne(wrapper));
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/edu/cmipt/gcs/filter/JwtFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ private void authorize(HttpServletRequest request, String accessToken, String re
// this will be checked in controller
// because we must query the database to get the user id of the repository
} else if (request.getRequestURI()
.startsWith(ApiPathConstant.REPOSITORY_ADD_COLLABORATOR_API_PREFIX)) {
.equals(ApiPathConstant.REPOSITORY_ADD_COLLABORATOR_API_PATH)) {
// this will be checked in controller
// because we must query the database to get the user id of the repository
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,13 @@ public void testUpdateRepositoryValid() throws Exception {

@Test
@Order(Ordered.HIGHEST_PRECEDENCE + 2)
public void testAddCollaboratorByIdValid() throws Exception {
public void testAddCollaboratorValid() throws Exception {
mvc.perform(
post(ApiPathConstant.REPOSITORY_ADD_COLLABORATOR_BY_ID_API_PATH)
post(ApiPathConstant.REPOSITORY_ADD_COLLABORATOR_API_PATH)
.header(HeaderParameter.ACCESS_TOKEN, TestConstant.ACCESS_TOKEN)
.param("repositoryId", TestConstant.REPOSITORY_ID)
.param("collaboratorId", TestConstant.OTHER_ID))
.param("collaborator", TestConstant.OTHER_ID)
.param("collaboratorType", "id"))
.andExpect(status().isOk());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public void testGetUserValid() throws Exception {
mvc.perform(
get(ApiPathConstant.USER_GET_USER_API_PATH)
.header(HeaderParameter.ACCESS_TOKEN, TestConstant.ACCESS_TOKEN)
.param("username", TestConstant.USERNAME))
.param("user", TestConstant.USERNAME)
.param("userType", "username"))
.andExpectAll(
status().isOk(),
jsonPath("$.username", is(TestConstant.USERNAME)),
Expand All @@ -58,7 +59,8 @@ public void testGetUserInvalid() throws Exception {
mvc.perform(
get(ApiPathConstant.USER_GET_USER_API_PATH)
.header(HeaderParameter.ACCESS_TOKEN, TestConstant.ACCESS_TOKEN)
.param("username", invalidUsername))
.param("user", invalidUsername)
.param("userType", "username"))
.andExpectAll(
status().isNotFound(),
content()
Expand Down

0 comments on commit 263e7b8

Please sign in to comment.