Commit 4be4da4b by 段启岩

CMS完成

parent d59d9d2c
package cn.meteor.beyondclouds.modules.content.api;
import cn.meteor.beyondclouds.common.form.PageForm;
import cn.meteor.beyondclouds.common.vo.PageVO;
import cn.meteor.beyondclouds.core.annotation.Anonymous;
import cn.meteor.beyondclouds.core.api.Response;
import cn.meteor.beyondclouds.modules.content.bean.ContentDetail;
import cn.meteor.beyondclouds.modules.content.entity.Content;
import cn.meteor.beyondclouds.modules.content.exception.ContentServiceException;
import cn.meteor.beyondclouds.modules.content.service.IContentService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
/**
* CMS 内容
* @author 段启岩
*/
@Api(tags = "CMS内容API")
@RestController
public class ContentApi {
private IContentService contentService;
@Autowired
public ContentApi(IContentService contentService) {
this.contentService = contentService;
}
/**
* 分页获取栏目下的内容列表
* @param channelId
* @param pageForm
* @param bindingResult
* @return
*/
@Anonymous
@ApiOperation("内容列表")
@GetMapping("/api/channel/{channelId}/contents")
public Response getContentPage(@PathVariable("channelId") Integer channelId,
@RequestParam("type") Integer contentType,
@Valid PageForm pageForm, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return Response.fieldError(bindingResult.getFieldError());
}
try {
IPage<Content> contentPage = contentService.getPageByChannelId(channelId, contentType, pageForm.getPage(), pageForm.getSize());
PageVO<Content> contentPageVO = new PageVO<>(contentPage);
return Response.success(contentPageVO);
} catch (ContentServiceException e) {
e.printStackTrace();
return Response.error(e);
}
}
@Anonymous
@ApiOperation("内容详情")
@GetMapping("/api/content/{contentId}")
public Response<ContentDetail> getContentDetail(@PathVariable("contentId") Integer contentId) {
try {
ContentDetail contentDetail = contentService.getContentDetail(contentId);
return Response.success(contentDetail);
} catch (ContentServiceException e) {
e.printStackTrace();
return Response.error(e);
}
}
}
package cn.meteor.beyondclouds.modules.content.bean;
import cn.meteor.beyondclouds.modules.content.entity.Content;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* @author meteor
* 内容详情
*/
@Data
public class ContentDetail extends Content {
@ApiModelProperty(value = "内容")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String content;
}
......@@ -2,6 +2,7 @@ package cn.meteor.beyondclouds.modules.content.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
......@@ -9,7 +10,7 @@ import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Date;
/**
* <p>
......@@ -40,29 +41,32 @@ public class Content implements Serializable {
private Integer contentType;
@ApiModelProperty(value = "内容标题")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String title;
@ApiModelProperty(value = "子标题")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String subTitle;
@ApiModelProperty(value = "访问链接")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String link;
@ApiModelProperty(value = "封面图")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String cover;
@ApiModelProperty(value = "图片1")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String pic1;
@ApiModelProperty(value = "图片2")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String pic2;
@ApiModelProperty(value = "内容")
private String content;
private Date createTime;
private LocalDateTime createTime;
private LocalDateTime updateTime;
private Date updateTime;
}
package cn.meteor.beyondclouds.modules.content.entity;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
......@@ -29,6 +30,7 @@ public class ContentExt implements Serializable {
private Integer contentId;
@ApiModelProperty(value = "内容")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String content;
......
package cn.meteor.beyondclouds.modules.content.enums;
/**
* 内容类型
* @author meteor
*/
public enum CmsContentType {
/**
* 幻灯
*/
SLIDE_SHOW,
/**
* 普通文章
*/
GENERAL_ARTICLE,
/**
* 广告
*/
ADVERTISEMENT;
public static CmsContentType valueOf(int ordinal) {
for (CmsContentType contentType:
values()) {
if (contentType.ordinal() == ordinal) {
return contentType;
}
}
return null;
}
}
package cn.meteor.beyondclouds.modules.content.enums;
import cn.meteor.beyondclouds.core.IErrorCode;
/**
* @author 段启岩
*/
public enum ContentErrorCode implements IErrorCode {
CONTENT_NOT_FOUND(8001,"不存在该内容"),
CONTENT_TYPE_ERROR(8002, "type错误, 可用的type = {0:幻灯, 1:普通文章, 2:广告}");
private long code;
private String msg;
ContentErrorCode(long code, String msg) {
this.code = code;
this.msg = msg;
}
@Override
public long code() {
return code;
}
@Override
public String msg() {
return msg;
}
}
package cn.meteor.beyondclouds.modules.content.exception;
import cn.meteor.beyondclouds.core.IErrorCode;
import cn.meteor.beyondclouds.core.exception.ServiceException;
/**
* 项目业务异常类
*/
public class ContentServiceException extends ServiceException {
public ContentServiceException(long errorCode, String errorMsg) {
super(errorCode, errorMsg);
}
public ContentServiceException(IErrorCode errorCode) {
super(errorCode);
}
}
package cn.meteor.beyondclouds.modules.content.service;
import cn.meteor.beyondclouds.modules.content.bean.ContentDetail;
import cn.meteor.beyondclouds.modules.content.entity.Content;
import cn.meteor.beyondclouds.modules.content.exception.ContentServiceException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
/**
......@@ -13,4 +16,20 @@ import com.baomidou.mybatisplus.extension.service.IService;
*/
public interface IContentService extends IService<Content> {
/**
* 根据栏目ID分页获取内容列表
* @param channelId
* @param contentType
* @param pageNumber
* @param pageSize
* @return
*/
IPage<Content> getPageByChannelId(Integer channelId, Integer contentType, Integer pageNumber, Integer pageSize) throws ContentServiceException;
/**
* 根据contentId获取内容详情
* @param contentId
* @return
*/
ContentDetail getContentDetail(Integer contentId) throws ContentServiceException;
}
package cn.meteor.beyondclouds.modules.content.service.impl;
import cn.meteor.beyondclouds.modules.content.bean.ContentDetail;
import cn.meteor.beyondclouds.modules.content.entity.Content;
import cn.meteor.beyondclouds.modules.content.entity.ContentExt;
import cn.meteor.beyondclouds.modules.content.enums.CmsContentType;
import cn.meteor.beyondclouds.modules.content.enums.ContentErrorCode;
import cn.meteor.beyondclouds.modules.content.exception.ContentServiceException;
import cn.meteor.beyondclouds.modules.content.mapper.ContentMapper;
import cn.meteor.beyondclouds.modules.content.service.IContentExtService;
import cn.meteor.beyondclouds.modules.content.service.IContentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
/**
* <p>
......@@ -17,4 +29,48 @@ import org.springframework.stereotype.Service;
@Service
public class ContentServiceImpl extends ServiceImpl<ContentMapper, Content> implements IContentService {
private IContentExtService contentExtService;
@Autowired
public ContentServiceImpl(IContentExtService contentExtService) {
this.contentExtService = contentExtService;
}
@Override
public IPage<Content> getPageByChannelId(Integer channelId, Integer contentType, Integer pageNumber, Integer pageSize) throws ContentServiceException {
Assert.notNull(channelId, "channelId must not be null");
if (CmsContentType.valueOf(contentType) == null) {
throw new ContentServiceException(ContentErrorCode.CONTENT_TYPE_ERROR);
}
IPage<Content> page = new Page<>(pageNumber, pageSize);
QueryWrapper<Content> contentQueryWrapper = new QueryWrapper<>();
contentQueryWrapper.eq("channel_id", channelId);
contentQueryWrapper.eq("content_type", contentType);
return page(page, contentQueryWrapper);
}
@Override
public ContentDetail getContentDetail(Integer contentId) throws ContentServiceException {
Assert.notNull(contentId, "contentId must not be null");
// 1. 获取content
Content content = getById(contentId);
if (null == content) {
throw new ContentServiceException(ContentErrorCode.CONTENT_NOT_FOUND);
}
// 2. 获取contentExt
ContentExt contentExt = contentExtService.getById(contentId);
// 3. 组合数据
ContentDetail contentDetail = new ContentDetail();
BeanUtils.copyProperties(content, contentDetail);
if (null != contentExt) {
contentDetail.setContent(contentExt.getContent());
}
return contentDetail;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment