BannerController.java 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package com.izouma.nineth.web;
  2. import com.izouma.nineth.domain.Banner;
  3. import com.izouma.nineth.service.BannerService;
  4. import com.izouma.nineth.dto.PageQuery;
  5. import com.izouma.nineth.exception.BusinessException;
  6. import com.izouma.nineth.repo.BannerRepo;
  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.security.access.prepost.PreAuthorize;
  12. import org.springframework.web.bind.annotation.*;
  13. import javax.servlet.http.HttpServletResponse;
  14. import java.io.IOException;
  15. import java.util.List;
  16. @RestController
  17. @RequestMapping("/banner")
  18. @AllArgsConstructor
  19. public class BannerController extends BaseController {
  20. private BannerService bannerService;
  21. private BannerRepo bannerRepo;
  22. //@PreAuthorize("hasRole('ADMIN')")
  23. @PostMapping("/save")
  24. public Banner save(@RequestBody Banner record) {
  25. if (record.getId() != null) {
  26. Banner orig = bannerRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
  27. ObjUtils.merge(orig, record);
  28. return bannerRepo.save(orig);
  29. }
  30. return bannerRepo.save(record);
  31. }
  32. //@PreAuthorize("hasRole('ADMIN')")
  33. @PostMapping("/all")
  34. public Page<Banner> all(@RequestBody PageQuery pageQuery) {
  35. pageQuery.getQuery().put("del", false);
  36. return bannerService.all(pageQuery);
  37. }
  38. @GetMapping("/get/{id}")
  39. public Banner get(@PathVariable Long id) {
  40. return bannerRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  41. }
  42. @PostMapping("/del/{id}")
  43. public void del(@PathVariable Long id) {
  44. bannerRepo.softDelete(id);
  45. }
  46. @GetMapping("/excel")
  47. @ResponseBody
  48. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  49. List<Banner> data = all(pageQuery).getContent();
  50. ExcelUtils.export(response, data);
  51. }
  52. }