|
|
@@ -1,18 +1,182 @@
|
|
|
package com.goafanti.admin.controller;
|
|
|
|
|
|
+import java.text.ParseException;
|
|
|
+import java.util.Arrays;
|
|
|
+
|
|
|
import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.validation.Valid;
|
|
|
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.validation.BindingResult;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
+import com.goafanti.common.bo.Result;
|
|
|
+import com.goafanti.common.constant.AFTConstants;
|
|
|
+import com.goafanti.common.constant.ErrorConstants;
|
|
|
import com.goafanti.common.controller.BaseApiController;
|
|
|
+import com.goafanti.common.enums.AttachmentType;
|
|
|
+import com.goafanti.common.model.News;
|
|
|
+import com.goafanti.common.utils.DateUtils;
|
|
|
+import com.goafanti.common.utils.StringUtils;
|
|
|
+import com.goafanti.core.mybatis.JDBCIdGenerator;
|
|
|
+import com.goafanti.core.shiro.token.TokenManager;
|
|
|
+import com.goafanti.news.bo.InputNews;
|
|
|
+import com.goafanti.news.enums.NewsFields;
|
|
|
import com.goafanti.news.service.NewsService;
|
|
|
+
|
|
|
/**
|
|
|
* 新闻
|
|
|
*/
|
|
|
@RestController
|
|
|
@RequestMapping(value = "/api/admin/news")
|
|
|
-public class AdminNewsApiController extends BaseApiController{
|
|
|
+public class AdminNewsApiController extends BaseApiController {
|
|
|
@Resource
|
|
|
- private NewsService newsService;
|
|
|
+ private NewsService newsService;
|
|
|
+ @Autowired
|
|
|
+ private JDBCIdGenerator idGenerator;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新闻列表
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/list", method = RequestMethod.GET)
|
|
|
+ public Result list(Integer type, String title, String author, String startCreateTime, String endCreateTime,
|
|
|
+ String source, Integer hot, String pageSize, String pageNo) {
|
|
|
+ Result res = new Result();
|
|
|
+ if (null == type) {
|
|
|
+ res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "新闻类型"));
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+ Integer pNo = 1;
|
|
|
+ Integer pSize = 10;
|
|
|
+ if (StringUtils.isNumeric(pageSize)) {
|
|
|
+ pSize = Integer.parseInt(pageSize);
|
|
|
+ }
|
|
|
+ if (StringUtils.isNumeric(pageNo)) {
|
|
|
+ pNo = Integer.parseInt(pageNo);
|
|
|
+ }
|
|
|
+ res.setData(newsService.listNews(type, title, author, startCreateTime, endCreateTime, source, hot, pNo, pSize));
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/add", method = RequestMethod.POST)
|
|
|
+ public Result add(@Valid InputNews news, BindingResult bindingResult, String createTimeFormattedDate) {
|
|
|
+ Result res = new Result();
|
|
|
+ if (bindingResult.hasErrors()) {
|
|
|
+ res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
|
|
|
+ NewsFields.getFieldDesc(bindingResult.getFieldError().getField())));
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+ res = disposeInputNews(res, news, createTimeFormattedDate);
|
|
|
+ if (!res.getError().isEmpty()) {
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+ newsService.save(idGenerator.generateId(), (News) res.getData());
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/update", method = RequestMethod.POST)
|
|
|
+ public Result update(InputNews news, BindingResult bindingResult, String createTimeFormattedDate) {
|
|
|
+ Result res = new Result();
|
|
|
+ if (bindingResult.hasErrors()) {
|
|
|
+ res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
|
|
|
+ NewsFields.getFieldDesc(bindingResult.getFieldError().getField())));
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (null == news.getId()) {
|
|
|
+ res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "新闻Id"));
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ res = disposeInputNews(res, news, createTimeFormattedDate);
|
|
|
+ if (!res.getError().isEmpty()) {
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+ newsService.update(news.getId(), (News) res.getData());
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 详情
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/detail", method = RequestMethod.GET)
|
|
|
+ public Result detail(Long id) {
|
|
|
+ Result res = new Result();
|
|
|
+ if (null == id) {
|
|
|
+ res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "新闻Id"));
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+ res.setData(newsService.findNewsDetail(id));
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/delete", method = RequestMethod.POST)
|
|
|
+ public Result delete(@RequestParam(name = "ids[]", required = false) String[] ids) {
|
|
|
+ Result res = new Result();
|
|
|
+ if (ids == null || ids.length < 1) {
|
|
|
+ res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", ""));
|
|
|
+ } else {
|
|
|
+ res.setData(newsService.batchDeleteByPrimaryKey(Arrays.asList(ids)));
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新闻题图图片上传
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/upload", method = RequestMethod.POST)
|
|
|
+ public Result upload(HttpServletRequest req, String sign) {
|
|
|
+ Result res = new Result();
|
|
|
+ AttachmentType attachmentType = AttachmentType.getField(sign);
|
|
|
+ if (attachmentType == AttachmentType.NEWS_TITLE_PICTURE ||
|
|
|
+ attachmentType == AttachmentType.NEWS_CONTENT_PICTURE) {
|
|
|
+ res.setData(handleFiles(res, "/news/", false, req, sign, TokenManager.getAdminId()));
|
|
|
+ } else {
|
|
|
+ res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private Result disposeInputNews(Result res, InputNews news, String createTimeFormattedDate) {
|
|
|
+ if (StringUtils.isBlank(news.getTitle())) {
|
|
|
+ res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "新闻标题"));
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(createTimeFormattedDate)) {
|
|
|
+ res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "新闻时间"));
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (null == news.getType()) {
|
|
|
+ res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "新闻类型"));
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ News n = new News();
|
|
|
+ BeanUtils.copyProperties(news, n);
|
|
|
+ try {
|
|
|
+ n.setCreateTime(DateUtils.parseDate(createTimeFormattedDate, AFTConstants.YYYYMMDD));
|
|
|
+ } catch (ParseException e) {
|
|
|
+ }
|
|
|
+ res.setData(n);
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
}
|