CollectionController.java 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.service.CollectionService;
  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.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.data.domain.Page;
  16. import org.springframework.security.access.prepost.PreAuthorize;
  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. public void like(@PathVariable Long id) {
  61. likeService.like(SecurityUtils.getAuthenticatedUser().getId(), id);
  62. }
  63. @GetMapping("/{id}/unlike")
  64. @ApiOperation("取消点赞")
  65. public void unlike(@PathVariable Long id) {
  66. likeService.unlike(SecurityUtils.getAuthenticatedUser().getId(), id);
  67. }
  68. @GetMapping("/myLikes")
  69. @ApiOperation("我收藏的")
  70. public List<CollectionDTO> myLikes() {
  71. return collectionService.toDTO(collectionRepo.userLikes(SecurityUtils.getAuthenticatedUser().getId()));
  72. }
  73. @PostMapping("/createBlindBox")
  74. public Collection createBlindBox(@RequestBody CreateBlindBox createBlindBox) {
  75. return collectionService.createBlindBox(createBlindBox);
  76. }
  77. }