Refactor AttachmentService and CategoryService

pull/98/head
johnniang 2019-02-19 22:27:28 +08:00
parent 6577d062dc
commit cf4c491797
14 changed files with 56 additions and 152 deletions

View File

@ -47,7 +47,7 @@ public class CommonTagDirective implements TemplateDirectiveModel {
environment.setVariable("menus", builder.build().wrap(menuService.findAll()));
break;
case "categories":
environment.setVariable("categories", builder.build().wrap(categoryService.findAll()));
environment.setVariable("categories", builder.build().wrap(categoryService.listAll()));
break;
case "tags":
environment.setVariable("tags", builder.build().wrap(tagService.findAll()));

View File

@ -1,6 +1,7 @@
package cc.ryanc.halo.service;
import cc.ryanc.halo.model.domain.Attachment;
import cc.ryanc.halo.service.base.CrudService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.multipart.MultipartFile;
@ -18,46 +19,7 @@ import java.util.Optional;
* @author : RYAN0UP
* @date : 2018/1/10
*/
public interface AttachmentService {
/**
*
*
* @param attachment attachment
* @return Attachment
*/
Attachment save(Attachment attachment);
/**
*
*
* @return List
*/
List<Attachment> findAll();
/**
*
*
* @param pageable pageable
* @return Page
*/
Page<Attachment> findAll(Pageable pageable);
/**
*
*
* @param attachId attachId
* @return Attachment
*/
Optional<Attachment> findByAttachId(Long attachId);
/**
*
*
* @param attachId attachId
* @return Attachment
*/
Attachment remove(Long attachId);
public interface AttachmentService extends CrudService<Attachment, Long> {
/**
*
@ -111,10 +73,4 @@ public interface AttachmentService {
*/
boolean deleteUpYunAttachment(String fileName);
/**
*
*
* @return Long
*/
Long getCount();
}

View File

@ -1,6 +1,7 @@
package cc.ryanc.halo.service;
import cc.ryanc.halo.model.domain.Category;
import cc.ryanc.halo.service.base.CrudService;
import java.util.List;
import java.util.Optional;
@ -13,38 +14,7 @@ import java.util.Optional;
* @author : RYAN0UP
* @date : 2017/11/30
*/
public interface CategoryService {
/**
* /
*
* @param category
* @return
*/
Category save(Category category);
/**
*
*
* @param cateId
* @return category
*/
Category remove(Long cateId);
/**
*
*
* @return List
*/
List<Category> findAll();
/**
*
*
* @param cateId
* @return category
*/
Optional<Category> findByCateId(Long cateId);
public interface CategoryService extends CrudService<Category, Long> {
/**
*

View File

@ -112,6 +112,11 @@ public abstract class AbstractCrudService<DOMAIN, ID> implements CrudService<DOM
}
}
@Override
public long count() {
return repository.count();
}
@Override
public DOMAIN create(DOMAIN domain) {
Assert.notNull(domain, domainName + " data must not be null");

View File

@ -8,7 +8,6 @@ import org.springframework.lang.Nullable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
@ -50,6 +49,7 @@ public interface CrudService<DOMAIN, ID> {
void mustExistById(@NonNull ID id);
long count();
// **************** Create
@NonNull

View File

@ -6,6 +6,7 @@ import cc.ryanc.halo.model.enums.AttachLocationEnum;
import cc.ryanc.halo.model.enums.BlogPropertiesEnum;
import cc.ryanc.halo.repository.AttachmentRepository;
import cc.ryanc.halo.service.AttachmentService;
import cc.ryanc.halo.service.base.AbstractCrudService;
import cc.ryanc.halo.utils.HaloUtils;
import cc.ryanc.halo.utils.Md5Util;
import cn.hutool.core.date.DateUtil;
@ -25,7 +26,6 @@ import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import com.upyun.UpException;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
@ -52,12 +52,16 @@ import static cc.ryanc.halo.model.dto.HaloConst.OPTIONS;
* @date : 2018/1/10
*/
@Service
public class AttachmentServiceImpl implements AttachmentService {
public class AttachmentServiceImpl extends AbstractCrudService<Attachment, Long> implements AttachmentService {
private static final String ATTACHMENTS_CACHE_NAME = "attachments";
@Autowired
private AttachmentRepository attachmentRepository;
private final AttachmentRepository attachmentRepository;
public AttachmentServiceImpl(AttachmentRepository attachmentRepository) {
super(attachmentRepository);
this.attachmentRepository = attachmentRepository;
}
/**
*
@ -67,8 +71,8 @@ public class AttachmentServiceImpl implements AttachmentService {
*/
@Override
@CacheEvict(value = ATTACHMENTS_CACHE_NAME, allEntries = true, beforeInvocation = true)
public Attachment save(Attachment attachment) {
return attachmentRepository.save(attachment);
public Attachment create(Attachment attachment) {
return super.create(attachment);
}
/**
@ -78,8 +82,8 @@ public class AttachmentServiceImpl implements AttachmentService {
*/
@Override
@Cacheable(value = ATTACHMENTS_CACHE_NAME, key = "'attachment'")
public List<Attachment> findAll() {
return attachmentRepository.findAll();
public List<Attachment> listAll() {
return super.listAll();
}
/**
@ -89,7 +93,7 @@ public class AttachmentServiceImpl implements AttachmentService {
* @return Page
*/
@Override
public Page<Attachment> findAll(Pageable pageable) {
public Page<Attachment> listAll(Pageable pageable) {
return attachmentRepository.findAll(pageable);
}
@ -100,7 +104,7 @@ public class AttachmentServiceImpl implements AttachmentService {
* @return Optional
*/
@Override
public Optional<Attachment> findByAttachId(Long attachId) {
public Optional<Attachment> fetchById(Long attachId) {
return attachmentRepository.findById(attachId);
}
@ -112,10 +116,8 @@ public class AttachmentServiceImpl implements AttachmentService {
*/
@Override
@CacheEvict(value = ATTACHMENTS_CACHE_NAME, allEntries = true, beforeInvocation = true)
public Attachment remove(Long attachId) {
Optional<Attachment> attachment = this.findByAttachId(attachId);
attachmentRepository.delete(attachment.orElse(null));
return attachment.orElse(null);
public Attachment removeById(Long attachId) {
return super.removeById(attachId);
}
/**
@ -406,13 +408,4 @@ public class AttachmentServiceImpl implements AttachmentService {
return flag;
}
/**
*
*
* @return Long
*/
@Override
public Long getCount() {
return attachmentRepository.count();
}
}

View File

@ -3,7 +3,7 @@ package cc.ryanc.halo.service.impl;
import cc.ryanc.halo.model.domain.Category;
import cc.ryanc.halo.repository.CategoryRepository;
import cc.ryanc.halo.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import cc.ryanc.halo.service.base.AbstractCrudService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
@ -20,12 +20,16 @@ import java.util.Optional;
* @date : 2017/11/30
*/
@Service
public class CategoryServiceImpl implements CategoryService {
public class CategoryServiceImpl extends AbstractCrudService<Category, Long> implements CategoryService {
private static final String POSTS_CACHE_NAME = "posts";
@Autowired
private CategoryRepository categoryRepository;
private final CategoryRepository categoryRepository;
public CategoryServiceImpl(CategoryRepository categoryRepository) {
super(categoryRepository);
this.categoryRepository = categoryRepository;
}
/**
* /
@ -35,8 +39,8 @@ public class CategoryServiceImpl implements CategoryService {
*/
@Override
@CacheEvict(value = POSTS_CACHE_NAME, allEntries = true, beforeInvocation = true)
public Category save(Category category) {
return categoryRepository.save(category);
public Category create(Category category) {
return super.create(category);
}
/**
@ -47,31 +51,8 @@ public class CategoryServiceImpl implements CategoryService {
*/
@Override
@CacheEvict(value = POSTS_CACHE_NAME, allEntries = true, beforeInvocation = true)
public Category remove(Long cateId) {
final Optional<Category> category = this.findByCateId(cateId);
categoryRepository.delete(category.orElse(null));
return category.orElse(null);
}
/**
*
*
* @return List
*/
@Override
public List<Category> findAll() {
return categoryRepository.findAll();
}
/**
*
*
* @param cateId
* @return Category
*/
@Override
public Optional<Category> findByCateId(Long cateId) {
return categoryRepository.findById(cateId);
public Category removeById(Long cateId) {
return super.removeById(cateId);
}
/**
@ -108,10 +89,9 @@ public class CategoryServiceImpl implements CategoryService {
return null;
}
final List<Category> categories = new ArrayList<>();
Optional<Category> category = null;
for (String str : strings) {
category = findByCateId(Long.parseLong(str));
categories.add(category.get());
// TODO There maybe cause NoSuchElementException
categories.add(fetchById(Long.parseLong(str)).get());
}
return categories;
}

View File

@ -100,7 +100,7 @@ public class AdminController extends BaseController {
model.addAttribute("comments", comments);
//附件数量
model.addAttribute("mediaCount", attachmentService.getCount());
model.addAttribute("mediaCount", attachmentService.count());
//文章阅读总数
final Long postViewsSum = postService.getPostViews();
@ -316,7 +316,7 @@ public class AdminController extends BaseController {
category.setCateName(ele);
category.setCateUrl(ele);
category.setCateDesc(ele);
category = categoryService.save(category);
category = categoryService.create(category);
}
categories.add(category);
}

View File

@ -60,7 +60,7 @@ public class AttachmentController {
@GetMapping
public String attachments(Model model,
@PageableDefault(size = 18, sort = "attachId", direction = Sort.Direction.DESC) Pageable pageable) {
final Page<Attachment> attachments = attachmentService.findAll(pageable);
final Page<Attachment> attachments = attachmentService.listAll(pageable);
model.addAttribute("attachments", attachments);
return "admin/admin_attachment";
}
@ -76,7 +76,7 @@ public class AttachmentController {
@PageableDefault(size = 18, sort = "attachId", direction = Sort.Direction.DESC) Pageable pageable,
@RequestParam(value = "id", defaultValue = "none") String id,
@RequestParam(value = "type", defaultValue = "normal") String type) {
final Page<Attachment> attachments = attachmentService.findAll(pageable);
final Page<Attachment> attachments = attachmentService.listAll(pageable);
model.addAttribute("attachments", attachments);
model.addAttribute("id", id);
if (StrUtil.equals(type, PostTypeEnum.POST_TYPE_POST.getDesc())) {
@ -127,7 +127,7 @@ public class AttachmentController {
attachment.setAttachSize(resultMap.get("size"));
attachment.setAttachWh(resultMap.get("wh"));
attachment.setAttachLocation(resultMap.get("location"));
attachmentService.save(attachment);
attachmentService.create(attachment);
log.info("Upload file {} to {} successfully", resultMap.get("fileName"), resultMap.get("filePath"));
result.put("success", ResultCodeEnum.SUCCESS.getCode());
result.put("message", localeMessageUtil.getMessage("code.admin.attachment.upload-success"));
@ -154,7 +154,7 @@ public class AttachmentController {
*/
@GetMapping(value = "/attachment")
public String attachmentDetail(Model model, @RequestParam("attachId") Long attachId) {
final Optional<Attachment> attachment = attachmentService.findByAttachId(attachId);
final Optional<Attachment> attachment = attachmentService.fetchById(attachId);
model.addAttribute("attachment", attachment.orElse(new Attachment()));
return "admin/widget/_attachment-detail";
}
@ -170,7 +170,7 @@ public class AttachmentController {
@ResponseBody
public JsonResult removeAttachment(@RequestParam("attachId") Long attachId,
HttpServletRequest request) {
final Attachment attachment = attachmentService.findByAttachId(attachId).orElse(new Attachment());
final Attachment attachment = attachmentService.fetchById(attachId).orElse(new Attachment());
final String attachLocation = attachment.getAttachLocation();
final String attachName = attachment.getAttachName();
final String attachPath = attachment.getAttachPath();
@ -200,7 +200,7 @@ public class AttachmentController {
}
}
if (flag) {
attachmentService.remove(attachId);
attachmentService.removeById(attachId);
log.info("Delete file {} successfully!", attachName);
logsService.save(LogsRecord.REMOVE_FILE, attachName, request);
} else {

View File

@ -69,7 +69,7 @@ public class CategoryController {
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.url-is-exists"));
}
}
category = categoryService.save(category);
category = categoryService.create(category);
if (null == category) {
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.save-failed"));
}
@ -85,7 +85,7 @@ public class CategoryController {
@GetMapping(value = "/remove")
public String removeCategory(@RequestParam("cateId") Long cateId) {
try {
categoryService.remove(cateId);
categoryService.removeById(cateId);
} catch (Exception e) {
log.error("Delete category failed: {}", e.getMessage());
}
@ -101,7 +101,7 @@ public class CategoryController {
*/
@GetMapping(value = "/edit")
public String toEditCategory(Model model, @RequestParam("cateId") Long cateId) {
final Optional<Category> category = categoryService.findByCateId(cateId);
final Optional<Category> category = categoryService.fetchById(cateId);
model.addAttribute("updateCategory", category.orElse(new Category()));
return "admin/admin_category";
}

View File

@ -50,7 +50,7 @@ public class ApiCategoryController {
*/
@GetMapping
public JsonResult categories() {
List<Category> categories = categoryService.findAll();
List<Category> categories = categoryService.listAll();
if (null != categories && categories.size() > 0) {
return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), ResponseStatusEnum.SUCCESS.getMsg(), categories);
} else {

View File

@ -263,7 +263,7 @@ public class ApiMetaWeBlog {
*/
private String buildCategories() throws Exception {
final StrBuilder strBuilder = new StrBuilder();
final List<Category> categories = categoryService.findAll();
final List<Category> categories = categoryService.listAll();
for (Category category : categories) {
final String cateName = category.getCateName();
final Long cateId = category.getCateId();

View File

@ -123,7 +123,7 @@ public class InstallController {
category.setCateName("未分类");
category.setCateUrl("default");
category.setCateDesc("未分类");
categoryService.save(category);
categoryService.create(category);
//第一篇文章
final Post post = new Post();

View File

@ -51,7 +51,7 @@ public class FrontCategoryController extends BaseController {
*/
@GetMapping
public String categories(Model model) {
final List<Category> categories = categoryService.findAll();
final List<Category> categories = categoryService.listAll();
model.addAttribute("categories", categories);
return this.render("categories");
}