Refactor GalleryService

pull/98/head
johnniang 2019-02-19 22:43:36 +08:00
parent 50a3e94347
commit 180bfc06b4
6 changed files with 23 additions and 88 deletions

View File

@ -1,11 +1,7 @@
package cc.ryanc.halo.service; package cc.ryanc.halo.service;
import cc.ryanc.halo.model.domain.Gallery; import cc.ryanc.halo.model.domain.Gallery;
import org.springframework.data.domain.Page; import cc.ryanc.halo.service.base.CrudService;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Optional;
/** /**
* <pre> * <pre>
@ -15,44 +11,6 @@ import java.util.Optional;
* @author : RYAN0UP * @author : RYAN0UP
* @date : 2018/2/26 * @date : 2018/2/26
*/ */
public interface GalleryService { public interface GalleryService extends CrudService<Gallery, Long> {
/**
*
*
* @param gallery gallery
* @return Gallery
*/
Gallery save(Gallery gallery);
/**
*
*
* @param galleryId galleryId
* @return Gallery
*/
Gallery remove(Long galleryId);
/**
*
*
* @param pageable pageable
* @return Page
*/
Page<Gallery> findAll(Pageable pageable);
/**
*
*
* @return List
*/
List<Gallery> findAll();
/**
*
*
* @param galleryId galleryId
* @return Optional
*/
Optional<Gallery> findByGalleryId(Long galleryId);
} }

View File

@ -9,7 +9,6 @@ import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional;
/** /**
* <pre> * <pre>

View File

@ -3,15 +3,12 @@ package cc.ryanc.halo.service.impl;
import cc.ryanc.halo.model.domain.Gallery; import cc.ryanc.halo.model.domain.Gallery;
import cc.ryanc.halo.repository.GalleryRepository; import cc.ryanc.halo.repository.GalleryRepository;
import cc.ryanc.halo.service.GalleryService; import cc.ryanc.halo.service.GalleryService;
import org.springframework.beans.factory.annotation.Autowired; import cc.ryanc.halo.service.base.AbstractCrudService;
import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
import java.util.Optional;
/** /**
* <pre> * <pre>
@ -22,12 +19,16 @@ import java.util.Optional;
* @date : 2018/2/26 * @date : 2018/2/26
*/ */
@Service @Service
public class GalleryServiceImpl implements GalleryService { public class GalleryServiceImpl extends AbstractCrudService<Gallery, Long> implements GalleryService {
private static final String GALLERIES_CACHE_NAME = "galleries"; private static final String GALLERIES_CACHE_NAME = "galleries";
@Autowired private final GalleryRepository galleryRepository;
private GalleryRepository galleryRepository;
public GalleryServiceImpl(GalleryRepository galleryRepository) {
super(galleryRepository);
this.galleryRepository = galleryRepository;
}
/** /**
* *
@ -37,8 +38,8 @@ public class GalleryServiceImpl implements GalleryService {
*/ */
@Override @Override
@CacheEvict(value = GALLERIES_CACHE_NAME, allEntries = true, beforeInvocation = true) @CacheEvict(value = GALLERIES_CACHE_NAME, allEntries = true, beforeInvocation = true)
public Gallery save(Gallery gallery) { public Gallery create(Gallery gallery) {
return galleryRepository.save(gallery); return super.create(gallery);
} }
/** /**
@ -49,21 +50,8 @@ public class GalleryServiceImpl implements GalleryService {
*/ */
@Override @Override
@CacheEvict(value = GALLERIES_CACHE_NAME, allEntries = true, beforeInvocation = true) @CacheEvict(value = GALLERIES_CACHE_NAME, allEntries = true, beforeInvocation = true)
public Gallery remove(Long galleryId) { public Gallery removeById(Long galleryId) {
final Optional<Gallery> gallery = this.findByGalleryId(galleryId); return super.removeById(galleryId);
galleryRepository.delete(gallery.get());
return gallery.get();
}
/**
*
*
* @param pageable pageable
* @return Page
*/
@Override
public Page<Gallery> findAll(Pageable pageable) {
return galleryRepository.findAll(pageable);
} }
/** /**
@ -73,18 +61,8 @@ public class GalleryServiceImpl implements GalleryService {
*/ */
@Override @Override
@Cacheable(value = GALLERIES_CACHE_NAME, key = "'gallery'") @Cacheable(value = GALLERIES_CACHE_NAME, key = "'gallery'")
public List<Gallery> findAll() { public List<Gallery> listAll() {
return galleryRepository.findAll(); return super.listAll();
} }
/**
*
*
* @param galleryId galleryId
* @return Optional
*/
@Override
public Optional<Gallery> findByGalleryId(Long galleryId) {
return galleryRepository.findById(galleryId);
}
} }

View File

@ -158,7 +158,7 @@ public class PageController {
@GetMapping(value = "/galleries") @GetMapping(value = "/galleries")
public String gallery(Model model, public String gallery(Model model,
@PageableDefault(size = 18, sort = "galleryId", direction = Sort.Direction.DESC) Pageable pageable) { @PageableDefault(size = 18, sort = "galleryId", direction = Sort.Direction.DESC) Pageable pageable) {
final Page<Gallery> galleries = galleryService.findAll(pageable); final Page<Gallery> galleries = galleryService.listAll(pageable);
model.addAttribute("galleries", galleries); model.addAttribute("galleries", galleries);
return "admin/admin_page_gallery"; return "admin/admin_page_gallery";
} }
@ -175,7 +175,7 @@ public class PageController {
if (StrUtil.isEmpty(gallery.getGalleryThumbnailUrl())) { if (StrUtil.isEmpty(gallery.getGalleryThumbnailUrl())) {
gallery.setGalleryThumbnailUrl(gallery.getGalleryUrl()); gallery.setGalleryThumbnailUrl(gallery.getGalleryUrl());
} }
galleryService.save(gallery); galleryService.create(gallery);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -191,7 +191,7 @@ public class PageController {
*/ */
@GetMapping(value = "/gallery") @GetMapping(value = "/gallery")
public String gallery(Model model, @RequestParam("galleryId") Long galleryId) { public String gallery(Model model, @RequestParam("galleryId") Long galleryId) {
final Optional<Gallery> gallery = galleryService.findByGalleryId(galleryId); final Optional<Gallery> gallery = galleryService.fetchById(galleryId);
model.addAttribute("gallery", gallery.orElse(new Gallery())); model.addAttribute("gallery", gallery.orElse(new Gallery()));
return "admin/widget/_gallery-detail"; return "admin/widget/_gallery-detail";
} }
@ -206,7 +206,7 @@ public class PageController {
@ResponseBody @ResponseBody
public JsonResult removeGallery(@RequestParam("galleryId") Long galleryId) { public JsonResult removeGallery(@RequestParam("galleryId") Long galleryId) {
try { try {
galleryService.remove(galleryId); galleryService.removeById(galleryId);
} catch (Exception e) { } catch (Exception e) {
log.error("Failed to delete image: {}", e.getMessage()); log.error("Failed to delete image: {}", e.getMessage());
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.delete-failed")); return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.delete-failed"));

View File

@ -54,7 +54,7 @@ public class ApiGalleryController {
*/ */
@GetMapping @GetMapping
public JsonResult galleries() { public JsonResult galleries() {
final List<Gallery> galleries = galleryService.findAll(); final List<Gallery> galleries = galleryService.listAll();
if (null != galleries && galleries.size() > 0) { if (null != galleries && galleries.size() > 0) {
return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), ResponseStatusEnum.SUCCESS.getMsg(), galleries); return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), ResponseStatusEnum.SUCCESS.getMsg(), galleries);
} else { } else {
@ -91,7 +91,7 @@ public class ApiGalleryController {
*/ */
@GetMapping(value = "/{id}") @GetMapping(value = "/{id}")
public JsonResult galleries(@PathVariable("id") Long id) { public JsonResult galleries(@PathVariable("id") Long id) {
final Optional<Gallery> gallery = galleryService.findByGalleryId(id); final Optional<Gallery> gallery = galleryService.fetchById(id);
if (gallery.isPresent()) { if (gallery.isPresent()) {
return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), ResponseStatusEnum.SUCCESS.getMsg(), gallery.get()); return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), ResponseStatusEnum.SUCCESS.getMsg(), gallery.get());
} else { } else {

View File

@ -50,7 +50,7 @@ public class FrontPageController extends BaseController {
*/ */
@GetMapping(value = "/gallery") @GetMapping(value = "/gallery")
public String gallery(Model model) { public String gallery(Model model) {
final List<Gallery> galleries = galleryService.findAll(); final List<Gallery> galleries = galleryService.listAll();
model.addAttribute("galleries", galleries); model.addAttribute("galleries", galleries);
return this.render("gallery"); return this.render("gallery");
} }