mirror of https://github.com/halo-dev/halo
Refactor GalleryService
parent
50a3e94347
commit
180bfc06b4
|
@ -1,11 +1,7 @@
|
|||
package cc.ryanc.halo.service;
|
||||
|
||||
import cc.ryanc.halo.model.domain.Gallery;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import cc.ryanc.halo.service.base.CrudService;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
|
@ -15,44 +11,6 @@ import java.util.Optional;
|
|||
* @author : RYAN0UP
|
||||
* @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);
|
||||
}
|
||||
|
|
|
@ -9,7 +9,6 @@ import org.springframework.stereotype.Service;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
|
|
|
@ -3,15 +3,12 @@ package cc.ryanc.halo.service.impl;
|
|||
import cc.ryanc.halo.model.domain.Gallery;
|
||||
import cc.ryanc.halo.repository.GalleryRepository;
|
||||
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.Cacheable;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
|
@ -22,12 +19,16 @@ import java.util.Optional;
|
|||
* @date : 2018/2/26
|
||||
*/
|
||||
@Service
|
||||
public class GalleryServiceImpl implements GalleryService {
|
||||
public class GalleryServiceImpl extends AbstractCrudService<Gallery, Long> implements GalleryService {
|
||||
|
||||
private static final String GALLERIES_CACHE_NAME = "galleries";
|
||||
|
||||
@Autowired
|
||||
private GalleryRepository galleryRepository;
|
||||
private final GalleryRepository galleryRepository;
|
||||
|
||||
public GalleryServiceImpl(GalleryRepository galleryRepository) {
|
||||
super(galleryRepository);
|
||||
this.galleryRepository = galleryRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存图片
|
||||
|
@ -37,8 +38,8 @@ public class GalleryServiceImpl implements GalleryService {
|
|||
*/
|
||||
@Override
|
||||
@CacheEvict(value = GALLERIES_CACHE_NAME, allEntries = true, beforeInvocation = true)
|
||||
public Gallery save(Gallery gallery) {
|
||||
return galleryRepository.save(gallery);
|
||||
public Gallery create(Gallery gallery) {
|
||||
return super.create(gallery);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -49,21 +50,8 @@ public class GalleryServiceImpl implements GalleryService {
|
|||
*/
|
||||
@Override
|
||||
@CacheEvict(value = GALLERIES_CACHE_NAME, allEntries = true, beforeInvocation = true)
|
||||
public Gallery remove(Long galleryId) {
|
||||
final Optional<Gallery> gallery = this.findByGalleryId(galleryId);
|
||||
galleryRepository.delete(gallery.get());
|
||||
return gallery.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询所有图片 分页
|
||||
*
|
||||
* @param pageable pageable
|
||||
* @return Page
|
||||
*/
|
||||
@Override
|
||||
public Page<Gallery> findAll(Pageable pageable) {
|
||||
return galleryRepository.findAll(pageable);
|
||||
public Gallery removeById(Long galleryId) {
|
||||
return super.removeById(galleryId);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -73,18 +61,8 @@ public class GalleryServiceImpl implements GalleryService {
|
|||
*/
|
||||
@Override
|
||||
@Cacheable(value = GALLERIES_CACHE_NAME, key = "'gallery'")
|
||||
public List<Gallery> findAll() {
|
||||
return galleryRepository.findAll();
|
||||
public List<Gallery> listAll() {
|
||||
return super.listAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据编号查询图片信息
|
||||
*
|
||||
* @param galleryId galleryId
|
||||
* @return Optional
|
||||
*/
|
||||
@Override
|
||||
public Optional<Gallery> findByGalleryId(Long galleryId) {
|
||||
return galleryRepository.findById(galleryId);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -158,7 +158,7 @@ public class PageController {
|
|||
@GetMapping(value = "/galleries")
|
||||
public String gallery(Model model,
|
||||
@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);
|
||||
return "admin/admin_page_gallery";
|
||||
}
|
||||
|
@ -175,7 +175,7 @@ public class PageController {
|
|||
if (StrUtil.isEmpty(gallery.getGalleryThumbnailUrl())) {
|
||||
gallery.setGalleryThumbnailUrl(gallery.getGalleryUrl());
|
||||
}
|
||||
galleryService.save(gallery);
|
||||
galleryService.create(gallery);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -191,7 +191,7 @@ public class PageController {
|
|||
*/
|
||||
@GetMapping(value = "/gallery")
|
||||
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()));
|
||||
return "admin/widget/_gallery-detail";
|
||||
}
|
||||
|
@ -206,7 +206,7 @@ public class PageController {
|
|||
@ResponseBody
|
||||
public JsonResult removeGallery(@RequestParam("galleryId") Long galleryId) {
|
||||
try {
|
||||
galleryService.remove(galleryId);
|
||||
galleryService.removeById(galleryId);
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to delete image: {}", e.getMessage());
|
||||
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.delete-failed"));
|
||||
|
|
|
@ -54,7 +54,7 @@ public class ApiGalleryController {
|
|||
*/
|
||||
@GetMapping
|
||||
public JsonResult galleries() {
|
||||
final List<Gallery> galleries = galleryService.findAll();
|
||||
final List<Gallery> galleries = galleryService.listAll();
|
||||
if (null != galleries && galleries.size() > 0) {
|
||||
return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), ResponseStatusEnum.SUCCESS.getMsg(), galleries);
|
||||
} else {
|
||||
|
@ -91,7 +91,7 @@ public class ApiGalleryController {
|
|||
*/
|
||||
@GetMapping(value = "/{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()) {
|
||||
return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), ResponseStatusEnum.SUCCESS.getMsg(), gallery.get());
|
||||
} else {
|
||||
|
|
|
@ -50,7 +50,7 @@ public class FrontPageController extends BaseController {
|
|||
*/
|
||||
@GetMapping(value = "/gallery")
|
||||
public String gallery(Model model) {
|
||||
final List<Gallery> galleries = galleryService.findAll();
|
||||
final List<Gallery> galleries = galleryService.listAll();
|
||||
model.addAttribute("galleries", galleries);
|
||||
return this.render("gallery");
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue