NewsLikeController.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package com.izouma.nineth.web;
  2. import com.izouma.nineth.domain.NewsLike;
  3. import com.izouma.nineth.service.NewsLikeService;
  4. import com.izouma.nineth.dto.PageQuery;
  5. import com.izouma.nineth.exception.BusinessException;
  6. import com.izouma.nineth.repo.NewsLikeRepo;
  7. import com.izouma.nineth.utils.ObjUtils;
  8. import com.izouma.nineth.utils.SecurityUtils;
  9. import com.izouma.nineth.utils.excel.ExcelUtils;
  10. import io.swagger.annotations.ApiOperation;
  11. import lombok.AllArgsConstructor;
  12. import org.springframework.cache.annotation.CacheEvict;
  13. import org.springframework.data.domain.Page;
  14. import org.springframework.security.access.prepost.PreAuthorize;
  15. import org.springframework.web.bind.annotation.*;
  16. import javax.servlet.http.HttpServletResponse;
  17. import java.io.IOException;
  18. import java.util.List;
  19. @RestController
  20. @RequestMapping("/newsLike")
  21. @AllArgsConstructor
  22. public class NewsLikeController extends BaseController {
  23. private NewsLikeService newsLikeService;
  24. private NewsLikeRepo newsLikeRepo;
  25. //@PreAuthorize("hasRole('ADMIN')")
  26. @PostMapping("/save")
  27. public NewsLike save(@RequestBody NewsLike record) {
  28. if (record.getId() != null) {
  29. NewsLike orig = newsLikeRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
  30. ObjUtils.merge(orig, record);
  31. return newsLikeRepo.save(orig);
  32. }
  33. return newsLikeRepo.save(record);
  34. }
  35. //@PreAuthorize("hasRole('ADMIN')")
  36. @PostMapping("/all")
  37. public Page<NewsLike> all(@RequestBody PageQuery pageQuery) {
  38. return newsLikeService.all(pageQuery);
  39. }
  40. @GetMapping("/get/{id}")
  41. public NewsLike get(@PathVariable Long id) {
  42. return newsLikeRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  43. }
  44. @PostMapping("/del/{id}")
  45. public void del(@PathVariable Long id) {
  46. newsLikeRepo.softDelete(id);
  47. }
  48. @GetMapping("/excel")
  49. @ResponseBody
  50. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  51. List<NewsLike> data = all(pageQuery).getContent();
  52. ExcelUtils.export(response, data);
  53. }
  54. @GetMapping("/{id}/like")
  55. @ApiOperation("点赞")
  56. @CacheEvict("news")
  57. public void like(@PathVariable Long id) {
  58. newsLikeService.like(SecurityUtils.getAuthenticatedUser().getId(), id);
  59. }
  60. @GetMapping("/{id}/unlike")
  61. @ApiOperation("取消点赞")
  62. @CacheEvict("news")
  63. public void unlike(@PathVariable Long id) {
  64. newsLikeService.unlike(SecurityUtils.getAuthenticatedUser().getId(), id);
  65. }
  66. @GetMapping("/{id}/likeRoom")
  67. @ApiOperation("点赞")
  68. public void likeRoom(@PathVariable Long id) {
  69. newsLikeService.likeRoom(SecurityUtils.getAuthenticatedUser().getId(), id);
  70. }
  71. @GetMapping("/{id}/unlikeRoom")
  72. @ApiOperation("取消点赞")
  73. public void unlikeRoom(@PathVariable Long id) {
  74. newsLikeService.unlikeRoom(SecurityUtils.getAuthenticatedUser().getId(), id);
  75. }
  76. @GetMapping("/{id}/likeAuction")
  77. @ApiOperation("点赞拍卖")
  78. public void likeAuction(@PathVariable Long id) {
  79. newsLikeService.likeAuction(SecurityUtils.getAuthenticatedUser().getId(), id);
  80. }
  81. @GetMapping("/{id}/unlikeAuction")
  82. @ApiOperation("取消点赞拍卖")
  83. public void unlikeAuction(@PathVariable Long id) {
  84. newsLikeService.unlikeAuction(SecurityUtils.getAuthenticatedUser().getId(), id);
  85. }
  86. }