Authored by 张帅

后台发布文章

package com.yohobuy.platform.grass.restapi;
import com.yohobuy.platform.common.exception.PlatformException;
import com.yohobuy.platform.grass.service.IGrassArticleService;
import com.yohobuy.platform.model.common.ApiResponse;
import com.yohobuy.platform.model.grass.request.GrassArticleReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
... ... @@ -12,9 +17,17 @@ public class GrassArticleController {
private static final Logger logger = LoggerFactory.getLogger(GrassArticleController.class);
@RequestMapping("/publishArticle")
public ApiResponse publishArticle(){
@Autowired
IGrassArticleService grassArticleService;
@RequestMapping("/publishArticle")
public ApiResponse publishArticle(@RequestBody GrassArticleReq req){
logger.info("grassArticle publishArticle begin, req is {}", req);
try {
grassArticleService.publishArticle(req);
} catch (PlatformException e) {
return new ApiResponse.ApiResponseBuilder().code(e.getCode()).message(e.getMessage()).build();
}
return new ApiResponse.ApiResponseBuilder().build();
}
... ...
package com.yohobuy.platform.grass.service;
import com.yohobuy.platform.common.exception.PlatformException;
import com.yohobuy.platform.model.grass.request.GrassArticleReq;
/**
*/
public interface IGrassArticleService {
void publishArticle(GrassArticleReq req) throws PlatformException;
}
... ...
package com.yohobuy.platform.grass.service.impl;
import com.google.common.collect.Lists;
import com.yohobuy.platform.common.exception.PlatformException;
import com.yohobuy.platform.common.util.DateUtil;
import com.yohobuy.platform.dal.grass.IGrassArticleDao;
import com.yohobuy.platform.dal.grass.IGrassArticleImageDao;
import com.yohobuy.platform.dal.grass.IGrassArticleSknDao;
import com.yohobuy.platform.dal.grass.model.GrassArticle;
import com.yohobuy.platform.dal.grass.model.GrassArticleImage;
import com.yohobuy.platform.dal.grass.model.GrassArticleSkn;
import com.yohobuy.platform.grass.service.IGrassArticleService;
import com.yohobuy.platform.model.grass.request.GrassArticleReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
/**
*/
@Component
public class GrassArticleServiceImpl implements IGrassArticleService{
private static final Logger logger = LoggerFactory.getLogger(GrassArticleServiceImpl.class);
@Resource
private IGrassArticleDao grassArticleDao;
@Resource
private IGrassArticleImageDao grassArticleImageDao;
@Resource
private IGrassArticleSknDao grassArticleSknDao;
@Override
public void publishArticle(GrassArticleReq req) throws PlatformException {
logger.info("enter publishArticle, req is {}", req);
publishCheck(req);
GrassArticle article = new GrassArticle();
article.setAuthorUid(req.getArticleUid());
article.setArticleContent(req.getContent());
article.setPublishTime(DateUtil.getCurrentTimeSeconds());
article.setTopicId(req.getTopicId());
try{
grassArticleDao.insertArticleSeletive(article);
logger.info("insert grass article success, uid is {}, ariticle id is {}", article.getAuthorUid(), article.getId());
}catch (Exception e){
logger.warn("insert grass article fail, uid is {}, e is {}",req.getArticleUid(), e );
throw new PlatformException("publish article failed", 402);
}
try{
List<GrassArticleImage> imageList = Lists.newArrayList();
for (String img: req.getArticleImages()) {
GrassArticleImage image = new GrassArticleImage();
image.setArticleId(article.getGuangArticleId());
image.setImgSrc(img);
image.setCreateTime(DateUtil.getCurrentTimeSeconds());
imageList.add(image);
}
grassArticleImageDao.insertArticleImgs(imageList);
logger.info("insert grass article images success, ariticle id is {}, imageList size is {}", article.getId(), imageList.size());
}catch (Exception e){
logger.warn("insert article images fail,ariticle id is {}, e is {}",article.getId(), e );
}
try{
List<GrassArticleSkn> sknList = Lists.newArrayList();
for (String skn: req.getArticleSkns()) {
GrassArticleSkn grassArticleSkn = new GrassArticleSkn();
grassArticleSkn.setArticleId(article.getGuangArticleId());
grassArticleSkn.setProductSkn(Integer.valueOf(skn));
grassArticleSkn.setCreateTime(DateUtil.getCurrentTimeSeconds());
sknList.add(grassArticleSkn);
}
grassArticleSknDao.insertArticleSkns(sknList);
logger.info("insert grass article skns success, ariticle id is {}, sknList size is {}", article.getId(), sknList.size());
}catch (Exception e){
logger.warn("insert article skns fail,ariticle id is {}, e is {}",article.getId(), e );
}
}
private void publishCheck(GrassArticleReq req) throws PlatformException {
if(req.getArticleUid() ==null){
logger.info("publishCheck uid is null");
throw new PlatformException("publish uid is null", 403);
}
if(req.getArticleImages() ==null || req.getArticleImages().size()< 1){
logger.info("publishCheck images is null");
throw new PlatformException("publish images is null", 403);
}
if(req.getArticleSkns() ==null || req.getArticleSkns().size()< 1){
logger.info("publishCheck skn is null");
throw new PlatformException("publish skn is null", 403);
}
}
}
... ...
... ... @@ -24,7 +24,7 @@
<dependency>
<groupId>com.yoho.service.platform.model</groupId>
<artifactId>platform-service-model</artifactId>
<version>1.2.3-6.8.4-SNAPSHOT</version>
<version>1.2.3-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.yoho.dsf.yhplatform</groupId>
... ...
... ... @@ -326,6 +326,9 @@ datasources:
- com.yohobuy.platform.dal.guang.IPlustarCategoryDAO
- com.yohobuy.platform.dal.guang.IRelationshipImageDAO
- com.yohobuy.platform.dal.guang.ResourceTabMapper
- com.yohobuy.platform.dal.grass.IGrassArticleDao
- com.yohobuy.platform.dal.grass.IGrassArticleImageDao
- com.yohobuy.platform.dal.grass.IGrassArticleSknDao
yhb_promotion:
servers:
... ...
... ... @@ -325,6 +325,9 @@ datasources:
- com.yohobuy.platform.dal.guang.IPlustarCategoryDAO
- com.yohobuy.platform.dal.guang.IRelationshipImageDAO
- com.yohobuy.platform.dal.guang.ResourceTabMapper
- com.yohobuy.platform.dal.grass.IGrassArticleDao
- com.yohobuy.platform.dal.grass.IGrassArticleImageDao
- com.yohobuy.platform.dal.grass.IGrassArticleSknDao
yhb_promotion:
servers:
... ...