-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from CMIPT/kaiser-crud
Add function to get user info by name.
- Loading branch information
Showing
11 changed files
with
151 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/java/edu/cmipt/gcs/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,50 @@ | ||
package edu.cmipt.gcs.controller; | ||
|
||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | ||
|
||
import edu.cmipt.gcs.constant.ApiPathConstant; | ||
import edu.cmipt.gcs.enumeration.ErrorCodeEnum; | ||
import edu.cmipt.gcs.exception.GenericException; | ||
import edu.cmipt.gcs.pojo.error.ErrorVO; | ||
import edu.cmipt.gcs.pojo.user.UserPO; | ||
import edu.cmipt.gcs.pojo.user.UserVO; | ||
import edu.cmipt.gcs.service.UserService; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@Tag(name = "User", description = "User Related APIs") | ||
public class UserController { | ||
@Autowired private UserService userService; | ||
|
||
@GetMapping(ApiPathConstant.USER_GET_BY_NAME_API_PATH) | ||
@Operation( | ||
summary = "Get user by name", | ||
description = "Get user information by user name", | ||
tags = {"User", "Get Method"}) | ||
@ApiResponses({ | ||
@ApiResponse(responseCode = "200", description = "User information returned successfully"), | ||
@ApiResponse( | ||
responseCode = "404", | ||
description = "User not found", | ||
content = @Content(schema = @Schema(implementation = ErrorVO.class))) | ||
}) | ||
public UserVO getUserByName(@PathVariable("username") String username) { | ||
QueryWrapper<UserPO> wrapper = new QueryWrapper<UserPO>(); | ||
wrapper.eq("username", username); | ||
if (!userService.exists(wrapper)) { | ||
throw new GenericException(ErrorCodeEnum.USER_NOT_FOUND, username); | ||
} | ||
return new UserVO(userService.getOne(wrapper)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,13 @@ | ||
package edu.cmipt.gcs.pojo.user; | ||
|
||
import edu.cmipt.gcs.enumeration.TokenTypeEnum; | ||
import edu.cmipt.gcs.util.JwtUtil; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
|
||
@Schema(description = "User Value Object") | ||
public record UserVO( | ||
@Schema(description = "User ID") Long id, | ||
@Schema(description = "Username", example = "admin") String username, | ||
@Schema(description = "Email", example = "[email protected]") String email, | ||
@Schema(description = "Access Token") String accessToken, | ||
@Schema(description = "Refresh Token") String refreshToken) { | ||
@Schema(description = "Email", example = "[email protected]") String email) { | ||
public UserVO(UserPO userPO) { | ||
this( | ||
userPO.getId(), | ||
userPO.getUsername(), | ||
userPO.getEmail(), | ||
JwtUtil.generateToken(userPO.getId(), TokenTypeEnum.ACCESS_TOKEN), | ||
JwtUtil.generateToken(userPO.getId(), TokenTypeEnum.REFRESH_TOKEN)); | ||
this(userPO.getId(), userPO.getUsername(), userPO.getEmail()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package edu.cmipt.gcs.constant; | ||
|
||
import java.util.Date; | ||
|
||
public class TestConstant { | ||
public static String USERNAME = new Date().getTime() + ""; | ||
public static String USER_PASSWORD = "123456"; | ||
public static String EMAIL = USERNAME + "@cmipt.edu"; | ||
public static String ACCESS_TOKEN; | ||
public static String REFRESH_TOKEN; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters