Antiloveg 8 years ago
parent
commit
48eceb7049

+ 2 - 1
src/main/java/com/goafanti/admin/controller/AdminLectureApiController.java

@@ -20,6 +20,7 @@ import com.goafanti.common.constant.ErrorConstants;
 import com.goafanti.common.controller.BaseApiController;
 import com.goafanti.common.enums.AttachmentType;
 import com.goafanti.common.enums.LectureDynamicType;
+import com.goafanti.common.enums.LectureFields;
 import com.goafanti.common.enums.LectureHotType;
 import com.goafanti.common.enums.PatentInfoFields;
 import com.goafanti.common.model.Lecture;
@@ -92,7 +93,7 @@ public class AdminLectureApiController extends BaseApiController {
 		Result res = new Result();
 		if (bindingResult.hasErrors()) {
 			res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
-					PatentInfoFields.getFieldDesc(bindingResult.getFieldError().getField())));
+					LectureFields.getFieldDesc(bindingResult.getFieldError().getField())));
 			return res;
 		}
 

+ 81 - 6
src/main/java/com/goafanti/admin/controller/AdminNewsApiController.java

@@ -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;
+	}
+
 }

+ 57 - 0
src/main/java/com/goafanti/common/enums/LectureFields.java

@@ -0,0 +1,57 @@
+package com.goafanti.common.enums;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public enum LectureFields {
+	ID("id", "科技讲堂ID"),
+	UID("uid", "用户ID"),
+	NAME("name", "讲堂名称"),
+	SUMMARY("summary", "简介"),
+	
+	LECTUREURL("lectureUrl", "讲堂URL"),
+	HOT("hot", "大咖说页面展示标记"),
+	DYNAMIC("dynamic", "科技明星页面展示标记"),
+	
+	OTHER("", "未知参数");
+	
+	private String	code;
+	private String	desc;
+	
+	private static Map<String, LectureFields> status = new HashMap<String, LectureFields>();
+	
+	private LectureFields(String code, String desc) {
+		this.code = code;
+		this.desc = desc;
+	}
+	
+	static {
+		for (LectureFields value : LectureFields.values()) {
+			status.put(value.getCode(), value);
+		}
+	}
+	
+	public static LectureFields getField(String code) {
+		if (containsType(code)) {
+			return status.get(code);
+		}
+		return OTHER;
+	}
+	
+	public static String getFieldDesc(String code) {
+		return getField(code).getDesc();
+	}
+
+	public static boolean containsType(String code) {
+		return status.containsKey(code);
+	}
+	
+	public String getCode() {
+		return code;
+	}
+
+	public String getDesc() {
+		return desc;
+	}
+
+}

+ 123 - 0
src/main/java/com/goafanti/news/bo/InputNews.java

@@ -0,0 +1,123 @@
+package com.goafanti.news.bo;
+
+import javax.validation.constraints.Max;
+import javax.validation.constraints.Min;
+import javax.validation.constraints.Size;
+
+import com.goafanti.common.constant.ErrorConstants;
+
+public class InputNews {
+	
+	@Max(value = 999999999999999999L, message = "{" + ErrorConstants.PARAM_ERROR + "}")
+	@Min(value = 0, message = "{" + ErrorConstants.PARAM_ERROR + "}")
+	private Long	id;
+	
+	@Size(min = 0, max = 45, message = "{" + ErrorConstants.PARAM_SIZE_ERROR + "}")
+	private String	title;
+	
+	@Size(min = 0, max = 128, message = "{" + ErrorConstants.PARAM_SIZE_ERROR + "}")
+	private String	titleImg;
+	
+	@Size(min = 0, max = 45, message = "{" + ErrorConstants.PARAM_SIZE_ERROR + "}")
+	private String	author;
+	
+	@Max(value = 99, message = "{" + ErrorConstants.PARAM_ERROR + "}")
+	@Min(value = 0, message = "{" + ErrorConstants.PARAM_ERROR + "}")
+	private Integer	type;
+	
+	@Max(value = 1, message = "{" + ErrorConstants.PARAM_ERROR + "}")
+	@Min(value = 0, message = "{" + ErrorConstants.PARAM_ERROR + "}")
+	private Integer	hot;
+	
+	@Size(min = 0, max = 45, message = "{" + ErrorConstants.PARAM_SIZE_ERROR + "}")
+	private String	source;
+	
+	@Size(min = 0, max = 255, message = "{" + ErrorConstants.PARAM_SIZE_ERROR + "}")
+	private String	sourceUrl;
+	
+	@Size(min = 0, max = 144, message = "{" + ErrorConstants.PARAM_SIZE_ERROR + "}")
+	private String	summary;
+	
+	private String	content;
+
+	public Long getId() {
+		return id;
+	}
+
+	public void setId(Long id) {
+		this.id = id;
+	}
+
+	public String getTitle() {
+		return title;
+	}
+
+	public void setTitle(String title) {
+		this.title = title;
+	}
+
+	public String getTitleImg() {
+		return titleImg;
+	}
+
+	public void setTitleImg(String titleImg) {
+		this.titleImg = titleImg;
+	}
+
+	public String getAuthor() {
+		return author;
+	}
+
+	public void setAuthor(String author) {
+		this.author = author;
+	}
+
+	public Integer getType() {
+		return type;
+	}
+
+	public void setType(Integer type) {
+		this.type = type;
+	}
+
+	public Integer getHot() {
+		return hot;
+	}
+
+	public void setHot(Integer hot) {
+		this.hot = hot;
+	}
+
+	public String getSource() {
+		return source;
+	}
+
+	public void setSource(String source) {
+		this.source = source;
+	}
+
+	public String getSourceUrl() {
+		return sourceUrl;
+	}
+
+	public void setSourceUrl(String sourceUrl) {
+		this.sourceUrl = sourceUrl;
+	}
+
+	public String getSummary() {
+		return summary;
+	}
+
+	public void setSummary(String summary) {
+		this.summary = summary;
+	}
+
+	public String getContent() {
+		return content;
+	}
+
+	public void setContent(String content) {
+		this.content = content;
+	}
+
+}

+ 60 - 0
src/main/java/com/goafanti/news/enums/NewsFields.java

@@ -0,0 +1,60 @@
+package com.goafanti.news.enums;
+
+import java.util.HashMap;
+import java.util.Map;
+
+
+public enum NewsFields {
+	
+	ID("id", "科技讲堂ID"),
+	TITLE("title", "标题"),
+	TITLEIMG("titleImg", "题图url"),
+	AUTHOR("author", "作者"),
+	TYPE("type", "类型"),
+	HOT("hot", "大咖说页面展示标记"),
+	SOURCE("source", "来源"),
+	SOURCEURL("sourceUrl", "来源url"),
+	SUMMARY("summary", "简介"),
+	CONTENT("content", "内容"),
+	OTHER("", "未知参数");
+	
+	private String	code;
+	private String	desc;
+	
+	private static Map<String, NewsFields> status = new HashMap<String, NewsFields>();
+	
+	private NewsFields(String code, String desc) {
+		this.code = code;
+		this.desc = desc;
+	}
+	
+	static {
+		for (NewsFields value : NewsFields.values()) {
+			status.put(value.getCode(), value);
+		}
+	}
+	
+	public static NewsFields getField(String code) {
+		if (containsType(code)) {
+			return status.get(code);
+		}
+		return OTHER;
+	}
+	
+	public static String getFieldDesc(String code) {
+		return getField(code).getDesc();
+	}
+
+	public static boolean containsType(String code) {
+		return status.containsKey(code);
+	}
+	
+	public String getCode() {
+		return code;
+	}
+
+	public String getDesc() {
+		return desc;
+	}
+
+}

+ 9 - 0
src/main/java/com/goafanti/news/service/NewsService.java

@@ -62,6 +62,13 @@ public class NewsService extends BaseMybatisDao<NewsMapper> {
 		newsMapper.insert(news);
 		return news;
 	}
+	
+	@CachePut(value = "NewsCache", key = "'News:'+#id")
+	public News update(long id, News news) {
+		news.setId(id);
+		newsMapper.updateByPrimaryKeyWithBLOBs(news);
+		return news;
+	}
 
 	@SuppressWarnings("unchecked")
 	public Pagination<News> listNews(Integer type, String title, String author, String startCreateTime,
@@ -111,4 +118,6 @@ public class NewsService extends BaseMybatisDao<NewsMapper> {
 		return (Pagination<News>) findPage("findNewsListByPage", "findNewsCount", params, pNo, pSize);
 	}
 
+	
+
 }