CollectionController.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.CollectionDTO;
  5. import com.izouma.nineth.dto.CreateBlindBox;
  6. import com.izouma.nineth.dto.PageQuery;
  7. import com.izouma.nineth.exception.BusinessException;
  8. import com.izouma.nineth.repo.CollectionRepo;
  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.cache.annotation.Cacheable;
  19. import org.springframework.data.domain.Page;
  20. import org.springframework.web.bind.annotation.*;
  21. import javax.servlet.http.HttpServletResponse;
  22. import java.io.IOException;
  23. import java.util.Collections;
  24. import java.util.List;
  25. import java.util.stream.Collectors;
  26. @RestController
  27. @RequestMapping("/collection")
  28. @AllArgsConstructor
  29. public class CollectionController extends BaseController {
  30. private CollectionService collectionService;
  31. private CollectionRepo collectionRepo;
  32. private LikeService likeService;
  33. //@PreAuthorize("hasRole('ADMIN')")
  34. @PostMapping("/save")
  35. public Collection save(@RequestBody Collection record) {
  36. return collectionService.update(record);
  37. }
  38. @PostMapping("/create")
  39. public Collection create(@RequestBody Collection record) {
  40. return collectionService.create(record);
  41. }
  42. //@PreAuthorize("hasRole('ADMIN')")
  43. @PostMapping("/all")
  44. public Page<CollectionDTO> all(@RequestBody PageQuery pageQuery) {
  45. return collectionService.toDTO(collectionService.all(pageQuery));
  46. }
  47. @GetMapping("/get/{id}")
  48. public CollectionDTO get(@PathVariable Long id) {
  49. return collectionService.toDTO(collectionRepo.findById(id)
  50. .orElseThrow(new BusinessException("无记录")), true, true);
  51. }
  52. @PostMapping("/del/{id}")
  53. public void del(@PathVariable Long id) {
  54. collectionRepo.softDelete(id);
  55. }
  56. @GetMapping("/excel")
  57. @ResponseBody
  58. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  59. List<Collection> data = collectionService.all(pageQuery).getContent();
  60. ExcelUtils.export(response, data);
  61. }
  62. @GetMapping("/{id}/like")
  63. @ApiOperation("点赞")
  64. @CacheEvict("collection")
  65. public void like(@PathVariable Long id) {
  66. likeService.like(SecurityUtils.getAuthenticatedUser().getId(), id);
  67. }
  68. @GetMapping("/{id}/unlike")
  69. @ApiOperation("取消点赞")
  70. @CacheEvict("collection")
  71. public void unlike(@PathVariable Long id) {
  72. likeService.unlike(SecurityUtils.getAuthenticatedUser().getId(), id);
  73. }
  74. @GetMapping("/myLikes")
  75. @ApiOperation("我收藏的")
  76. public List<CollectionDTO> myLikes() {
  77. return collectionService.toDTO(collectionRepo.userLikes(SecurityUtils.getAuthenticatedUser().getId()));
  78. }
  79. @PostMapping("/createBlindBox")
  80. public Collection createBlindBox(@RequestBody CreateBlindBox createBlindBox) {
  81. return collectionService.createBlindBox(createBlindBox);
  82. }
  83. @PostMapping("/appointment")
  84. public void appointment(@RequestParam Long id) {
  85. collectionService.appointment(id, SecurityUtils.getAuthenticatedUser().getId());
  86. }
  87. @GetMapping("/recommend")
  88. @Cacheable("recommend")
  89. public List<CollectionDTO> recommend(@RequestParam String type) {
  90. return collectionRepo.recommend(type).stream().map(rc -> {
  91. if (StringUtils.isNotBlank(rc.getRecommend().getPic())) {
  92. rc.getCollection().setPic(Collections.singletonList(new FileObject(null, rc.getRecommend()
  93. .getPic(), null, null)));
  94. }
  95. CollectionDTO collectionDTO = new CollectionDTO();
  96. BeanUtils.copyProperties(rc.getCollection(), collectionDTO);
  97. return collectionDTO;
  98. }).collect(Collectors.toList());
  99. }
  100. }