Service层修改 单元测试

This commit is contained in:
禾几海
2020-05-25 23:04:41 +08:00
parent 4e383b6598
commit 73a1d3eadf
4 changed files with 29 additions and 38 deletions

View File

@@ -1,5 +1,6 @@
package cn.celess.blog.service;
import cn.celess.blog.entity.model.PageData;
import cn.celess.blog.entity.model.WebUpdateModel;
import com.github.pagehelper.PageInfo;
import net.sf.json.JSONObject;
@@ -45,7 +46,7 @@ public interface WebUpdateInfoService {
* @param page 数据页
* @return 分页数据
*/
PageInfo<WebUpdateModel> pages(int count, int page);
PageData<WebUpdateModel> pages(int count, int page);
/**
* 获取全部的更新记录

View File

@@ -2,12 +2,14 @@ package cn.celess.blog.service.serviceimpl;
import cn.celess.blog.enmu.ResponseEnum;
import cn.celess.blog.entity.WebUpdate;
import cn.celess.blog.entity.model.PageData;
import cn.celess.blog.entity.model.WebUpdateModel;
import cn.celess.blog.exception.MyException;
import cn.celess.blog.mapper.WebUpdateInfoMapper;
import cn.celess.blog.service.WebUpdateInfoService;
import cn.celess.blog.util.DateFormatUtil;
import cn.celess.blog.util.HttpUtil;
import cn.celess.blog.util.ModalTrans;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
@@ -39,11 +41,11 @@ public class WebUpdateInfoServiceImpl implements WebUpdateInfoService {
if (info == null || info.replaceAll(" ", "").isEmpty()) {
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
}
WebUpdate webUpdate = new WebUpdate(info, new Date());
WebUpdate webUpdate = new WebUpdate(info);
if (webUpdateInfoMapper.insert(webUpdate) == 0) {
throw new MyException(ResponseEnum.FAILURE);
}
return trans(webUpdate);
return ModalTrans.webUpdate(webUpdateInfoMapper.findById(webUpdate.getId()));
}
@Override
@@ -65,26 +67,20 @@ public class WebUpdateInfoServiceImpl implements WebUpdateInfoService {
}
webUpdate.setUpdateInfo(info);
webUpdateInfoMapper.update(id, info);
return trans(webUpdate);
return ModalTrans.webUpdate(webUpdate);
}
@Override
public PageInfo<WebUpdateModel> pages(int count, int page) {
public PageData<WebUpdateModel> pages(int count, int page) {
PageHelper.startPage(page, count);
List<WebUpdate> updateList = webUpdateInfoMapper.findAll();
PageInfo pageInfo = new PageInfo(updateList);
pageInfo.setList(list2List(updateList));
return pageInfo;
return new PageData<WebUpdateModel>(new PageInfo<WebUpdate>(updateList), list2List(updateList));
}
@Override
public List<WebUpdateModel> findAll() {
List<WebUpdate> all = webUpdateInfoMapper.findAll();
List<WebUpdateModel> webUpdateModels = new ArrayList<>();
for (WebUpdate w : all) {
webUpdateModels.add(trans(w));
}
return webUpdateModels;
return list2List(all);
}
@Override
@@ -101,7 +97,7 @@ public class WebUpdateInfoServiceImpl implements WebUpdateInfoService {
SimpleDateFormat sdf = new SimpleDateFormat();
Instant parse = Instant.parse(commit.getJSONObject("committer").getString("date"));
jsonObject.put("committerDate", DateFormatUtil.get(Date.from(parse)));
jsonObject.put("commitUrl", "https://github.com/xiaohai2271/blog-frontEnd/tree/"+object.getString("sha"));
jsonObject.put("commitUrl", "https://github.com/xiaohai2271/blog-frontEnd/tree/" + object.getString("sha"));
} catch (IOException e) {
log.info("网络请求失败{}", e.getMessage());
}
@@ -110,14 +106,7 @@ public class WebUpdateInfoServiceImpl implements WebUpdateInfoService {
private List<WebUpdateModel> list2List(List<WebUpdate> webUpdates) {
List<WebUpdateModel> webUpdateModels = new ArrayList<>();
for (WebUpdate w : webUpdates) {
webUpdateModels.add(trans(w));
}
webUpdates.forEach(update -> webUpdateModels.add(ModalTrans.webUpdate(update)));
return webUpdateModels;
}
private WebUpdateModel trans(WebUpdate webUpdate) {
return new WebUpdateModel(webUpdate.getId(), webUpdate.getUpdateInfo(), DateFormatUtil.get(webUpdate.getUpdateTime()));
}
}

View File

@@ -1,13 +1,7 @@
package cn.celess.blog.util;
import cn.celess.blog.entity.Article;
import cn.celess.blog.entity.Category;
import cn.celess.blog.entity.Tag;
import cn.celess.blog.entity.User;
import cn.celess.blog.entity.model.ArticleModel;
import cn.celess.blog.entity.model.CategoryModel;
import cn.celess.blog.entity.model.TagModel;
import cn.celess.blog.entity.model.UserModel;
import cn.celess.blog.entity.*;
import cn.celess.blog.entity.model.*;
import org.springframework.beans.BeanUtils;
/**
@@ -61,4 +55,13 @@ public class ModalTrans {
BeanUtils.copyProperties(tag, model);
return model;
}
public static WebUpdateModel webUpdate(WebUpdate update) {
WebUpdateModel model = new WebUpdateModel();
model.setId(update.getId());
model.setInfo(update.getUpdateInfo());
model.setTime(DateFormatUtil.get(update.getUpdateTime()));
return model;
}
}