-
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.
Finish the example for creating a user
- Loading branch information
1 parent
367eca5
commit c9607b9
Showing
12 changed files
with
141 additions
and
72 deletions.
There are no files selected for viewing
23 changes: 0 additions & 23 deletions
23
src/main/java/edu/cmipt/gcs/controller/SwaggerTmpController.java
This file was deleted.
Oops, something went wrong.
50 changes: 50 additions & 0 deletions
50
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package edu.cmipt.gcs.controller; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
|
||
import edu.cmipt.gcs.pojo.user.UserDTO; | ||
import edu.cmipt.gcs.pojo.user.UserPO; | ||
import edu.cmipt.gcs.service.UserService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
|
||
@Controller | ||
@RequestMapping("/user") | ||
@Tag(name = "User", description = "User Related APIs") | ||
public class UserController { | ||
@Autowired | ||
private UserService userService; | ||
|
||
@PostMapping | ||
@Operation( | ||
summary = "Create a new user", | ||
description = "Create a new user with the given information", | ||
tags = { "User", "Post Method" } | ||
) | ||
@ApiResponses({ | ||
@ApiResponse(responseCode = "200", description = "User created successfully"), | ||
@ApiResponse(responseCode = "400", description = "User creation failed") | ||
}) | ||
public ResponseEntity<Void> createUser(@RequestBody UserDTO user) { | ||
if (user == null || | ||
user.getUsername() == null || | ||
user.getEmail() == null || | ||
user.getUserPassword() == null) { | ||
return ResponseEntity.badRequest().build(); | ||
} | ||
// there may be some check before.... | ||
boolean res = userService.save(new UserPO(user)); | ||
if (res) { | ||
return ResponseEntity.ok().build(); | ||
} else { | ||
return ResponseEntity.badRequest().build(); | ||
} | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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,17 @@ | ||
package edu.cmipt.gcs.pojo.user; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.Data; | ||
|
||
@Data | ||
@Schema(description = "User Data Transfer Object") | ||
public class UserDTO { | ||
@Schema(accessMode = Schema.AccessMode.READ_ONLY, description = "User ID") | ||
private Long id; | ||
@Schema(description = "Username", requiredMode = Schema.RequiredMode.REQUIRED, example = "admin") | ||
private String username; | ||
@Schema(description = "Email", requiredMode = Schema.RequiredMode.REQUIRED, example = "[email protected]") | ||
private String email; | ||
@Schema(description = "User Password (Unencrypted)", requiredMode = Schema.RequiredMode.REQUIRED, example = "admin") | ||
private String userPassword; | ||
} |
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,36 @@ | ||
package edu.cmipt.gcs.pojo.user; | ||
|
||
import com.baomidou.mybatisplus.annotation.TableLogic; | ||
import com.baomidou.mybatisplus.annotation.TableName; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import java.security.MessageDigest; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@TableName("t_user") | ||
public class UserPO { | ||
private Long id; | ||
private String username; | ||
private String email; | ||
private String userPassword; | ||
private LocalDateTime gmtCreated; | ||
private LocalDateTime gmtUpdated; | ||
@TableLogic | ||
private LocalDateTime gmtDeleted; | ||
|
||
public UserPO(UserDTO userDTO) { | ||
this.id = userDTO.getId(); | ||
this.username = userDTO.getUsername(); | ||
this.email = userDTO.getEmail(); | ||
try { | ||
this.userPassword = new String( | ||
MessageDigest.getInstance("MD5").digest(userDTO.getUserPassword().getBytes())); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
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,8 @@ | ||
package edu.cmipt.gcs.service; | ||
|
||
import com.baomidou.mybatisplus.extension.service.IService; | ||
|
||
import edu.cmipt.gcs.pojo.user.UserPO; | ||
|
||
public interface UserService extends IService<UserPO> { | ||
} |
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,12 @@ | ||
package edu.cmipt.gcs.service; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | ||
|
||
import edu.cmipt.gcs.dao.UserMapper; | ||
import edu.cmipt.gcs.pojo.user.UserPO; | ||
|
||
@Service | ||
public class UserServiceImpl extends ServiceImpl<UserMapper, UserPO> implements UserService { | ||
} |
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
21 changes: 0 additions & 21 deletions
21
src/test/java/edu/cmipt/gcs/controller/SwaggerTmpControllerITest.java
This file was deleted.
Oops, something went wrong.
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