CollectionController.java 5.4 KB

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