频道管理——增删改查
对频道进行增删改查,在admin网关中增加leadnews-media路由
package com.heima.wemedia.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.heima.model.common.dtos.PageResponseResult; import com.heima.model.common.dtos.ResponseResult; import com.heima.model.common.enums.AppHttpCodeEnum; import com.heima.model.wemedia.dtos.ChannelDto; import com.heima.model.wemedia.pojos.WmChannel; import com.heima.model.wemedia.pojos.WmNews; import com.heima.wemedia.mapper.WmChannelMapper; import com.heima.wemedia.service.WmChannelService; import com.heima.wemedia.service.WmNewsService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.Date; import java.util.List; @Service @Transactional @Slf4j public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService { /** * 查询所有频道 * @return */ @Override public ResponseResult finddAll() { return ResponseResult.okResult(list()); } /** * 保存频道 * * @param adChannel * @return */ @Override public ResponseResult insert(WmChannel adChannel) { //验证参数 if(adChannel == null){ return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID); } //频道名称不能重复,根据名称查询是否存在 WmChannel channel = getOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, adChannel.getName())); //频道名称已存在 if(channel != null){ return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道已存在"); } adChannel.setCreatedTime(new Date()); save(adChannel); return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS); } /** * 分页查询、条件查询 * * @param dto * @return */ @Override public ResponseResult findByNameAndPage(ChannelDto dto) { //检查参数 if(dto == null){ return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID); } //检查分页 dto.checkParam(); //分页构造器 Page<WmChannel> page = new Page<>(dto.getPage(), dto.getSize()); //条件构造器 LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>(); //添加查询条件,模糊查询 queryWrapper.like(dto.getName()!=null, WmChannel::getName, dto.getName()); //添加排序时间,降序 queryWrapper.orderByDesc(WmChannel::getCreatedTime); page = page(page, queryWrapper); //结果返回 ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal()); responseResult.setData(page.getRecords()); return responseResult; } @Autowired private WmNewsService wmNewsService; /** * 修改频道 * * @param wmChannel * @return */ @Override public ResponseResult update(WmChannel wmChannel) { //检查参数 if(wmChannel == null || wmChannel.getId() == null){ return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID); } //判断是否被引用 int count = wmNewsService.count(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getChannelId, wmChannel.getId()).eq(WmNews::getStatus, WmNews.Status.PUBLISHED.getCode())); if(count > 0){ return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道被引用无法删除"); } updateById(wmChannel); return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS); } /** * 删除频道 * * @param id * @return */ @Override public ResponseResult delete(Integer id) { //检查参数 if(id == null){ return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID); } //查询频道 WmChannel channel = getById(id); if(channel == null){ return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "频道不存在"); } //检查该频道是否被启用 if(channel.getStatus()){ return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道有效,无法操作"); } //检查该频道是否被引用 int count = wmNewsService.count(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getChannelId, id).eq(WmNews::getStatus, WmNews.Status.PUBLISHED.getCode())); if(count > 0){ return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道被引用,无法删除"); } removeById(id); return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS); } }