feat:更新用户接口

This commit is contained in:
2025-06-28 15:51:04 +08:00
parent eadf548a94
commit 4f42574a02
8 changed files with 140 additions and 51 deletions

View File

@@ -9,6 +9,7 @@ import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@RestController
@@ -21,7 +22,21 @@ public class UserController {
@Operation(summary = "注册用户")
@PostMapping("")
@SaIgnore
public Boolean createSession(@JsonView(WriteView.class) @RequestBody UserDto userDto) {
return userService.register(userDto);
public Boolean registerUser(@RequestParam("username") String username,
@RequestParam("password") String password) {
return userService.register(username, password);
}
@Operation(summary = "查询用户")
@GetMapping("/{id}")
public @JsonView(ReadView.class) UserDto queryUser(@PathVariable("id") Long id) {
return userService.queryUser(id);
}
@Operation(summary = "更新用户")
@PutMapping("/{id}")
public Boolean updateUser(@PathVariable("id") Long id,
@JsonView(WriteView.class) @Validated @RequestBody UserDto userDto) {
return userService.updateUser(id, userDto);
}
}

View File

@@ -22,52 +22,34 @@ public class UserDto {
@NotBlank(message = "用户名称不能为空")
private String username;
@Schema(description = "密码")
@NotBlank(message = "密码不能为空")
@JsonView(WriteView.class)
private String password;
@Schema(description = "性别")
private Integer gender;
// @Schema(description = "性别")
// @JsonView(ReadView.class)
// private Integer gender;
//
// @Schema(description = "身份证号")
// @JsonView(ReadView.class)
// private String idNumber;
//
// @Schema(description = "手机号码")
// @JsonView(ReadView.class)
// private String phoneNumber;
//
// @Schema(description = "电子邮件")
// @JsonView(ReadView.class)
// private String email;
//
// @Schema(description = "生日")
// @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
// private Date birthDate;
@Schema(description = "手机号码")
private String phoneNumber;
@Schema(description = "电子邮件")
private String email;
@Schema(description = "生日")
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
private Date birthDate;
@Schema(description = "头像")
@JsonView(ReadView.class)
private String avatar;
// @Schema(description = "地区")
// @JsonView(ReadView.class)
// private List<String> area;
//
// @Schema(description = "地址")
// @JsonView(ReadView.class)
// private String address;
//
// @Schema(description = "职业")
// @JsonView(ReadView.class)
// private String job;
//
// @Schema(description = "标签")
// @JsonView(ReadView.class)
// private List<String> tags;
//
// @Schema(description = "描述")
// @JsonView(ReadView.class)
// private String description;
@Schema(description = "地区")
private List<String> area;
@Schema(description = "地址")
private String address;
@Schema(description = "职业")
private String job;
@Schema(description = "标签")
private List<String> tags;
@Schema(description = "描述")
private String description;
}

View File

@@ -2,10 +2,14 @@ package com.cxx.food.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.cxx.framework.data.AbstractEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.util.Date;
import java.util.List;
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("user")
@@ -13,9 +17,36 @@ public class User extends AbstractEntity {
@TableField("username")
private String username;
@TableField("gender")
private Integer gender;
@TableField("password")
private String password;
@TableField("phone_number")
private String phoneNumber;
@TableField("email")
private String email;
@TableField("birth_date")
private Date birthDate;
@TableField("avatar")
private String avatar;
@TableField(value = "area", typeHandler = JacksonTypeHandler.class)
private List<String> area;
@TableField("address")
private String address;
@TableField("job")
private String job;
@TableField(value = "tags", typeHandler = JacksonTypeHandler.class)
private List<String> tags;
@TableField("description")
private String description;
}

View File

@@ -1,9 +1,13 @@
package com.cxx.food.service;
import com.cxx.food.dto.SessionDto;
import com.cxx.food.dto.UserDto;
public interface UserService {
Boolean register(UserDto userDto);
Boolean register(String username, String password);
UserDto queryUser(Long id);
Boolean updateUser(Long id, UserDto userDto);
}

View File

@@ -19,16 +19,46 @@ public class UserServiceImpl implements UserService {
private UserMapper userMapper;
@Override
public Boolean register(UserDto userDto) {
public Boolean register(String username, String password) {
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
if (userMapper.exists(queryWrapper.eq(User::getUsername, userDto.getUsername()))) {
if (userMapper.exists(queryWrapper.eq(User::getUsername, username))) {
throw new CustomException("该用户已注册");
}
User user = new User();
BeanUtils.copyProperties(userDto, user);
user.setPassword(BCrypt.hashpw(userDto.getPassword(), BCrypt.gensalt()));
user.setUsername(username);
user.setPassword(BCrypt.hashpw(password, BCrypt.gensalt()));
return userMapper.insert(user) == 1;
}
@Override
public UserDto queryUser(Long id) {
User user = userMapper.selectById(id);
if (user == null) {
throw new CustomException("未查询到该用户");
}
UserDto userDto = new UserDto();
BeanUtils.copyProperties(user, userDto);
return userDto;
}
@Override
public Boolean updateUser(Long id, UserDto userDto) {
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
if (!userMapper.exists(queryWrapper.eq(User::getId, id))) {
throw new CustomException("该用户不存在");
}
queryWrapper.clear();
if (userMapper.exists(queryWrapper.eq(User::getUsername, userDto.getUsername()).ne(User::getId, id))) {
throw new CustomException("该用户已经存在");
}
User user = new User();
BeanUtils.copyProperties(userDto, user);
user.setId(id);
return userMapper.updateById(user) == 1;
}
}