Java's new project learning online notes - day3(3)

2 new page 2.1 new page interface definition 1. Define response model @Data public class CmsPageResult extends ResponseResult { CmsPage cmsPage; publ...

2 new page 2.1 new page interface definition
1. Define response model

@Data public class CmsPageResult extends ResponseResult { CmsPage cmsPage; public CmsPageResult(ResultCode resultCode,CmsPage cmsPage) { super(resultCode); this.cmsPage = cmsPage; } }

2. Define add api add interface in api project:

@ApiOperation("Add page") public CmsPageResult add(CmsPage cmsPage);

2.2 new page server development
2.2.1 page unique index
Create page name, site Id, and page web path as unique index 2.2.2 Dao in CMS page set
1. Add a page query method based on page name, site Id and page web path, which is used to verify whether the page exists

public interface CmsPageRepository extends MongoRepository<CmsPage,String> { //Query according to page name, site id and page access path CmsPage findByPageNameAndSiteIdAndPageWebPath(String pageName,String siteId,String pageWebPath); . .

2. Use the save method provided by CmsPageRepository. 2.2.3 Service

//Add page public CmsPageResult add(CmsPage cmsPage){ //Verify that the page exists, //Query according to page name, site Id, page web path CmsPage cmsPage1 = cmsPageRepository.findByPageNameAndSiteIdAndPageWebPath(cmsPage.getPageName(), cmsPage.getSiteId(), cmsPage.getPageWebPath()); if(cmsPage1==null){ cmsPage.setPageId(null);//Add page primary key automatically generated by spring data cmsPageRepository.save(cmsPage); //Return result CmsPageResult cmsPageResult = new CmsPageResult(CommonCode.SUCCESS,cmsPage); return cmsPageResult; } return new CmsPageResult(CommonCode.FAIL,null); }

2.2.4 Controller

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

2 December 2019, 23:52 | Views: 6480

Add new comment

For adding a comment, please log in
or create account

0 comments