CollectionController.java 5.4 KB

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