MetaZoumaLightController.java 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package com.izouma.nineth.web;
  2. import com.izouma.nineth.domain.MetaZoumaLight;
  3. import com.izouma.nineth.dto.PageQuery;
  4. import com.izouma.nineth.exception.BusinessException;
  5. import com.izouma.nineth.repo.MetaZoumaLightRepo;
  6. import com.izouma.nineth.service.MetaZoumaLightService;
  7. import com.izouma.nineth.utils.ObjUtils;
  8. import com.izouma.nineth.utils.excel.ExcelUtils;
  9. import lombok.AllArgsConstructor;
  10. import org.springframework.data.domain.Page;
  11. import org.springframework.web.bind.annotation.*;
  12. import javax.servlet.http.HttpServletResponse;
  13. import java.io.IOException;
  14. import java.util.List;
  15. import java.util.Objects;
  16. @RestController
  17. @RequestMapping("/metaZoumaLight")
  18. @AllArgsConstructor
  19. public class MetaZoumaLightController extends BaseController {
  20. private MetaZoumaLightService metaZoumaLightService;
  21. private MetaZoumaLightRepo metaZoumaLightRepo;
  22. //@PreAuthorize("hasRole('ADMIN')")
  23. @PostMapping("/save")
  24. public MetaZoumaLight save(@RequestBody MetaZoumaLight record) {
  25. if (record.isPublish()) {
  26. MetaZoumaLight metaZoumaLight = metaZoumaLightRepo.findByPublish(true);
  27. if (Objects.nonNull(metaZoumaLight) && !Objects.equals(metaZoumaLight.getId(), record.getId())){
  28. throw new BusinessException("仅允许发布一条!");
  29. }
  30. }
  31. if (record.getId() != null) {
  32. MetaZoumaLight orig = metaZoumaLightRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
  33. ObjUtils.merge(orig, record);
  34. return metaZoumaLightRepo.save(orig);
  35. }
  36. return metaZoumaLightRepo.save(record);
  37. }
  38. //@PreAuthorize("hasRole('ADMIN')")
  39. @PostMapping("/all")
  40. public Page<MetaZoumaLight> all(@RequestBody PageQuery pageQuery) {
  41. return metaZoumaLightService.all(pageQuery);
  42. }
  43. @GetMapping("/get/{id}")
  44. public MetaZoumaLight get(@PathVariable Long id) {
  45. return metaZoumaLightRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  46. }
  47. @PostMapping("/del/{id}")
  48. public void del(@PathVariable Long id) {
  49. metaZoumaLightRepo.softDelete(id);
  50. }
  51. @GetMapping("/excel")
  52. @ResponseBody
  53. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  54. List<MetaZoumaLight> data = all(pageQuery).getContent();
  55. ExcelUtils.export(response, data);
  56. }
  57. }