Files
food-hub-service/src/main/java/com/cxx/food/service/impl/MomentServiceImpl.java

140 lines
4.2 KiB
Java

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.*;
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;
import com.cxx.framework.web.CustomException;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class MomentServiceImpl implements MomentService {
@Resource
private MomentDao momentDao;
@Resource
private MomentMapper momentMapper;
@Resource
private CommentMapper commentMapper;
@Resource
private MomentLikeMapper momentLikeMapper;
@Resource
private MomentImageMapper momentImageMapper;
@Override
public Boolean addMoment(MomentDto momentDto) {
Moment moment = new Moment();
BeanUtils.copyProperties(momentDto, moment);
momentMapper.insert(moment);
insertMomentImage(moment.getId(), momentDto.getImageList());
return Boolean.TRUE;
}
@Override
public Boolean updateMoment(Long id, MomentDto momentDto) {
if (momentMapper.selectById(id) == null) {
throw new CustomException("该朋友圈不存在");
}
Moment moment = new Moment();
BeanUtils.copyProperties(momentDto, moment);
moment.setId(id);
momentMapper.updateById(moment);
deleteMomentImage(id);
insertMomentImage(id, momentDto.getImageList());
return Boolean.TRUE;
}
@Override
public Boolean deleteMoment(Long id) {
deleteMomentImage(id);
momentMapper.deleteById(id);
return Boolean.TRUE;
}
@Override
public List<MomentDto> queryMomentList() {
// TODO 只能查询好友的朋友圈
return momentDao.queryMomentList();
}
@Override
public List<MomentDto> queryMomentById(Long id) {
return momentDao.queryMomentById(id);
}
@Override
public Boolean addComment(Long momentId, String content) {
if (momentMapper.selectById(momentId) == null) {
throw new CustomException("该朋友圈不存在");
}
Comment comment = new Comment();
comment.setContent(content);
comment.setMomentId(momentId);
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));
}
private void insertMomentImage(Long momentId, List<String> imageList) {
if (!imageList.isEmpty()) {
momentDao.insertMomentImage(momentId, imageList);
}
}
}