新增页面接口
定义响应模型
/**
* 继承通用的responseResult响应类,
* 在构造方法中添加模型类参数
*/
@Data
public class CmsPageResult extends ResponseResult {
CmsPage cmsPage;
public CmsPageResult(ResultCode resultCode,CmsPage cmsPage) {
super(resultCode);
this.cmsPage = cmsPage;
}
}
加上CmsPage参数是因为前端可能或使用到返回的数据对象
定义Api接口
/**
* 新增页面
*/
@ApiOperation("新增页面接口")
public CmsPageResult addPage(CmsPage cmsPage);
Service
/**
* 页面新增
* @param cmsPage
* @return
*/
public CmsPageResult addPage(CmsPage cmsPage) {
//校验页面名称,webPath,页面名称唯一性
//根据上述去页面查询集合,查到就说明已存在,查询不到就可以添加
CmsPage cmsPage1 = cmsPageRepository.findByPageNameAndSiteIdAndPageWebPath(cmsPage.getPageName(), cmsPage.getSiteId(), cmsPage.getPageWebPath());
if (cmsPage1 == null) {
//调用dao,新增页面
/**
* 这里因为cms_page页面pageId为主键,所以避免人为设置,
* 不管有没有都将其置为空,让mongoDB的自增长自动生成主键
*/
cmsPage.setPageId(null);
CmsPage save = cmsPageRepository.save(cmsPage1);
return new CmsPageResult(CommonCode.SUCCESS, save);
}
//添加失败
return new CmsPageResult(CommonCode.FAIL, null);
}
由于新增页面需要进行校验, 判断站点ID,webPath,页面名称是否唯一来判断页面是否已经存在,
所以需要先自定Dao接口查询,根据不同的返回接口进行不同的return操作
Controller
/**
* 新增页面接口,页面提交信息,所以用post请求,又因为前端提交过来的都是json
* 数据,所以SpringMVC使用@RequstBody注解转成CmsPage对象
*/
@Override
@PostMapping("/add")
public CmsPageResult addPage(@RequestBody CmsPage cmsPage) {
return pageService.addPage(cmsPage);
}