data struct && register #23 #22

This commit is contained in:
whz 2024-05-11 09:49:22 +08:00
parent e4b61bc609
commit d245b41055
10 changed files with 248 additions and 1 deletions

View File

@ -2,8 +2,10 @@ package com.lk.paopao;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
@SpringBootApplication @SpringBootApplication
@EnableJpaAuditing
public class PaopaoApplication { public class PaopaoApplication {
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -0,0 +1,26 @@
package com.lk.paopao.controller;
import com.lk.paopao.dto.rest.request.RegisterRequest;
import com.lk.paopao.dto.rest.response.DataResult;
import com.lk.paopao.dto.rest.response.ResultUtil;
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;
@RestController
@RequestMapping("${app.version}/auth")
public class AuthController {
@Autowired
AuthService authService;
@PostMapping("/register")
public DataResult<User> register(@RequestBody RegisterRequest req) {
User saved = authService.register(req);
return ResultUtil.ok(saved);
}
}

View File

@ -0,0 +1,9 @@
package com.lk.paopao.dto.rest.request;
import lombok.Data;
@Data
public class RegisterRequest {
String username;
String password;
}

View File

@ -0,0 +1,16 @@
package com.lk.paopao.dto.rest.response;
import lombok.Getter;
import java.io.Serializable;
@Getter
public class DataResult<T> extends Result implements Serializable {
T data;
public DataResult(int code, T data, String message){
super(code, message);
this.data = data;
}
}

View File

@ -0,0 +1,15 @@
package com.lk.paopao.dto.rest.response;
import lombok.Getter;
import java.io.Serializable;
@Getter
public class Result implements Serializable {
int code;
String msg;
public Result(int code, String message) {
this.code = code;
this.msg = message;
}
}

View File

@ -0,0 +1,20 @@
package com.lk.paopao.dto.rest.response;
public class ResultUtil {
public static <T> DataResult<T> ok(T data) {
return new DataResult<T>(0, data, "SUCCESS");
}
public static Result ok() {
return new Result(0, "SUCCESS");
}
public static Result fail(String error) {
return new Result(500, error);
}
public static Result fail(int code, String error) {
return new Result(code, error);
}
}

View File

@ -0,0 +1,80 @@
package com.lk.paopao.entity;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.Comment;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
@Getter
@Setter
@Comment("用户")
@Entity
@Table(name = "p_user", indexes = {
@Index(name = "idx_user_phone", columnList = "phone")
}, uniqueConstraints = {
@UniqueConstraint(name = "idx_user_username", columnNames = {"username"})
})
@EntityListeners(AuditingEntityListener.class)
public class User {
@Id
@Comment("用户ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Comment("昵称")
@ColumnDefault("")
@Column(name = "nickname", nullable = false, length = 32)
private String nickname;
@Comment("用户名")
@ColumnDefault("")
@Column(name = "username", nullable = false, length = 32)
private String username;
@Comment("手机号")
// @ColumnDefault("")
@Column(name = "phone", nullable = true, length = 16)
private String phone;
@Comment("密码")
@ColumnDefault("")
@Column(name = "password", nullable = false, length = 32)
private String password;
@Comment("状态1正常2停用")
@Column(name = "status", nullable = false)
private Byte status = 1;
@Comment("用户头像")
@ColumnDefault("")
@Column(name = "avatar", nullable = false)
private String avatar;
@Comment("是否管理员")
@Column(name = "is_admin")
private Boolean isAdmin = false;
@Comment("创建时间")
@CreatedDate
@Column(name = "created_on", nullable = false)
private Long createdOn;
@Comment("修改时间")
@LastModifiedDate
@Column(name = "modified_on", nullable = false)
private Long modifiedOn;
@Comment("删除时间")
@ColumnDefault("0")
@Column(name = "deleted_on", nullable = true)
private Long deletedOn = 0L;
@Comment("是否删除 0 为未删除、1 为已删除")
@Column(name = "is_del")
private byte isDel = 0;
}

View File

@ -0,0 +1,10 @@
package com.lk.paopao.repository;
import com.lk.paopao.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByUsername(String username);
}

View File

@ -0,0 +1,42 @@
package com.lk.paopao.service;
import com.lk.paopao.dto.rest.request.RegisterRequest;
import com.lk.paopao.entity.User;
import com.lk.paopao.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class AuthService {
// @Autowired
// PasswordEncoder encoder; // 自动注入密码编码器
@Autowired
UserRepository userRepository; // 自动注入用户仓库
@Value("${app.default.head-icon}")
private String DEFAULT_HEAD_ICON; // 从配置文件中读取默认头像地址
/**
* 用户注册
* @param reg 注册请求对象包含用户名和密码
* @return 注册后的用户信息
*/
public User register(RegisterRequest reg) {
// 检查用户是否已存在
if(userRepository.findByUsername(reg.getUsername()).isPresent()){
return null;
}
// 创建用户对象并设置信息
User user = new User();
// user.setPassword(encoder.encode(reg.getPassword())); // 编码密码
user.setAvatar(DEFAULT_HEAD_ICON); // 设置默认头像
user.setNickname(reg.getUsername()); // 设置昵称为用户名
user.setUsername(reg.getUsername()); // 设置用户名
user.setPassword(reg.getPassword());
return userRepository.save(user); // 保存用户到数据库
}
}

View File

@ -3,5 +3,32 @@ springdoc:
path: /api-docs path: /api-docs
spring: spring:
datasource:
url: jdbc:h2:file:./paopao.h2
driverClassName: org.h2.Driver
username: root
password: root
# initialization-mode: always
h2:
console: # 开启console 访问 默认false
enabled: true
settings:
trace: true # 开启h2 console 跟踪 方便调试 默认 false
web-allow-others: true # 允许console 远程访问 默认false
path: /h2 # h2 访问路径上下文
jpa: jpa:
show-sql: true
open-in-view: false open-in-view: false
defer-datasource-initialization: true
database-platform: org.hibernate.dialect.H2Dialect
hibernate:
ddl-auto: create-drop # update create-drop
properties:
hibernate:
format_sql: true
dialect: org.hibernate.dialect.H2Dialect
app:
version: v1
default:
head-icon: https://assets.paopao.info/public/avatar/default/joshua.png