CollectionController.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package com.izouma.nineth.web;
  2. import com.izouma.nineth.domain.Collection;
  3. import com.izouma.nineth.domain.FileObject;
  4. import com.izouma.nineth.dto.*;
  5. import com.izouma.nineth.exception.BusinessException;
  6. import com.izouma.nineth.repo.CollectionRepo;
  7. import com.izouma.nineth.repo.NewsRepo;
  8. import com.izouma.nineth.service.CacheService;
  9. import com.izouma.nineth.service.CollectionService;
  10. import com.izouma.nineth.service.LikeService;
  11. import com.izouma.nineth.utils.SecurityUtils;
  12. import com.izouma.nineth.utils.excel.ExcelUtils;
  13. import io.swagger.annotations.ApiOperation;
  14. import lombok.AllArgsConstructor;
  15. import org.apache.commons.lang3.StringUtils;
  16. import org.springframework.beans.BeanUtils;
  17. import org.springframework.cache.annotation.CacheEvict;
  18. import org.springframework.data.domain.Page;
  19. import org.springframework.web.bind.annotation.*;
  20. import javax.servlet.http.HttpServletResponse;
  21. import java.io.IOException;
  22. import java.util.*;
  23. import java.util.stream.Collectors;
  24. import org.springframework.cache.annotation.Cacheable;
  25. @RestController
  26. @RequestMapping("/collection")
  27. @AllArgsConstructor
  28. public class CollectionController extends BaseController {
  29. private CollectionService collectionService;
  30. private CollectionRepo collectionRepo;
  31. private LikeService likeService;
  32. private NewsRepo newsRepo;
  33. private CacheService cacheService;
  34. //@PreAuthorize("hasRole('ADMIN')")
  35. @PostMapping("/save")
  36. public Collection save(@RequestBody Collection record) {
  37. return collectionService.update(record);
  38. }
  39. @PostMapping("/create")
  40. public Collection create(@RequestBody Collection record) {
  41. return collectionService.create(record);
  42. }
  43. //@PreAuthorize("hasRole('ADMIN')")
  44. @PostMapping("/all")
  45. public Page<CollectionDTO> all(@RequestBody PageQuery pageQuery) {
  46. return collectionService.toDTO(collectionService.all(pageQuery).toPage());
  47. }
  48. @GetMapping("/get/{id}")
  49. public CollectionDTO get(@PathVariable Long id) {
  50. return collectionService.toDTO(collectionRepo.findById(id)
  51. .orElseThrow(new BusinessException("无记录")), true, true);
  52. }
  53. @PostMapping("/del/{id}")
  54. public void del(@PathVariable Long id) {
  55. collectionRepo.softDelete(id);
  56. }
  57. @GetMapping("/excel")
  58. @ResponseBody
  59. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  60. List<Collection> data = collectionService.all(pageQuery).getContent();
  61. ExcelUtils.export(response, data);
  62. }
  63. @GetMapping("/{id}/like")
  64. @ApiOperation("点赞")
  65. @CacheEvict("collection")
  66. public void like(@PathVariable Long id) {
  67. likeService.like(SecurityUtils.getAuthenticatedUser().getId(), id);
  68. }
  69. @GetMapping("/{id}/unlike")
  70. @ApiOperation("取消点赞")
  71. @CacheEvict("collection")
  72. public void unlike(@PathVariable Long id) {
  73. likeService.unlike(SecurityUtils.getAuthenticatedUser().getId(), id);
  74. }
  75. @GetMapping("/myLikes")
  76. @ApiOperation("我收藏的")
  77. public List<CollectionDTO> myLikes() {
  78. return collectionService.toDTO(collectionRepo.userLikes(SecurityUtils.getAuthenticatedUser().getId()));
  79. }
  80. @PostMapping("/createBlindBox")
  81. public Collection createBlindBox(@RequestBody CreateBlindBox createBlindBox) {
  82. return collectionService.createBlindBox(createBlindBox);
  83. }
  84. @PostMapping("/appointment")
  85. public void appointment(@RequestParam Long id) {
  86. collectionService.appointment(id, SecurityUtils.getAuthenticatedUser().getId());
  87. }
  88. // @GetMapping("/recommend")
  89. // @Cacheable("recommend")
  90. public List<CollectionDTO> recommend(@RequestParam String type) {
  91. return collectionRepo.recommend(type).stream().map(rc -> {
  92. if (StringUtils.isNotBlank(rc.getRecommend().getPic())) {
  93. rc.getCollection().setPic(Collections.singletonList(new FileObject(null, rc.getRecommend()
  94. .getPic(), null, null)));
  95. }
  96. CollectionDTO collectionDTO = new CollectionDTO();
  97. BeanUtils.copyProperties(rc.getCollection(), collectionDTO);
  98. return collectionDTO;
  99. }).collect(Collectors.toList());
  100. }
  101. @GetMapping("/recommend")
  102. @Cacheable("recommend")
  103. public List<RecommendDTO> recommendAll() {
  104. List<RecommendDTO> recommedDTOS = new ArrayList<>();
  105. List<RecommendDTO> collectionDTOS = collectionRepo.recommend("LIST").stream().map(rc -> {
  106. if (StringUtils.isNotBlank(rc.getRecommend().getPic())) {
  107. rc.getCollection().setPic(Collections.singletonList(new FileObject(null, rc.getRecommend()
  108. .getPic(), null, null)));
  109. }
  110. CollectionDTO collectionDTO = new CollectionDTO();
  111. BeanUtils.copyProperties(rc.getCollection(), collectionDTO);
  112. return new RecommendDTO(rc.getRecommend().getSort(), collectionDTO, "collection");
  113. }).collect(Collectors.toList());
  114. List<RecommendDTO> news = newsRepo.recommend("LIST").stream().map(rn -> {
  115. if (StringUtils.isNotBlank(rn.getRecommend().getPic())) {
  116. rn.getNews().setPic(rn.getRecommend().getPic());
  117. }
  118. return new RecommendDTO(rn.getRecommend().getSort(), rn.getNews(), "news");
  119. }).collect(Collectors.toList());
  120. recommedDTOS.addAll(collectionDTOS);
  121. recommedDTOS.addAll(news);
  122. recommedDTOS.sort((a, b) -> b.getSort().compareTo(a.getSort()));
  123. return recommedDTOS;
  124. }
  125. @PostMapping("/clearRecommend")
  126. public String clearRecommend() {
  127. cacheService.clearRecommend();
  128. return "ok";
  129. }
  130. }