基础信息管理
学习目标
在这里,你将系统学习了解基础信息管理
的相关内容
我们将以最简单直接
的方式为您呈现内容!
# 🍜 用户信息管理
用户注册:
在
UserService
接口中,我们定义了一个名为userRegister
的方法,用于处理用户的注册操作
。
/**
* 用户注册
*
* @param userAccount 用户账户
* @param userPassword 用户密码
* @param checkPassword 校验密码
* @return 新用户 id
*/
long userRegister(String userAccount, String userPassword, String checkPassword);
- 在
UserServiceImpl
类中,具体实现 UserService 接口的userRegister 方法
,以构建用户注册的业务逻辑。此方法将涵盖用户账户
、密码
及校验密码
的验证流程,并确保新用户能够成功注册并获得相应的用户 id。
/**
* 盐值,混淆密码
*/
private static final String SALT = "memory";
@Override
public long userRegister(String userAccount, String userPassword, String checkPassword) {
// 1. 校验
if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword)) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数为空");
}
if (userAccount.length() < 4) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户账号过短");
}
if (userPassword.length() < 8 || checkPassword.length() < 8) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户密码过短");
}
// 密码和校验密码相同
if (!userPassword.equals(checkPassword)) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "两次输入的密码不一致");
}
synchronized (userAccount.intern()) {
// 账户不能重复
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("userAccount", userAccount);
long count = this.baseMapper.selectCount(queryWrapper);
if (count > 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "账号重复");
}
// 2. 加密
String encryptPassword = DigestUtils.md5DigestAsHex((SALT + userPassword).getBytes());
// 3. 插入数据
User user = new User();
user.setUserAccount(userAccount);
user.setUserPassword(encryptPassword);
boolean saveResult = this.save(user);
if (!saveResult) {
throw new BusinessException(ErrorCode.SYSTEM_ERROR, "注册失败,数据库错误");
}
return user.getId();
}
}
- 在
UserController
下实现了用户注册功能,通过接收用户注册请求参数,进行参数校验,调用userService.userRegister 方法
完成注册操作,并返回用户 id。
@Resource
private UserService userService;
/**
* 用户注册
*
* @param userRegisterRequest 用户注册请求参数
* @return 用户 id
*/
@PostMapping("/register")
public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
// Controller 层对参数的校验
if (userRegisterRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
String userAccount = userRegisterRequest.getUserAccount();
String userPassword = userRegisterRequest.getUserPassword();
String checkPassword = userRegisterRequest.getCheckPassword();
// 校验 userAccount、userPassword、checkPassword 不能为空
if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword)) {
return null;
}
long result = userService.userRegister(userAccount, userPassword, checkPassword);
return ResultUtils.success(result);
}
- 用户登录:
/**
* 用户登录
*
* @param userLoginRequest 用户登录请求参数
* @param request request
* @return 登录用户信息
*/
@PostMapping("/login")
public BaseResponse<LoginUserVO> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
// Controller 层对参数的校验
if (userLoginRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
String userAccount = userLoginRequest.getUserAccount();
String userPassword = userLoginRequest.getUserPassword();
if (StringUtils.isAnyBlank(userAccount, userPassword)) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
LoginUserVO loginUserVO = userService.userLogin(userAccount, userPassword, request);
return ResultUtils.success(loginUserVO);
}
- 获取用户列表(管理员):
/**
* 分页获取用户列表
*
* @param userQueryRequest
* @param request
* @return
*/
@PostMapping("/list/page/vo")
public BaseResponse<Page<UserVO>> listUserVOByPage(@RequestBody UserQueryRequest userQueryRequest,
HttpServletRequest request) {
if (userQueryRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
long current = userQueryRequest.getCurrent();
long size = userQueryRequest.getPageSize();
// 限制爬虫
ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
Page<User> userPage = userService.page(new Page<>(current, size),
userService.getQueryWrapper(userQueryRequest));
Page<UserVO> userVOPage = new Page<>(current, size, userPage.getTotal());
List<UserVO> userVO = userService.getUserVO(userPage.getRecords());
userVOPage.setRecords(userVO);
return ResultUtils.success(userVOPage);
}
- 创建用户(管理员):
/**
* 创建用户
*
* @param userAddRequest
* @param request
* @return
*/
@PostMapping("/add")
@AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
public BaseResponse<Long> addUser(@RequestBody UserAddRequest userAddRequest, HttpServletRequest request) {
if (userAddRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
User user = new User();
BeanUtils.copyProperties(userAddRequest, user);
boolean result = userService.save(user);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
return ResultUtils.success(user.getId());
}
- 删除用户(管理员):
/**
* 删除用户
*
* @param deleteRequest
* @param request
* @return
*/
@PostMapping("/delete")
@AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
public BaseResponse<Boolean> deleteUser(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
if (deleteRequest == null || deleteRequest.getId() <= 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
boolean b = userService.removeById(deleteRequest.getId());
return ResultUtils.success(b);
}
- 更新用户(管理员):
/**
* 更新用户
*
* @param userUpdateRequest
* @param request
* @return
*/
@PostMapping("/update")
@AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
public BaseResponse<Boolean> updateUser(@RequestBody UserUpdateRequest userUpdateRequest,
HttpServletRequest request) {
if (userUpdateRequest == null || userUpdateRequest.getId() == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
User user = new User();
BeanUtils.copyProperties(userUpdateRequest, user);
boolean result = userService.updateById(user);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
return ResultUtils.success(true);
}
- 当前用户(管理员):
/**
* 获取当前登录用户
*
* @param request
* @return
*/
@GetMapping("/get/login")
public BaseResponse<LoginUserVO> getLoginUser(HttpServletRequest request) {
User user = userService.getLoginUser(request);
LoginUserVO loginUserVO = userService.getLoginUserVO(user);
return ResultUtils.success(loginUserVO);
}
- 用户注销:
/**
* 用户注销
*
* @param request
* @return
*/
@PostMapping("/logout")
public BaseResponse<Boolean> userLogout(HttpServletRequest request) {
if (request == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
boolean result = userService.userLogout(request);
return ResultUtils.success(result);
}
# 🍚 诗词信息管理
- 上传诗词记录:
/**
* 创建
*
* @param postAddRequest
* @param request
* @return
*/
@PostMapping("/add")
public BaseResponse<Long> addPost(@RequestBody PostAddRequest postAddRequest, HttpServletRequest request) {
if (postAddRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
Post post = new Post();
BeanUtils.copyProperties(postAddRequest, post);
List<String> tags = postAddRequest.getTags();
if (tags != null) {
post.setTags(GSON.toJson(tags));
}
postService.validPost(post, true);
post.setFavourNum(0);
post.setThumbNum(0);
boolean result = postService.save(post);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
long newPostId = post.getId();
return ResultUtils.success(newPostId);
}
- 删除诗词纪录:
/**
* 删除
*
* @param deleteRequest
* @param request
* @return
*/
@PostMapping("/delete")
public BaseResponse<Boolean> deletePost(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
if (deleteRequest == null || deleteRequest.getId() <= 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
long id = deleteRequest.getId();
// 判断是否存在
Post oldPost = postService.getById(id);
ThrowUtils.throwIf(oldPost == null, ErrorCode.NOT_FOUND_ERROR);
boolean b = postService.removeById(id);
return ResultUtils.success(b);
}
- 更新诗词纪录(管理员):
/**
* 更新(仅管理员)
*
* @param postUpdateRequest
* @return
*/
@PostMapping("/update")
public BaseResponse<Boolean> updatePost(@RequestBody PostUpdateRequest postUpdateRequest) {
if (postUpdateRequest == null || postUpdateRequest.getId() <= 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
Post post = new Post();
BeanUtils.copyProperties(postUpdateRequest, post);
List<String> tags = postUpdateRequest.getTags();
if (tags != null) {
post.setTags(GSON.toJson(tags));
}
// 参数校验
postService.validPost(post, false);
long id = postUpdateRequest.getId();
// 判断是否存在
Post oldPost = postService.getById(id);
ThrowUtils.throwIf(oldPost == null, ErrorCode.NOT_FOUND_ERROR);
boolean result = postService.updateById(post);
return ResultUtils.success(result);
}
- 分页获取诗词:
/**
* 分页获取诗词(封装类)
*
* @param postQueryRequest
* @param request
* @return
*/
@PostMapping("/list/page/vo")
public BaseResponse<Page<Post>> listPostVOByPage(@RequestBody PostQueryRequest postQueryRequest,
HttpServletRequest request) {
long pageNum = postQueryRequest.getPageNum();
long size = postQueryRequest.getPageSize();
// 限制爬虫
ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
Page<Post> PostPage = postService.page(new Page<>(pageNum, size),
postService.getQueryWrapper(postQueryRequest));
// Page<PostVO> postVOPage = postService.listPostVOByPage(postQueryRequest, request);
return ResultUtils.success(PostPage);
}
- 分页获取资源列表
/**
* 分页获取当前用户创建的资源列表
*
* @param postQueryRequest
* @param request
* @return
*/
@PostMapping("/my/list/page/vo")
public BaseResponse<Page<PostVO>> listMyPostVOByPage(@RequestBody PostQueryRequest postQueryRequest,
HttpServletRequest request) {
if (postQueryRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
long current = postQueryRequest.getPageNum();
long size = postQueryRequest.getPageSize();
// 限制爬虫
ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
Page<Post> postPage = postService.page(new Page<>(current, size),
postService.getQueryWrapper(postQueryRequest));
return ResultUtils.success(postService.getPostVOPage(postPage, request));
}
# 🦪 博文信息管理
- 上传博文记录:
/**
* 创建
*
* @param postAddRequest
* @param request
* @return
*/
@PostMapping("/add")
public BaseResponse<Long> addPost(@RequestBody PostAddRequest postAddRequest, HttpServletRequest request) {
if (postAddRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
Article article = new Article();
BeanUtils.copyProperties(postAddRequest, article);
List<String> tags = postAddRequest.getTags();
if (tags != null) {
article.setTags(GSON.toJson(tags));
}
boolean result = articleService.save(article);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
long newPostId = article.getId();
return ResultUtils.success(newPostId);
}
- 分页获取博文记录:
/**
* 分页获取博文(封装类)
*
* @param articleQueryRequest
* @param request
* @return
*/
@PostMapping("/list/page/vo")
public BaseResponse<Page<Article>> listPostVOByPage(@RequestBody ArticleQueryRequest articleQueryRequest,
HttpServletRequest request) {
long pageNum = articleQueryRequest.getPageNum();
long size = articleQueryRequest.getPageSize();
// 限制爬虫
ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
Page<Article> ArticlePage = articleService.page(new Page<>(pageNum, size));
// Page<PostVO> postVOPage = articleService.listPostVOByPage(postQueryRequest, request);
return ResultUtils.success(ArticlePage);
}
- 获取博文记录:
/**
* 获取博文
*
* @param articleDTO
* @param request
* @return
*/
@GetMapping("/get/VO")
public BaseResponse<ArticleVO> getArticleById(ArticleDTO articleDTO, HttpServletRequest request) {
// controller对参数的校验
ArticleVO articleVO = articleService.getArticle(articleDTO, request);
return ResultUtils.success(articleVO);
}
# 🥘 图片信息管理
- 分页获取图片列表:
/**
*
* 分页获取资源列表
* @param pictureQueryRequest
* @param request
* @return
* @throws IOException
*/
@PostMapping("/list/page/vo")
public BaseResponse<Page<Picture>> listPictureByPage(@RequestBody PictureQueryRequest pictureQueryRequest,
HttpServletRequest request) throws IOException {
//controller层对参数的校验
int currentPage = pictureQueryRequest.getCurrentPage();
int pageSize = pictureQueryRequest.getPageSize();
String searchText = pictureQueryRequest.getSearchText();
Page<Picture> picturePage = pictureService.listPictureVOByPage(searchText, pageSize, currentPage);
return ResultUtils.success(picturePage);
}
- 分页获取当前用户创建的资源列表
/**
* 分页获取当前用户创建的资源列表
*
* @param pictureQueryRequest
* @param request
* @return
*/
@PostMapping("/my/list/picture/vo")
public BaseResponse<Page<Picture>> listMyPostVOByPage(@RequestBody PictureQueryRequest pictureQueryRequest,
HttpServletRequest request) {
// 限制爬虫
Page<Picture> listMyPostVOByPage = pictureService.listMyPostVOByPage(pictureQueryRequest, request);
return ResultUtils.success(listMyPostVOByPage);
}