| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- package com.izouma.nineth.web;
- import com.izouma.nineth.domain.Collection;
- import com.izouma.nineth.domain.FileObject;
- import com.izouma.nineth.dto.*;
- import com.izouma.nineth.exception.BusinessException;
- import com.izouma.nineth.repo.CollectionRepo;
- import com.izouma.nineth.repo.NewsRepo;
- import com.izouma.nineth.service.CacheService;
- import com.izouma.nineth.service.CollectionService;
- import com.izouma.nineth.service.LikeService;
- import com.izouma.nineth.utils.SecurityUtils;
- import com.izouma.nineth.utils.excel.ExcelUtils;
- import io.swagger.annotations.ApiOperation;
- import lombok.AllArgsConstructor;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.BeanUtils;
- import org.springframework.cache.annotation.CacheEvict;
- import org.springframework.data.domain.Page;
- import org.springframework.web.bind.annotation.*;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- import java.util.*;
- import java.util.stream.Collectors;
- import org.springframework.cache.annotation.Cacheable;
- @RestController
- @RequestMapping("/collection")
- @AllArgsConstructor
- public class CollectionController extends BaseController {
- private CollectionService collectionService;
- private CollectionRepo collectionRepo;
- private LikeService likeService;
- private NewsRepo newsRepo;
- private CacheService cacheService;
- //@PreAuthorize("hasRole('ADMIN')")
- @PostMapping("/save")
- public Collection save(@RequestBody Collection record) {
- return collectionService.update(record);
- }
- @PostMapping("/create")
- public Collection create(@RequestBody Collection record) {
- return collectionService.create(record);
- }
- //@PreAuthorize("hasRole('ADMIN')")
- @PostMapping("/all")
- public Page<CollectionDTO> all(@RequestBody PageQuery pageQuery) {
- return collectionService.toDTO(collectionService.all(pageQuery).toPage());
- }
- @GetMapping("/get/{id}")
- public CollectionDTO get(@PathVariable Long id) {
- return collectionService.toDTO(collectionRepo.findById(id)
- .orElseThrow(new BusinessException("无记录")), true, true);
- }
- @PostMapping("/del/{id}")
- public void del(@PathVariable Long id) {
- collectionRepo.softDelete(id);
- }
- @GetMapping("/excel")
- @ResponseBody
- public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
- List<Collection> data = collectionService.all(pageQuery).getContent();
- ExcelUtils.export(response, data);
- }
- @GetMapping("/{id}/like")
- @ApiOperation("点赞")
- @CacheEvict("collection")
- public void like(@PathVariable Long id) {
- likeService.like(SecurityUtils.getAuthenticatedUser().getId(), id);
- }
- @GetMapping("/{id}/unlike")
- @ApiOperation("取消点赞")
- @CacheEvict("collection")
- public void unlike(@PathVariable Long id) {
- likeService.unlike(SecurityUtils.getAuthenticatedUser().getId(), id);
- }
- @GetMapping("/myLikes")
- @ApiOperation("我收藏的")
- public List<CollectionDTO> myLikes() {
- return collectionService.toDTO(collectionRepo.userLikes(SecurityUtils.getAuthenticatedUser().getId()));
- }
- @PostMapping("/createBlindBox")
- public Collection createBlindBox(@RequestBody CreateBlindBox createBlindBox) {
- return collectionService.createBlindBox(createBlindBox);
- }
- @PostMapping("/appointment")
- public void appointment(@RequestParam Long id) {
- collectionService.appointment(id, SecurityUtils.getAuthenticatedUser().getId());
- }
- // @GetMapping("/recommend")
- // @Cacheable("recommend")
- public List<CollectionDTO> recommend(@RequestParam String type) {
- return collectionRepo.recommend(type).stream().map(rc -> {
- if (StringUtils.isNotBlank(rc.getRecommend().getPic())) {
- rc.getCollection().setPic(Collections.singletonList(new FileObject(null, rc.getRecommend()
- .getPic(), null, null)));
- }
- CollectionDTO collectionDTO = new CollectionDTO();
- BeanUtils.copyProperties(rc.getCollection(), collectionDTO);
- return collectionDTO;
- }).collect(Collectors.toList());
- }
- @GetMapping("/recommend")
- @Cacheable("recommend")
- public List<RecommendDTO> recommendAll() {
- List<RecommendDTO> recommedDTOS = new ArrayList<>();
- List<RecommendDTO> collectionDTOS = collectionRepo.recommend("LIST").stream().map(rc -> {
- if (StringUtils.isNotBlank(rc.getRecommend().getPic())) {
- rc.getCollection().setPic(Collections.singletonList(new FileObject(null, rc.getRecommend()
- .getPic(), null, null)));
- }
- CollectionDTO collectionDTO = new CollectionDTO();
- BeanUtils.copyProperties(rc.getCollection(), collectionDTO);
- return new RecommendDTO(rc.getRecommend().getSort(), collectionDTO, "collection");
- }).collect(Collectors.toList());
- List<RecommendDTO> news = newsRepo.recommend("LIST").stream().map(rn -> {
- if (StringUtils.isNotBlank(rn.getRecommend().getPic())) {
- rn.getNews().setPic(rn.getRecommend().getPic());
- }
- return new RecommendDTO(rn.getRecommend().getSort(), rn.getNews(), "news");
- }).collect(Collectors.toList());
- recommedDTOS.addAll(collectionDTOS);
- recommedDTOS.addAll(news);
- recommedDTOS.sort((a, b) -> b.getSort().compareTo(a.getSort()));
- return recommedDTOS;
- }
- @PostMapping("/clearRecommend")
- public String clearRecommend() {
- cacheService.clearRecommend();
- return "ok";
- }
- }
|