|
|
@@ -1,15 +1,26 @@
|
|
|
package com.goafanti.admin.controller;
|
|
|
|
|
|
+import java.text.ParseException;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
import javax.annotation.Resource;
|
|
|
+import javax.validation.Valid;
|
|
|
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+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.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.model.News;
|
|
|
+import com.goafanti.common.utils.DateUtils;
|
|
|
import com.goafanti.common.utils.StringUtils;
|
|
|
+import com.goafanti.news.bo.InputNews;
|
|
|
+import com.goafanti.news.enums.NewsFields;
|
|
|
import com.goafanti.news.service.NewsService;
|
|
|
|
|
|
/**
|
|
|
@@ -20,15 +31,15 @@ import com.goafanti.news.service.NewsService;
|
|
|
public class AdminNewsApiController extends BaseApiController {
|
|
|
@Resource
|
|
|
private NewsService newsService;
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 新闻列表
|
|
|
*/
|
|
|
@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) {
|
|
|
+ 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){
|
|
|
+ if (null == type) {
|
|
|
res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "新闻类型"));
|
|
|
return res;
|
|
|
}
|
|
|
@@ -43,12 +54,76 @@ public class AdminNewsApiController extends BaseApiController {
|
|
|
res.setData(newsService.listNews(type, title, author, startCreateTime, endCreateTime, source, hot, pNo, pSize));
|
|
|
return res;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 新增
|
|
|
*/
|
|
|
- public Result add(){
|
|
|
+ @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(UUID.randomUUID().timestamp(), (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;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
}
|