新闻
This commit is contained in:
parent
bbee0ea4e4
commit
b313240fe9
@ -0,0 +1,95 @@
|
|||||||
|
package cn.iocoder.yudao.module.base.controller.admin.news;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import io.swagger.v3.oas.annotations.Parameter;
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
|
||||||
|
import javax.validation.constraints.*;
|
||||||
|
import javax.validation.*;
|
||||||
|
import javax.servlet.http.*;
|
||||||
|
import java.util.*;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.common.pojo.PageParam;
|
||||||
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||||
|
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||||
|
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||||
|
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
|
||||||
|
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.module.base.controller.admin.news.vo.*;
|
||||||
|
import cn.iocoder.yudao.module.base.dal.dataobject.news.NewsDO;
|
||||||
|
import cn.iocoder.yudao.module.base.service.news.NewsService;
|
||||||
|
|
||||||
|
@Tag(name = "管理后台 - 公司新闻")
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/base/news")
|
||||||
|
@Validated
|
||||||
|
public class NewsController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private NewsService newsService;
|
||||||
|
|
||||||
|
@PostMapping("/create")
|
||||||
|
@Operation(summary = "创建公司新闻")
|
||||||
|
@PreAuthorize("@ss.hasPermission('base:news:create')")
|
||||||
|
public CommonResult<Long> createNews(@Valid @RequestBody NewsSaveReqVO createReqVO) {
|
||||||
|
return success(newsService.createNews(createReqVO));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/update")
|
||||||
|
@Operation(summary = "更新公司新闻")
|
||||||
|
@PreAuthorize("@ss.hasPermission('base:news:update')")
|
||||||
|
public CommonResult<Boolean> updateNews(@Valid @RequestBody NewsSaveReqVO updateReqVO) {
|
||||||
|
newsService.updateNews(updateReqVO);
|
||||||
|
return success(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/delete")
|
||||||
|
@Operation(summary = "删除公司新闻")
|
||||||
|
@Parameter(name = "id", description = "编号", required = true)
|
||||||
|
@PreAuthorize("@ss.hasPermission('base:news:delete')")
|
||||||
|
public CommonResult<Boolean> deleteNews(@RequestParam("id") Long id) {
|
||||||
|
newsService.deleteNews(id);
|
||||||
|
return success(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/get")
|
||||||
|
@Operation(summary = "获得公司新闻")
|
||||||
|
@Parameter(name = "id", description = "编号", required = true, example = "1024")
|
||||||
|
@PreAuthorize("@ss.hasPermission('base:news:query')")
|
||||||
|
public CommonResult<NewsRespVO> getNews(@RequestParam("id") Long id) {
|
||||||
|
NewsDO news = newsService.getNews(id);
|
||||||
|
return success(BeanUtils.toBean(news, NewsRespVO.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/page")
|
||||||
|
@Operation(summary = "获得公司新闻分页")
|
||||||
|
@PreAuthorize("@ss.hasPermission('base:news:query')")
|
||||||
|
public CommonResult<PageResult<NewsRespVO>> getNewsPage(@Valid NewsPageReqVO pageReqVO) {
|
||||||
|
PageResult<NewsDO> pageResult = newsService.getNewsPage(pageReqVO);
|
||||||
|
return success(BeanUtils.toBean(pageResult, NewsRespVO.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/export-excel")
|
||||||
|
@Operation(summary = "导出公司新闻 Excel")
|
||||||
|
@PreAuthorize("@ss.hasPermission('base:news:export')")
|
||||||
|
@ApiAccessLog(operateType = EXPORT)
|
||||||
|
public void exportNewsExcel(@Valid NewsPageReqVO pageReqVO,
|
||||||
|
HttpServletResponse response) throws IOException {
|
||||||
|
pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
|
||||||
|
List<NewsDO> list = newsService.getNewsPage(pageReqVO).getList();
|
||||||
|
// 导出 Excel
|
||||||
|
ExcelUtils.write(response, "公司新闻.xls", "数据", NewsRespVO.class,
|
||||||
|
BeanUtils.toBean(list, NewsRespVO.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
package cn.iocoder.yudao.module.base.controller.admin.news.vo;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
import java.util.*;
|
||||||
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
|
import cn.iocoder.yudao.framework.common.pojo.PageParam;
|
||||||
|
import org.springframework.format.annotation.DateTimeFormat;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
|
||||||
|
|
||||||
|
@Schema(description = "管理后台 - 公司新闻分页 Request VO")
|
||||||
|
@Data
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@ToString(callSuper = true)
|
||||||
|
public class NewsPageReqVO extends PageParam {
|
||||||
|
|
||||||
|
@Schema(description = "新闻id", example = "15159")
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@Schema(description = "新闻名称", example = "芋艿")
|
||||||
|
private String newsName;
|
||||||
|
|
||||||
|
@Schema(description = "发布时间")
|
||||||
|
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
|
||||||
|
private LocalDateTime[] newsCreatetime;
|
||||||
|
|
||||||
|
@Schema(description = "新闻分类", example = "1")
|
||||||
|
private Integer contentType;
|
||||||
|
|
||||||
|
@Schema(description = "新闻图片状态", example = "1")
|
||||||
|
private String newsStatus;
|
||||||
|
|
||||||
|
@Schema(description = "新闻状态", example = "2")
|
||||||
|
private String status;
|
||||||
|
|
||||||
|
@Schema(description = "部门id", example = "31402")
|
||||||
|
private Long deptId;
|
||||||
|
|
||||||
|
@Schema(description = "创建时间")
|
||||||
|
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
|
||||||
|
private LocalDateTime[] createTime;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,61 @@
|
|||||||
|
package cn.iocoder.yudao.module.base.controller.admin.news.vo;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
|
import lombok.*;
|
||||||
|
import java.util.*;
|
||||||
|
import org.springframework.format.annotation.DateTimeFormat;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import com.alibaba.excel.annotation.*;
|
||||||
|
import cn.iocoder.yudao.framework.excel.core.annotations.DictFormat;
|
||||||
|
import cn.iocoder.yudao.framework.excel.core.convert.DictConvert;
|
||||||
|
|
||||||
|
@Schema(description = "管理后台 - 公司新闻 Response VO")
|
||||||
|
@Data
|
||||||
|
@ExcelIgnoreUnannotated
|
||||||
|
public class NewsRespVO {
|
||||||
|
|
||||||
|
@Schema(description = "新闻id", requiredMode = Schema.RequiredMode.REQUIRED, example = "15159")
|
||||||
|
@ExcelProperty("新闻id")
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@Schema(description = "新闻名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "芋艿")
|
||||||
|
@ExcelProperty("新闻名称")
|
||||||
|
private String newsName;
|
||||||
|
|
||||||
|
@Schema(description = "发布时间")
|
||||||
|
@ExcelProperty("发布时间")
|
||||||
|
private LocalDateTime newsCreatetime;
|
||||||
|
|
||||||
|
@Schema(description = "轮换图片", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||||
|
@ExcelProperty("轮换图片")
|
||||||
|
private String newsImg;
|
||||||
|
|
||||||
|
@Schema(description = "发布内容", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||||
|
@ExcelProperty("发布内容")
|
||||||
|
// private byte[] newsContent;修改为String类型不然会返回字节
|
||||||
|
private String newsContent;
|
||||||
|
|
||||||
|
@Schema(description = "新闻分类", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
||||||
|
@ExcelProperty(value = "新闻分类", converter = DictConvert.class)
|
||||||
|
@DictFormat("oa_news_type") // TODO 代码优化:建议设置到对应的 DictTypeConstants 枚举类中
|
||||||
|
private Integer contentType;
|
||||||
|
|
||||||
|
@Schema(description = "新闻图片状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
||||||
|
@ExcelProperty(value = "新闻图片状态", converter = DictConvert.class)
|
||||||
|
@DictFormat("oa_news_img_status") // TODO 代码优化:建议设置到对应的 DictTypeConstants 枚举类中
|
||||||
|
private String newsStatus;
|
||||||
|
|
||||||
|
@Schema(description = "新闻状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")
|
||||||
|
@ExcelProperty(value = "新闻状态", converter = DictConvert.class)
|
||||||
|
@DictFormat("oa_news_status") // TODO 代码优化:建议设置到对应的 DictTypeConstants 枚举类中
|
||||||
|
private String status;
|
||||||
|
|
||||||
|
@Schema(description = "部门id", requiredMode = Schema.RequiredMode.REQUIRED, example = "31402")
|
||||||
|
@ExcelProperty("部门id")
|
||||||
|
private Long deptId;
|
||||||
|
|
||||||
|
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||||
|
@ExcelProperty("创建时间")
|
||||||
|
private LocalDateTime createTime;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
package cn.iocoder.yudao.module.base.controller.admin.news.vo;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
|
import lombok.*;
|
||||||
|
import java.util.*;
|
||||||
|
import javax.validation.constraints.*;
|
||||||
|
import org.springframework.format.annotation.DateTimeFormat;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Schema(description = "管理后台 - 公司新闻新增/修改 Request VO")
|
||||||
|
@Data
|
||||||
|
public class NewsSaveReqVO {
|
||||||
|
|
||||||
|
@Schema(description = "新闻id", requiredMode = Schema.RequiredMode.REQUIRED, example = "15159")
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@Schema(description = "新闻名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "芋艿")
|
||||||
|
@NotEmpty(message = "新闻名称不能为空")
|
||||||
|
private String newsName;
|
||||||
|
|
||||||
|
@Schema(description = "发布时间")
|
||||||
|
private LocalDateTime newsCreatetime;
|
||||||
|
|
||||||
|
@Schema(description = "轮换图片", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||||
|
@NotEmpty(message = "轮换图片不能为空")
|
||||||
|
private String newsImg;
|
||||||
|
|
||||||
|
@Schema(description = "发布内容", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||||
|
@NotNull(message = "发布内容不能为空")
|
||||||
|
// private byte[] newsContent;修改为String类型不然会返回字节
|
||||||
|
private String newsContent;
|
||||||
|
|
||||||
|
@Schema(description = "新闻分类", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
||||||
|
@NotNull(message = "新闻分类不能为空")
|
||||||
|
private Integer contentType;
|
||||||
|
|
||||||
|
@Schema(description = "新闻图片状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
||||||
|
@NotEmpty(message = "新闻图片状态不能为空")
|
||||||
|
private String newsStatus;
|
||||||
|
|
||||||
|
@Schema(description = "新闻状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")
|
||||||
|
@NotEmpty(message = "新闻状态不能为空")
|
||||||
|
private String status;
|
||||||
|
|
||||||
|
@Schema(description = "部门id", requiredMode = Schema.RequiredMode.REQUIRED, example = "31402")
|
||||||
|
@NotNull(message = "部门id不能为空")
|
||||||
|
private Long deptId;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,72 @@
|
|||||||
|
package cn.iocoder.yudao.module.base.dal.dataobject.news;
|
||||||
|
|
||||||
|
import com.sun.xml.bind.v2.TODO;
|
||||||
|
import lombok.*;
|
||||||
|
import java.util.*;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import com.baomidou.mybatisplus.annotation.*;
|
||||||
|
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 公司新闻 DO
|
||||||
|
*
|
||||||
|
* @author 君风
|
||||||
|
*/
|
||||||
|
@TableName("oa_news")
|
||||||
|
@KeySequence("oa_news_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
|
||||||
|
@Data
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@ToString(callSuper = true)
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class NewsDO extends BaseDO {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新闻id
|
||||||
|
*/
|
||||||
|
@TableId
|
||||||
|
private Long id;
|
||||||
|
/**
|
||||||
|
* 新闻名称
|
||||||
|
*/
|
||||||
|
private String newsName;
|
||||||
|
/**
|
||||||
|
* 发布时间
|
||||||
|
*/
|
||||||
|
private LocalDateTime newsCreatetime;
|
||||||
|
/**
|
||||||
|
* 轮换图片
|
||||||
|
*/
|
||||||
|
private String newsImg;
|
||||||
|
/**
|
||||||
|
* 发布内容修改为String类型不然会返回字节
|
||||||
|
*/
|
||||||
|
// private byte[] newsContent;
|
||||||
|
private String newsContent;
|
||||||
|
/**
|
||||||
|
* 新闻分类
|
||||||
|
*
|
||||||
|
* 枚举 {@link TODO oa_news_type 对应的类}
|
||||||
|
*/
|
||||||
|
private Integer contentType;
|
||||||
|
/**
|
||||||
|
* 新闻图片状态
|
||||||
|
*
|
||||||
|
* 枚举 {@link TODO oa_news_img_status 对应的类}
|
||||||
|
*/
|
||||||
|
private String newsStatus;
|
||||||
|
/**
|
||||||
|
* 新闻状态
|
||||||
|
*
|
||||||
|
* 枚举 {@link TODO oa_news_status 对应的类}
|
||||||
|
*/
|
||||||
|
private String status;
|
||||||
|
/**
|
||||||
|
* 部门id
|
||||||
|
*/
|
||||||
|
private Long deptId;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
package cn.iocoder.yudao.module.base.dal.mysql.news;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||||
|
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
|
||||||
|
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
|
||||||
|
import cn.iocoder.yudao.module.base.dal.dataobject.news.NewsDO;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import cn.iocoder.yudao.module.base.controller.admin.news.vo.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 公司新闻 Mapper
|
||||||
|
*
|
||||||
|
* @author 君风
|
||||||
|
*/
|
||||||
|
@Mapper
|
||||||
|
public interface NewsMapper extends BaseMapperX<NewsDO> {
|
||||||
|
|
||||||
|
default PageResult<NewsDO> selectPage(NewsPageReqVO reqVO) {
|
||||||
|
return selectPage(reqVO, new LambdaQueryWrapperX<NewsDO>()
|
||||||
|
.eqIfPresent(NewsDO::getId, reqVO.getId())
|
||||||
|
.likeIfPresent(NewsDO::getNewsName, reqVO.getNewsName())
|
||||||
|
.betweenIfPresent(NewsDO::getNewsCreatetime, reqVO.getNewsCreatetime())
|
||||||
|
.eqIfPresent(NewsDO::getContentType, reqVO.getContentType())
|
||||||
|
.eqIfPresent(NewsDO::getNewsStatus, reqVO.getNewsStatus())
|
||||||
|
.eqIfPresent(NewsDO::getStatus, reqVO.getStatus())
|
||||||
|
.eqIfPresent(NewsDO::getDeptId, reqVO.getDeptId())
|
||||||
|
.betweenIfPresent(NewsDO::getCreateTime, reqVO.getCreateTime())
|
||||||
|
.orderByDesc(NewsDO::getId));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
package cn.iocoder.yudao.module.base.service.news;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import javax.validation.*;
|
||||||
|
import cn.iocoder.yudao.module.base.controller.admin.news.vo.*;
|
||||||
|
import cn.iocoder.yudao.module.base.dal.dataobject.news.NewsDO;
|
||||||
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||||
|
import cn.iocoder.yudao.framework.common.pojo.PageParam;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 公司新闻 Service 接口
|
||||||
|
*
|
||||||
|
* @author 君风
|
||||||
|
*/
|
||||||
|
public interface NewsService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建公司新闻
|
||||||
|
*
|
||||||
|
* @param createReqVO 创建信息
|
||||||
|
* @return 编号
|
||||||
|
*/
|
||||||
|
Long createNews(@Valid NewsSaveReqVO createReqVO);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新公司新闻
|
||||||
|
*
|
||||||
|
* @param updateReqVO 更新信息
|
||||||
|
*/
|
||||||
|
void updateNews(@Valid NewsSaveReqVO updateReqVO);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除公司新闻
|
||||||
|
*
|
||||||
|
* @param id 编号
|
||||||
|
*/
|
||||||
|
void deleteNews(Long id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得公司新闻
|
||||||
|
*
|
||||||
|
* @param id 编号
|
||||||
|
* @return 公司新闻
|
||||||
|
*/
|
||||||
|
NewsDO getNews(Long id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得公司新闻分页
|
||||||
|
*
|
||||||
|
* @param pageReqVO 分页查询
|
||||||
|
* @return 公司新闻分页
|
||||||
|
*/
|
||||||
|
PageResult<NewsDO> getNewsPage(NewsPageReqVO pageReqVO);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,74 @@
|
|||||||
|
package cn.iocoder.yudao.module.base.service.news;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import cn.iocoder.yudao.module.base.controller.admin.news.vo.*;
|
||||||
|
import cn.iocoder.yudao.module.base.dal.dataobject.news.NewsDO;
|
||||||
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||||
|
import cn.iocoder.yudao.framework.common.pojo.PageParam;
|
||||||
|
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.module.base.dal.mysql.news.NewsMapper;
|
||||||
|
|
||||||
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||||
|
import static cn.iocoder.yudao.module.base.enums.ErrorCodeConstants.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 公司新闻 Service 实现类
|
||||||
|
*
|
||||||
|
* @author 君风
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
@Validated
|
||||||
|
public class NewsServiceImpl implements NewsService {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private NewsMapper newsMapper;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Long createNews(NewsSaveReqVO createReqVO) {
|
||||||
|
// 插入
|
||||||
|
NewsDO news = BeanUtils.toBean(createReqVO, NewsDO.class);
|
||||||
|
newsMapper.insert(news);
|
||||||
|
// 返回
|
||||||
|
return news.getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateNews(NewsSaveReqVO updateReqVO) {
|
||||||
|
// 校验存在
|
||||||
|
validateNewsExists(updateReqVO.getId());
|
||||||
|
// 更新
|
||||||
|
NewsDO updateObj = BeanUtils.toBean(updateReqVO, NewsDO.class);
|
||||||
|
newsMapper.updateById(updateObj);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteNews(Long id) {
|
||||||
|
// 校验存在
|
||||||
|
validateNewsExists(id);
|
||||||
|
// 删除
|
||||||
|
newsMapper.deleteById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void validateNewsExists(Long id) {
|
||||||
|
if (newsMapper.selectById(id) == null) {
|
||||||
|
throw exception(NEWS_NOT_EXISTS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NewsDO getNews(Long id) {
|
||||||
|
return newsMapper.selectById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PageResult<NewsDO> getNewsPage(NewsPageReqVO pageReqVO) {
|
||||||
|
return newsMapper.selectPage(pageReqVO);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
<mapper namespace="cn.iocoder.yudao.module.base.dal.mysql.news.NewsMapper">
|
||||||
|
|
||||||
|
<!--
|
||||||
|
一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。
|
||||||
|
无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。
|
||||||
|
代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。
|
||||||
|
文档可见:https://www.iocoder.cn/MyBatis/x-plugins/
|
||||||
|
-->
|
||||||
|
|
||||||
|
</mapper>
|
@ -0,0 +1,158 @@
|
|||||||
|
package cn.iocoder.yudao.module.base.service.news;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Disabled;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.module.base.controller.admin.news.vo.*;
|
||||||
|
import cn.iocoder.yudao.module.base.dal.dataobject.news.NewsDO;
|
||||||
|
import cn.iocoder.yudao.module.base.dal.mysql.news.NewsMapper;
|
||||||
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
import java.util.*;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
import static cn.hutool.core.util.RandomUtil.*;
|
||||||
|
import static cn.iocoder.yudao.module.base.enums.ErrorCodeConstants.*;
|
||||||
|
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.*;
|
||||||
|
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
|
||||||
|
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.*;
|
||||||
|
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.*;
|
||||||
|
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.*;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link NewsServiceImpl} 的单元测试类
|
||||||
|
*
|
||||||
|
* @author 君风
|
||||||
|
*/
|
||||||
|
@Import(NewsServiceImpl.class)
|
||||||
|
public class NewsServiceImplTest extends BaseDbUnitTest {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private NewsServiceImpl newsService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private NewsMapper newsMapper;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCreateNews_success() {
|
||||||
|
// 准备参数
|
||||||
|
NewsSaveReqVO createReqVO = randomPojo(NewsSaveReqVO.class).setId(null);
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
Long newsId = newsService.createNews(createReqVO);
|
||||||
|
// 断言
|
||||||
|
assertNotNull(newsId);
|
||||||
|
// 校验记录的属性是否正确
|
||||||
|
NewsDO news = newsMapper.selectById(newsId);
|
||||||
|
assertPojoEquals(createReqVO, news, "id");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateNews_success() {
|
||||||
|
// mock 数据
|
||||||
|
NewsDO dbNews = randomPojo(NewsDO.class);
|
||||||
|
newsMapper.insert(dbNews);// @Sql: 先插入出一条存在的数据
|
||||||
|
// 准备参数
|
||||||
|
NewsSaveReqVO updateReqVO = randomPojo(NewsSaveReqVO.class, o -> {
|
||||||
|
o.setId(dbNews.getId()); // 设置更新的 ID
|
||||||
|
});
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
newsService.updateNews(updateReqVO);
|
||||||
|
// 校验是否更新正确
|
||||||
|
NewsDO news = newsMapper.selectById(updateReqVO.getId()); // 获取最新的
|
||||||
|
assertPojoEquals(updateReqVO, news);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateNews_notExists() {
|
||||||
|
// 准备参数
|
||||||
|
NewsSaveReqVO updateReqVO = randomPojo(NewsSaveReqVO.class);
|
||||||
|
|
||||||
|
// 调用, 并断言异常
|
||||||
|
assertServiceException(() -> newsService.updateNews(updateReqVO), NEWS_NOT_EXISTS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteNews_success() {
|
||||||
|
// mock 数据
|
||||||
|
NewsDO dbNews = randomPojo(NewsDO.class);
|
||||||
|
newsMapper.insert(dbNews);// @Sql: 先插入出一条存在的数据
|
||||||
|
// 准备参数
|
||||||
|
Long id = dbNews.getId();
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
newsService.deleteNews(id);
|
||||||
|
// 校验数据不存在了
|
||||||
|
assertNull(newsMapper.selectById(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteNews_notExists() {
|
||||||
|
// 准备参数
|
||||||
|
Long id = randomLongId();
|
||||||
|
|
||||||
|
// 调用, 并断言异常
|
||||||
|
assertServiceException(() -> newsService.deleteNews(id), NEWS_NOT_EXISTS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解
|
||||||
|
public void testGetNewsPage() {
|
||||||
|
// mock 数据
|
||||||
|
NewsDO dbNews = randomPojo(NewsDO.class, o -> { // 等会查询到
|
||||||
|
o.setId(null);
|
||||||
|
o.setNewsName(null);
|
||||||
|
o.setNewsCreatetime(null);
|
||||||
|
o.setContentType(null);
|
||||||
|
o.setNewsStatus(null);
|
||||||
|
o.setStatus(null);
|
||||||
|
o.setDeptId(null);
|
||||||
|
o.setCreateTime(null);
|
||||||
|
});
|
||||||
|
newsMapper.insert(dbNews);
|
||||||
|
// 测试 id 不匹配
|
||||||
|
newsMapper.insert(cloneIgnoreId(dbNews, o -> o.setId(null)));
|
||||||
|
// 测试 newsName 不匹配
|
||||||
|
newsMapper.insert(cloneIgnoreId(dbNews, o -> o.setNewsName(null)));
|
||||||
|
// 测试 newsCreatetime 不匹配
|
||||||
|
newsMapper.insert(cloneIgnoreId(dbNews, o -> o.setNewsCreatetime(null)));
|
||||||
|
// 测试 contentType 不匹配
|
||||||
|
newsMapper.insert(cloneIgnoreId(dbNews, o -> o.setContentType(null)));
|
||||||
|
// 测试 newsStatus 不匹配
|
||||||
|
newsMapper.insert(cloneIgnoreId(dbNews, o -> o.setNewsStatus(null)));
|
||||||
|
// 测试 status 不匹配
|
||||||
|
newsMapper.insert(cloneIgnoreId(dbNews, o -> o.setStatus(null)));
|
||||||
|
// 测试 deptId 不匹配
|
||||||
|
newsMapper.insert(cloneIgnoreId(dbNews, o -> o.setDeptId(null)));
|
||||||
|
// 测试 createTime 不匹配
|
||||||
|
newsMapper.insert(cloneIgnoreId(dbNews, o -> o.setCreateTime(null)));
|
||||||
|
// 准备参数
|
||||||
|
NewsPageReqVO reqVO = new NewsPageReqVO();
|
||||||
|
reqVO.setId(null);
|
||||||
|
reqVO.setNewsName(null);
|
||||||
|
reqVO.setNewsCreatetime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
|
||||||
|
reqVO.setContentType(null);
|
||||||
|
reqVO.setNewsStatus(null);
|
||||||
|
reqVO.setStatus(null);
|
||||||
|
reqVO.setDeptId(null);
|
||||||
|
reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
PageResult<NewsDO> pageResult = newsService.getNewsPage(reqVO);
|
||||||
|
// 断言
|
||||||
|
assertEquals(1, pageResult.getTotal());
|
||||||
|
assertEquals(1, pageResult.getList().size());
|
||||||
|
assertPojoEquals(dbNews, pageResult.getList().get(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
DELETE FROM "oa_news";
|
@ -0,0 +1,18 @@
|
|||||||
|
CREATE TABLE IF NOT EXISTS "oa_news" (
|
||||||
|
"id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
|
||||||
|
"news_name" varchar NOT NULL,
|
||||||
|
"news_createtime" varchar,
|
||||||
|
"news_img" varchar NOT NULL,
|
||||||
|
"news_content" varchar NOT NULL,
|
||||||
|
"content_type" int NOT NULL,
|
||||||
|
"news_status" varchar NOT NULL,
|
||||||
|
"status" varchar NOT NULL,
|
||||||
|
"dept_id" bigint NOT NULL,
|
||||||
|
"creator" varchar DEFAULT '',
|
||||||
|
"create_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
"updater" varchar DEFAULT '',
|
||||||
|
"update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||||
|
"deleted" bit NOT NULL DEFAULT FALSE,
|
||||||
|
"tenant_id" bigint NOT NULL DEFAULT 0,
|
||||||
|
PRIMARY KEY ("id")
|
||||||
|
) COMMENT '公司新闻表';
|
Loading…
Reference in New Issue
Block a user