Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finish the example for creating a user #33

Merged
merged 5 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion database/diagram/gcs_back_end.drawio
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@
<mxRectangle width="30" height="30" as="alternateBounds" />
</mxGeometry>
</mxCell>
<mxCell id="6WugjG2xhQJgWSEuV__S-166" value="user_password character(128) NOT NULL" style="shape=partialRectangle;overflow=hidden;connectable=0;fillColor=none;align=left;strokeColor=inherit;top=0;left=0;bottom=0;right=0;spacingLeft=6;" parent="6WugjG2xhQJgWSEuV__S-164" vertex="1">
<mxCell id="6WugjG2xhQJgWSEuV__S-166" value="user_password character(32) NOT NULL" style="shape=partialRectangle;overflow=hidden;connectable=0;fillColor=none;align=left;strokeColor=inherit;top=0;left=0;bottom=0;right=0;spacingLeft=6;" parent="6WugjG2xhQJgWSEuV__S-164" vertex="1">
<mxGeometry x="30" width="510" height="30" as="geometry">
<mxRectangle width="510" height="30" as="alternateBounds" />
</mxGeometry>
Expand Down
Binary file modified database/diagram/gcs_back_end.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion database/table/t_user.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ CREATE TABLE public.t_user (
id bigint NOT NULL,
username character varying(50) NOT NULL,
email character varying(254) NOT NULL,
user_password character(128) NOT NULL,
user_password character(32) NOT NULL,
gmt_created timestamp without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
gmt_updated timestamp without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
gmt_deleted timestamp without time zone
Expand Down
23 changes: 0 additions & 23 deletions src/main/java/edu/cmipt/gcs/controller/SwaggerTmpController.java

This file was deleted.

49 changes: 49 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,49 @@
package edu.cmipt.gcs.controller;

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;

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;

@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();
azx-azx marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/edu/cmipt/gcs/dao/UserMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

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> {}
16 changes: 0 additions & 16 deletions src/main/java/edu/cmipt/gcs/pojo/UserPO.java

This file was deleted.

30 changes: 30 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,30 @@
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;
}
31 changes: 31 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,31 @@
package edu.cmipt.gcs.pojo.user;

import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;

import edu.cmipt.gcs.util.MD5Converter;

import lombok.AllArgsConstructor;
import lombok.Data;

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();
this.userPassword = MD5Converter.convertToMD5(userDTO.getUserPassword());
}
}
7 changes: 7 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,7 @@
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> {}
11 changes: 11 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,11 @@
package edu.cmipt.gcs.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

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

import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserPO> implements UserService {}
23 changes: 23 additions & 0 deletions src/main/java/edu/cmipt/gcs/util/MD5Converter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package edu.cmipt.gcs.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Converter {
public static String convertToMD5(String input) {
try {
byte[] hashBytes = MessageDigest.getInstance("MD5").digest(input.getBytes());
StringBuilder hexString = new StringBuilder();
for (byte b : hashBytes) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("MD5 algorithm not found", e);
}
}
}
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,6 @@
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 +17,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);
}
}
Loading