Skip to content

Commit

Permalink
Finish the example for creating a user
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaiser-Yang committed Aug 21, 2024
1 parent 367eca5 commit c9607b9
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 72 deletions.
23 changes: 0 additions & 23 deletions src/main/java/edu/cmipt/gcs/controller/SwaggerTmpController.java

This file was deleted.

50 changes: 50 additions & 0 deletions src/main/java/edu/cmipt/gcs/controller/UserController.java
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();
}
}
}
5 changes: 3 additions & 2 deletions src/main/java/edu/cmipt/gcs/dao/UserMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import edu.cmipt.gcs.pojo.UserPO;
import edu.cmipt.gcs.pojo.user.UserPO;

public interface UserMapper extends BaseMapper<UserPO> {}
public interface UserMapper extends BaseMapper<UserPO> {
}
16 changes: 0 additions & 16 deletions src/main/java/edu/cmipt/gcs/pojo/UserPO.java

This file was deleted.

17 changes: 17 additions & 0 deletions src/main/java/edu/cmipt/gcs/pojo/user/UserDTO.java
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;
}
36 changes: 36 additions & 0 deletions src/main/java/edu/cmipt/gcs/pojo/user/UserPO.java
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();
}
}
}
8 changes: 8 additions & 0 deletions src/main/java/edu/cmipt/gcs/service/UserService.java
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> {
}
12 changes: 12 additions & 0 deletions src/main/java/edu/cmipt/gcs/service/UserServiceImpl.java
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 {
}
7 changes: 7 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,12 @@ spring:
reset-enable: false
allow: # empty means allow all

mybatis-plus:
global-config:
db-config:
logic-delete-field: gmt_deleted
logic-delete-value: now()
logic-not-delete-value: "null"

logging:
include-application-name: false

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

@SpringBootTest
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
public class SwaggerTmpControllerTest {
public class UserControllerTest {

@Autowired private MockMvc mvc;

@Test
public void testGetUser() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/api/users/1"))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("true")));
public void testCreateUser() throws Exception {
mvc.perform(MockMvcRequestBuilders.post("/user").content("{\"name\": \"test\"}"))
.andExpect(status().isBadRequest())
.andExpect(content().string(equalTo("")));
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package edu.cmipt.gcs.controller.dao;
package edu.cmipt.gcs.dao;

import com.baomidou.mybatisplus.core.toolkit.Assert;

import edu.cmipt.gcs.dao.UserMapper;
import edu.cmipt.gcs.pojo.UserPO;
import edu.cmipt.gcs.pojo.user.UserPO;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -20,7 +19,6 @@ public class UserMapperTest {
public void testSelect() {
System.out.println(("----- selectAll method test ------"));
List<UserPO> userList = userMapper.selectList(null);
Assert.isTrue(1 == userList.size(), "");
userList.forEach(System.out::println);
}
}

0 comments on commit c9607b9

Please sign in to comment.