Compare commits
1 Commits
d53bc3966c
...
feature-Po
| Author | SHA1 | Date | |
|---|---|---|---|
| 1007a2a1e4 |
@@ -25,7 +25,9 @@ public class WebConfig implements WebMvcConfigurer {
|
||||
.addPathPatterns("/**")
|
||||
// 忽略的接口
|
||||
.excludePathPatterns(
|
||||
"/user/login"
|
||||
"/user/login",
|
||||
"/user/userRegister"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,12 +2,15 @@ package com.bao.dating.controller;
|
||||
|
||||
import com.bao.dating.common.Result;
|
||||
import com.bao.dating.common.ResultCode;
|
||||
import com.bao.dating.pojo.entity.Post;
|
||||
import com.bao.dating.pojo.entity.PostFavorite;
|
||||
import com.bao.dating.pojo.entity.User;
|
||||
import com.bao.dating.service.PostFavoriteService;
|
||||
import com.bao.dating.service.PostService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@@ -31,4 +34,11 @@ public class PostFavoriteController {
|
||||
Long userId = user.getUserId();
|
||||
return postFavoriteService.deletePostFavorite(userId, postId);
|
||||
}
|
||||
@GetMapping("/{user_id}/favorites")
|
||||
public Result<List<Post>> getFavorites(@PathVariable("user_id")Long postId){
|
||||
if (postId == null){
|
||||
return Result.error(ResultCode.PARAM_ERROR);
|
||||
}
|
||||
return postFavoriteService.getAllFavoritePost(postId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,4 +24,25 @@ public class UserController {
|
||||
UserLoginVO userloginVO = userService.userLogin(userLoginDTO);
|
||||
return Result.success(ResultCode.SUCCESS, "登录成功", userloginVO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户注册
|
||||
* @param userAccount 用户名称
|
||||
* @param userPassword 用户密码
|
||||
* @return 用户id
|
||||
*/
|
||||
@PostMapping("/userRegister")
|
||||
public Result userRegister(String userAccount, String userPassword){
|
||||
long result = userService.userRegister(userAccount, userPassword);
|
||||
if (result == -1){
|
||||
return Result.error(ResultCode.SYSTEM_ERROR);
|
||||
}
|
||||
if (result==-2){
|
||||
return Result.error(ResultCode.PARAM_ERROR);
|
||||
}
|
||||
if (result == -3){
|
||||
return Result.error(ResultCode.FAIL,"用户名称相同");
|
||||
}
|
||||
return Result.success(ResultCode.SUCCESS,"注册成功",result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.bao.dating.mapper;
|
||||
|
||||
import com.bao.dating.pojo.entity.Post;
|
||||
import com.bao.dating.pojo.entity.PostFavorite;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
@@ -12,4 +13,5 @@ public interface PostFavoriteMapper {
|
||||
List<Long> selectUserIDByPostID(@Param("postId") Long postId);
|
||||
int addPostFavorite(PostFavorite postFavorite);
|
||||
int deletePostFavorite(@Param("postId") Long postId);
|
||||
List<Post> getAllPost(@Param("userId") Long userId);
|
||||
}
|
||||
|
||||
@@ -13,4 +13,12 @@ public interface UserMapper {
|
||||
* @return 用户
|
||||
*/
|
||||
User getByUsername(String username);
|
||||
|
||||
/**
|
||||
* 添加用户
|
||||
* @param user 用户对象
|
||||
* @return 受影响行数
|
||||
*/
|
||||
Long insertUser(User user);
|
||||
long getMaxUserId();
|
||||
}
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
package com.bao.dating.service;
|
||||
|
||||
import com.bao.dating.common.Result;
|
||||
import com.bao.dating.pojo.entity.Post;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface PostFavoriteService {
|
||||
Result<Map<String,Long>> postFavorite(Long userid,Long postId);
|
||||
Result<?> deletePostFavorite(Long userid,Long postId);
|
||||
Result<List<Post>> getAllFavoritePost(Long userId);
|
||||
}
|
||||
|
||||
@@ -10,4 +10,5 @@ public interface UserService {
|
||||
* @return 登录结果
|
||||
*/
|
||||
UserLoginVO userLogin(UserLoginDTO userLoginDTO);
|
||||
long userRegister(String userAccount, String userPassword);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.bao.dating.common.Result;
|
||||
import com.bao.dating.common.ResultCode;
|
||||
import com.bao.dating.mapper.PostFavoriteMapper;
|
||||
import com.bao.dating.mapper.PostMapper;
|
||||
import com.bao.dating.pojo.entity.Post;
|
||||
import com.bao.dating.pojo.entity.PostFavorite;
|
||||
import com.bao.dating.service.PostFavoriteService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -58,4 +59,10 @@ public class PostFavoriteServiceImpl implements PostFavoriteService {
|
||||
postMapper.decreaseFavoriteCount(postId);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<Post>> getAllFavoritePost(Long userId) {
|
||||
List<Post> result = postFavoriteMapper.getAllPost(userId);
|
||||
return Result.success(ResultCode.SUCCESS,"查询成功",result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@ import com.bao.dating.util.MD5Util;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Service
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
@@ -45,4 +47,38 @@ public class UserServiceImpl implements UserService {
|
||||
userLoginVO.setToken(token);
|
||||
return userLoginVO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户注册
|
||||
* @param userAccount 账户名称
|
||||
* @param userPassword 密码
|
||||
* @return 用户id
|
||||
*/
|
||||
@Override
|
||||
public long userRegister(String userAccount, String userPassword) {
|
||||
//验证非空
|
||||
if (userAccount.isEmpty() ||userPassword.isEmpty()){
|
||||
return -2;
|
||||
}
|
||||
//校验账户不能重复
|
||||
User resultUser = userMapper.getByUsername(userAccount);
|
||||
if (resultUser!=null){
|
||||
return -3;
|
||||
}
|
||||
//对密码进行加密
|
||||
String encryptPassword = MD5Util.encryptWithSalt(userPassword, "yujian");
|
||||
long maxUserId = userMapper.getMaxUserId();
|
||||
User user = new User();
|
||||
user.setUserId(maxUserId+1);
|
||||
user.setUserName(userAccount);
|
||||
user.setSalt("yujian");
|
||||
user.setPasswordHash(encryptPassword);
|
||||
user.setCreatedAt(LocalDateTime.now());
|
||||
user.setUpdatedAt(LocalDateTime.now());
|
||||
Long result = userMapper.insertUser(user);
|
||||
if (result < 0){
|
||||
return -1;
|
||||
}
|
||||
return user.getUserId();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,4 +14,8 @@
|
||||
<select id="selectUserIDByPostID" resultType="java.lang.Long">
|
||||
SELECT user_id FROM post_favorite WHERE post_id = #{postId}
|
||||
</select>
|
||||
<!--查询用户收藏的所有动态-->
|
||||
<select id="getAllPost" resultType="com.bao.dating.pojo.entity.Post">
|
||||
select * from post where post_id in (select post_id from post_favorite where user_id=#{userId})
|
||||
</select>
|
||||
</mapper>
|
||||
@@ -3,9 +3,15 @@
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
|
||||
<mapper namespace="com.bao.dating.mapper.UserMapper">
|
||||
<insert id="insertUser" useGeneratedKeys="true" parameterType="com.bao.dating.pojo.entity.User">
|
||||
insert into user(user_id,user_name,salt,password_hash,created_at,updated_at) values (#{userId},#{userName},#{salt},#{passwordHash},#{createdAt},#{updatedAt})
|
||||
</insert>
|
||||
|
||||
<select id="getByUsername" resultType="com.bao.dating.pojo.entity.User">
|
||||
SELECT * FROM user WHERE user_name = #{userName}
|
||||
</select>
|
||||
<select id="getMaxUserId" resultType="java.lang.Long">
|
||||
SELECT IFNULL(MAX(user_id), 0) AS max_id FROM user;
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user