From 8e4db3d25bbe8ab5c8f65eb78d3f65849a9f192b Mon Sep 17 00:00:00 2001 From: many2many <6168830@qq.com> Date: Fri, 22 Nov 2024 10:39:18 +0800 Subject: [PATCH] add register impl code --- .../lk/paopao/controller/AuthController.java | 30 +++++++++++++ .../main/java/com/lk/paopao/entity/User.java | 42 +++++++++++++++++++ .../lk/paopao/repository/UserRepository.java | 7 ++++ .../com/lk/paopao/service/AuthService.java | 17 ++++++++ 4 files changed, 96 insertions(+) create mode 100644 examples/chapter07/paopao/src/main/java/com/lk/paopao/controller/AuthController.java create mode 100644 examples/chapter07/paopao/src/main/java/com/lk/paopao/entity/User.java create mode 100644 examples/chapter07/paopao/src/main/java/com/lk/paopao/repository/UserRepository.java create mode 100644 examples/chapter07/paopao/src/main/java/com/lk/paopao/service/AuthService.java diff --git a/examples/chapter07/paopao/src/main/java/com/lk/paopao/controller/AuthController.java b/examples/chapter07/paopao/src/main/java/com/lk/paopao/controller/AuthController.java new file mode 100644 index 0000000..4e444ec --- /dev/null +++ b/examples/chapter07/paopao/src/main/java/com/lk/paopao/controller/AuthController.java @@ -0,0 +1,30 @@ +package com.lk.paopao.controller; + + +import com.lk.paopao.entity.User; +import com.lk.paopao.service.AuthService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Map; + +@RestController +@RequestMapping("/v1/auth") +public class AuthController { + + @Autowired + private AuthService authService; + + @PostMapping("/register") + public Map register(@RequestBody User req) { + User user = authService.register(req); + + Map data = Map.of("username",user.getUsername(),"id",user.getId()); + + return Map.of("code","0", "msg","success","data", data); + + } +} diff --git a/examples/chapter07/paopao/src/main/java/com/lk/paopao/entity/User.java b/examples/chapter07/paopao/src/main/java/com/lk/paopao/entity/User.java new file mode 100644 index 0000000..cd4a2b1 --- /dev/null +++ b/examples/chapter07/paopao/src/main/java/com/lk/paopao/entity/User.java @@ -0,0 +1,42 @@ +package com.lk.paopao.entity; + +import jakarta.persistence.*; + +@Entity +@Table(name = "users") +public class User { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(unique = true, nullable = false) + private String username; + + @Column(nullable = false) + private String password; + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } +} diff --git a/examples/chapter07/paopao/src/main/java/com/lk/paopao/repository/UserRepository.java b/examples/chapter07/paopao/src/main/java/com/lk/paopao/repository/UserRepository.java new file mode 100644 index 0000000..d23931e --- /dev/null +++ b/examples/chapter07/paopao/src/main/java/com/lk/paopao/repository/UserRepository.java @@ -0,0 +1,7 @@ +package com.lk.paopao.repository; + +import com.lk.paopao.entity.User; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface UserRepository extends JpaRepository { +} diff --git a/examples/chapter07/paopao/src/main/java/com/lk/paopao/service/AuthService.java b/examples/chapter07/paopao/src/main/java/com/lk/paopao/service/AuthService.java new file mode 100644 index 0000000..b89b4d0 --- /dev/null +++ b/examples/chapter07/paopao/src/main/java/com/lk/paopao/service/AuthService.java @@ -0,0 +1,17 @@ +package com.lk.paopao.service; + +import com.lk.paopao.entity.User; +import com.lk.paopao.repository.UserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class AuthService { + @Autowired + private UserRepository userRepository; + + // 注册 + public User register(User user){ + return userRepository.save(user); + } +}