阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_04-新增页面-服务端-接口开发

时间:2024-01-16 09:13:08

api接口定义方法

阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_04-新增页面-服务端-接口开发
api的微服务里面。CmsPageControllerApi内定义add方法,返回类型是CmsPageResult
阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_04-新增页面-服务端-接口开发
CmsPageResult继承了ResponseResult
阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_04-新增页面-服务端-接口开发
ResponseResult里面这三项,SUCCESS、SUCCESS_CODE、message
阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_04-新增页面-服务端-接口开发
把新增的数据响应给客户端,客户端可能会用
阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_04-新增页面-服务端-接口开发
加上swagger的注解
阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_04-新增页面-服务端-接口开发

dao

dao里面就不用写了我们直接用springDataMongoDB自带的save方法就可以了。在继承的MongoReposityory即可
阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_04-新增页面-服务端-接口开发

Service

新增之前要先校验页面的唯一性
阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_04-新增页面-服务端-接口开发
mongoDB数据内,右键cmsPage这个集合,创建索引
阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_04-新增页面-服务端-接口开发
阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_04-新增页面-服务端-接口开发
站点id、页面名称、页面的路径
阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_04-新增页面-服务端-接口开发
阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_04-新增页面-服务端-接口开发

阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_04-新增页面-服务端-接口开发
右下角 创建这个索引
阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_04-新增页面-服务端-接口开发
在dao里面定义根据这是三个字段的查询条件。字段之间用And来拼接。
阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_04-新增页面-服务端-接口开发

阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_04-新增页面-服务端-接口开发
失败和成功的 返回数据
阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_04-新增页面-服务端-接口开发

要注意CmsPageResult的命名空间,

阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_04-新增页面-服务端-接口开发

controller内直接调用Service
阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_04-新增页面-服务端-接口开发
请求的json数据转换成对象。使用@RequestBody
阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_04-新增页面-服务端-接口开发

以上接口开发 完成

最终代码

package com.xuecheng.manage_cms.service;

import com.xuecheng.framework.domain.cms.CmsPage;
import com.xuecheng.framework.domain.cms.request.QueryPageRequest;
import com.xuecheng.framework.domain.cms.response.CmsPageResult;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.QueryResult;
import com.xuecheng.manage_cms.dao.CmsPageRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service; @Service
public class PageService {
@Autowired
CmsPageRepository cmsPageRepository;
public QueryResponseResult findList(int page,int size, QueryPageRequest queryPageRequest) { if(queryPageRequest==null){
queryPageRequest=new QueryPageRequest();
}
//自定义查询条件
ExampleMatcher exampleMatcher=ExampleMatcher.matching()
.withMatcher("pageAliase",ExampleMatcher.GenericPropertyMatchers.contains());
//条件之对象
CmsPage cmsPage=new CmsPage();
//设置条件值 (站点ID)
if(StringUtils.isNotEmpty(queryPageRequest.getSiteId())){
cmsPage.setSiteId(queryPageRequest.getSiteId());
}
//设置模板id 作为查询条件
if(StringUtils.isNotEmpty(queryPageRequest.getTemplateId())){
cmsPage.setTemplateId(queryPageRequest.getTemplateId());
}
//设置页面别名为查询条件
if(StringUtils.isNotEmpty(queryPageRequest.getPageAliase())){
cmsPage.setPageAliase(queryPageRequest.getPageAliase());
}
//定义Exmaple对象
Example<CmsPage> example=Example.of(cmsPage,exampleMatcher); if(page<){
page=;
}
page = page -;
if(size<=){
size = ;
}
Pageable pageable = PageRequest.of(page, size);
Page<CmsPage> all = cmsPageRepository.findAll(example,pageable);
QueryResult queryResult=new QueryResult();
queryResult.setList(all.getContent());//设置返回的列表数据
queryResult.setTotal(all.getTotalElements());//设置总记录数
QueryResponseResult queryResponseResult=new QueryResponseResult(CommonCode.SUCCESS,queryResult);
return queryResponseResult;
} public CmsPageResult add(CmsPage cmsPage){
//校验页面名称、站点Id、页面WebPath的唯一性
CmsPage cmsPage1=cmsPageRepository.findByPageNameAndSiteIdAndPageWebPath(cmsPage.getPageName(),cmsPage.getSiteId(),cmsPage.getPageWebPath());
if(cmsPage1==null){
cmsPage.setPageId(null);//设置设置为null 让mongoDB自动去生成,
cmsPageRepository.save(cmsPage);
return new CmsPageResult(CommonCode.SUCCESS,cmsPage);
}
return new CmsPageResult(CommonCode.FAIL,null);
}
}

pageService

cmsPageController


@Override
@PostMapping("/add")
public CmsPageResult add(@RequestBody CmsPage cmsPage) {
return pageService.add(cmsPage);
}