CollectionController.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package com.izouma.nineth.web;
  2. import com.izouma.nineth.domain.Collection;
  3. import com.izouma.nineth.dto.CollectionDTO;
  4. import com.izouma.nineth.dto.CreateBlindBox;
  5. import com.izouma.nineth.dto.PageQuery;
  6. import com.izouma.nineth.exception.BusinessException;
  7. import com.izouma.nineth.repo.CollectionRepo;
  8. import com.izouma.nineth.service.CollectionService;
  9. import com.izouma.nineth.service.LikeService;
  10. import com.izouma.nineth.utils.ObjUtils;
  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.springframework.cache.annotation.CacheEvict;
  16. import org.springframework.data.domain.Page;
  17. import org.springframework.web.bind.annotation.*;
  18. import javax.servlet.http.HttpServletResponse;
  19. import java.io.IOException;
  20. import java.util.List;
  21. @RestController
  22. @RequestMapping("/collection")
  23. @AllArgsConstructor
  24. public class CollectionController extends BaseController {
  25. private CollectionService collectionService;
  26. private CollectionRepo collectionRepo;
  27. private LikeService likeService;
  28. //@PreAuthorize("hasRole('ADMIN')")
  29. @PostMapping("/save")
  30. public Collection save(@RequestBody Collection record) {
  31. Collection orig = collectionRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
  32. ObjUtils.merge(orig, record);
  33. return collectionRepo.save(orig);
  34. }
  35. @PostMapping("/create")
  36. public Collection create(@RequestBody Collection record) {
  37. return collectionService.create(record);
  38. }
  39. //@PreAuthorize("hasRole('ADMIN')")
  40. @PostMapping("/all")
  41. public Page<CollectionDTO> all(@RequestBody PageQuery pageQuery) {
  42. return collectionService.toDTO(collectionService.all(pageQuery));
  43. }
  44. @GetMapping("/get/{id}")
  45. public CollectionDTO get(@PathVariable Long id) {
  46. return collectionService.toDTO(collectionRepo.findById(id).orElseThrow(new BusinessException("无记录")), true);
  47. }
  48. @PostMapping("/del/{id}")
  49. public void del(@PathVariable Long id) {
  50. collectionRepo.softDelete(id);
  51. }
  52. @GetMapping("/excel")
  53. @ResponseBody
  54. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  55. List<Collection> data = collectionService.all(pageQuery).getContent();
  56. ExcelUtils.export(response, data);
  57. }
  58. @GetMapping("/{id}/like")
  59. @ApiOperation("点赞")
  60. @CacheEvict("collection")
  61. public void like(@PathVariable Long id) {
  62. likeService.like(SecurityUtils.getAuthenticatedUser().getId(), id);
  63. }
  64. @GetMapping("/{id}/unlike")
  65. @ApiOperation("取消点赞")
  66. @CacheEvict("collection")
  67. public void unlike(@PathVariable Long id) {
  68. likeService.unlike(SecurityUtils.getAuthenticatedUser().getId(), id);
  69. }
  70. @GetMapping("/myLikes")
  71. @ApiOperation("我收藏的")
  72. public List<CollectionDTO> myLikes() {
  73. return collectionService.toDTO(collectionRepo.userLikes(SecurityUtils.getAuthenticatedUser().getId()));
  74. }
  75. @PostMapping("/createBlindBox")
  76. public Collection createBlindBox(@RequestBody CreateBlindBox createBlindBox) {
  77. return collectionService.createBlindBox(createBlindBox);
  78. }
  79. @PostMapping("/appointment")
  80. public void appointment(@RequestParam Long id) {
  81. collectionService.appointment(id, SecurityUtils.getAuthenticatedUser().getId());
  82. }
  83. @GetMapping("/recommend")
  84. public List<CollectionDTO> recommend(@RequestParam String type) {
  85. return collectionService.toDTO(collectionRepo.recommend(type));
  86. }
  87. }