CollectionController.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.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.Collections;
  22. import java.util.List;
  23. import java.util.stream.Collectors;
  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. //@PreAuthorize("hasRole('ADMIN')")
  32. @PostMapping("/save")
  33. public Collection save(@RequestBody Collection record) {
  34. return collectionService.update(record);
  35. }
  36. @PostMapping("/create")
  37. public Collection create(@RequestBody Collection record) {
  38. return collectionService.create(record);
  39. }
  40. //@PreAuthorize("hasRole('ADMIN')")
  41. @PostMapping("/all")
  42. public Page<CollectionDTO> all(@RequestBody PageQuery pageQuery) {
  43. return collectionService.toDTO(collectionService.all(pageQuery));
  44. }
  45. @GetMapping("/get/{id}")
  46. public CollectionDTO get(@PathVariable Long id) {
  47. return collectionService.toDTO(collectionRepo.findById(id).orElseThrow(new BusinessException("无记录")), true);
  48. }
  49. @PostMapping("/del/{id}")
  50. public void del(@PathVariable Long id) {
  51. collectionRepo.softDelete(id);
  52. }
  53. @GetMapping("/excel")
  54. @ResponseBody
  55. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  56. List<Collection> data = collectionService.all(pageQuery).getContent();
  57. ExcelUtils.export(response, data);
  58. }
  59. @GetMapping("/{id}/like")
  60. @ApiOperation("点赞")
  61. @CacheEvict("collection")
  62. public void like(@PathVariable Long id) {
  63. likeService.like(SecurityUtils.getAuthenticatedUser().getId(), id);
  64. }
  65. @GetMapping("/{id}/unlike")
  66. @ApiOperation("取消点赞")
  67. @CacheEvict("collection")
  68. public void unlike(@PathVariable Long id) {
  69. likeService.unlike(SecurityUtils.getAuthenticatedUser().getId(), id);
  70. }
  71. @GetMapping("/myLikes")
  72. @ApiOperation("我收藏的")
  73. public List<CollectionDTO> myLikes() {
  74. return collectionService.toDTO(collectionRepo.userLikes(SecurityUtils.getAuthenticatedUser().getId()));
  75. }
  76. @PostMapping("/createBlindBox")
  77. public Collection createBlindBox(@RequestBody CreateBlindBox createBlindBox) {
  78. return collectionService.createBlindBox(createBlindBox);
  79. }
  80. @PostMapping("/appointment")
  81. public void appointment(@RequestParam Long id) {
  82. collectionService.appointment(id, SecurityUtils.getAuthenticatedUser().getId());
  83. }
  84. @GetMapping("/recommend")
  85. public List<CollectionDTO> recommend(@RequestParam String type, @RequestParam(defaultValue = "0") int projectId) {
  86. return collectionService.toDTO(collectionRepo.recommend(type, projectId).stream().map(rc -> {
  87. if (StringUtils.isNotBlank(rc.getRecommend().getPic())) {
  88. rc.getCollection().setPic(Collections.singletonList(new FileObject(null, rc.getRecommend()
  89. .getPic(), null, null)));
  90. }
  91. return rc.getCollection();
  92. }).collect(Collectors.toList()));
  93. }
  94. }