add register impl code

This commit is contained in:
many2many 2024-11-22 10:39:18 +08:00
parent 3b2df2be35
commit 8e4db3d25b
4 changed files with 96 additions and 0 deletions

View File

@ -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<String, Object> register(@RequestBody User req) {
User user = authService.register(req);
Map<String,Object> data = Map.of("username",user.getUsername(),"id",user.getId());
return Map.of("code","0", "msg","success","data", data);
}
}

View File

@ -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;
}
}

View File

@ -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<User, Long> {
}

View File

@ -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);
}
}