feat:新增点赞接口
This commit is contained in:
@@ -49,5 +49,17 @@ public class MomentController {
|
||||
@RequestParam String content) {
|
||||
return momentService.addComment(id, content);
|
||||
}
|
||||
|
||||
@Operation(summary = "增加点赞")
|
||||
@PostMapping("/{id}/like")
|
||||
public Boolean addLike(@PathVariable("id") long id) {
|
||||
return momentService.addLike(id);
|
||||
}
|
||||
|
||||
@Operation(summary = "取消点赞")
|
||||
@DeleteMapping("/{id}/like")
|
||||
public Boolean deleteLike(@PathVariable("id") long id) {
|
||||
return momentService.deleteLike(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ public class UserController {
|
||||
@Operation(summary = "更新用户")
|
||||
@PutMapping("/{id}")
|
||||
public Boolean updateUser(@PathVariable("id") Long id,
|
||||
@JsonView(WriteView.class) @Validated @RequestBody UserDto userDto) {
|
||||
@Validated @RequestBody UserDto userDto) {
|
||||
return userService.updateUser(id, userDto);
|
||||
}
|
||||
}
|
||||
@@ -42,6 +42,10 @@ public class MomentDto {
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
|
||||
private Date date;
|
||||
|
||||
@Schema(description = "点赞用户")
|
||||
@JsonView(ReadView.class)
|
||||
private List<Long> likeList;
|
||||
|
||||
@Schema(description = "评论内容")
|
||||
@JsonView(ReadView.class)
|
||||
private List<CommentDto> commentList;
|
||||
|
||||
@@ -8,8 +8,8 @@ import lombok.EqualsAndHashCode;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@TableName("moment")
|
||||
public class Like extends AbstractEntity {
|
||||
@TableName("moment_like")
|
||||
public class MomentLike extends AbstractEntity {
|
||||
@TableField("moment_id")
|
||||
private Long momentId;
|
||||
|
||||
@@ -12,7 +12,7 @@ import java.util.List;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@TableName("user")
|
||||
@TableName(value = "user", autoResultMap = true)
|
||||
public class User extends AbstractEntity {
|
||||
@TableField("username")
|
||||
private String username;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package com.cxx.food.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.cxx.food.entity.Like;
|
||||
import com.cxx.food.entity.MomentLike;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface LikeMapper extends BaseMapper<Like> {
|
||||
public interface MomentLikeMapper extends BaseMapper<MomentLike> {
|
||||
}
|
||||
@@ -15,4 +15,8 @@ public interface MomentService {
|
||||
List<MomentDto> queryMoment();
|
||||
|
||||
Boolean addComment(Long momentId, String content);
|
||||
|
||||
Boolean addLike(Long momentId);
|
||||
|
||||
Boolean deleteLike(Long momentId);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
package com.cxx.food.service.impl;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.cxx.food.dao.MomentDao;
|
||||
import com.cxx.food.dto.MomentDto;
|
||||
import com.cxx.food.entity.Comment;
|
||||
import com.cxx.food.entity.Moment;
|
||||
import com.cxx.food.entity.MomentImage;
|
||||
import com.cxx.food.entity.*;
|
||||
import com.cxx.food.mapper.CommentMapper;
|
||||
import com.cxx.food.mapper.MomentLikeMapper;
|
||||
import com.cxx.food.mapper.MomentImageMapper;
|
||||
import com.cxx.food.mapper.MomentMapper;
|
||||
import com.cxx.food.service.MomentService;
|
||||
@@ -28,6 +29,9 @@ public class MomentServiceImpl implements MomentService {
|
||||
@Resource
|
||||
private CommentMapper commentMapper;
|
||||
|
||||
@Resource
|
||||
private MomentLikeMapper momentLikeMapper;
|
||||
|
||||
@Resource
|
||||
private MomentImageMapper momentImageMapper;
|
||||
|
||||
@@ -83,6 +87,41 @@ public class MomentServiceImpl implements MomentService {
|
||||
return commentMapper.insert(comment) == 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean addLike(Long momentId) {
|
||||
LambdaQueryWrapper<MomentLike> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(MomentLike::getMomentId, momentId).eq(MomentLike::getCreateBy, StpUtil.getLoginIdAsString());
|
||||
MomentLike like = momentLikeMapper.selectOne(queryWrapper);
|
||||
|
||||
if (like != null) {
|
||||
if (like.getIsDelete() == 0) {
|
||||
throw new CustomException("已经点赞了");
|
||||
} else {
|
||||
like.setIsDelete(0);
|
||||
return momentLikeMapper.updateById(like) == 1;
|
||||
}
|
||||
}
|
||||
|
||||
MomentLike newLike = new MomentLike();
|
||||
newLike.setMomentId(momentId);
|
||||
newLike.setIsDelete(0);
|
||||
return momentLikeMapper.insert(newLike) == 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean deleteLike(Long momentId) {
|
||||
LambdaQueryWrapper<MomentLike> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(MomentLike::getMomentId, momentId).eq(MomentLike::getCreateBy, StpUtil.getLoginIdAsString());
|
||||
MomentLike like = momentLikeMapper.selectOne(queryWrapper);
|
||||
|
||||
if (like == null ) {
|
||||
throw new CustomException("该朋友圈不存在");
|
||||
}
|
||||
|
||||
like.setIsDelete(1);
|
||||
return momentLikeMapper.updateById(like) == 1;
|
||||
}
|
||||
|
||||
private void deleteMomentImage(Long id) {
|
||||
momentImageMapper.delete(Wrappers.lambdaQuery(MomentImage.class).eq(MomentImage::getMomentId, id));
|
||||
}
|
||||
|
||||
@@ -10,6 +10,9 @@
|
||||
<result property="date" column="date"/>
|
||||
<collection property="imageList" ofType="java.lang.String"
|
||||
column="id" select="queryMomentImage"/>
|
||||
<collection property="likeList" ofType="long"
|
||||
column="id" select="queryLike">
|
||||
</collection>
|
||||
<collection property="commentList" ofType="com.cxx.food.dto.CommentDto"
|
||||
column="id" select="queryComment">
|
||||
</collection>
|
||||
@@ -23,6 +26,13 @@
|
||||
<result property="date" column="date"/>
|
||||
</resultMap>
|
||||
|
||||
<select id="queryLike" resultType="long">
|
||||
SELECT a.create_by AS id
|
||||
FROM moment_like AS a
|
||||
WHERE moment_id = #{id}
|
||||
AND a.is_delete = 0
|
||||
</select>
|
||||
|
||||
<select id="queryComment" resultMap="commentResultMap">
|
||||
SELECT a.id AS id,
|
||||
b.username AS username,
|
||||
|
||||
Reference in New Issue
Block a user