| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- 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 {
- @Resource
- 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;
- }
- }
|