SetMealController.java 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package com.izouma.jiashanxia.web;
  2. import com.izouma.jiashanxia.domain.SetMeal;
  3. import com.izouma.jiashanxia.service.SetMealService;
  4. import com.izouma.jiashanxia.dto.PageQuery;
  5. import com.izouma.jiashanxia.exception.BusinessException;
  6. import com.izouma.jiashanxia.repo.SetMealRepo;
  7. import com.izouma.jiashanxia.utils.ObjUtils;
  8. import com.izouma.jiashanxia.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. @RestController
  16. @RequestMapping("/setMeal")
  17. @AllArgsConstructor
  18. public class SetMealController extends BaseController {
  19. private SetMealService setMealService;
  20. private SetMealRepo setMealRepo;
  21. //@PreAuthorize("hasRole('ADMIN')")
  22. @PostMapping("/save")
  23. public SetMeal save(@RequestBody SetMeal record) {
  24. if (record.getId() != null) {
  25. SetMeal orig = setMealRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
  26. ObjUtils.merge(orig, record);
  27. return setMealRepo.save(orig);
  28. }
  29. return setMealRepo.save(record);
  30. }
  31. //@PreAuthorize("hasRole('ADMIN')")
  32. @PostMapping("/all")
  33. public Page<SetMeal> all(@RequestBody PageQuery pageQuery) {
  34. return setMealService.all(pageQuery);
  35. }
  36. @GetMapping("/get/{id}")
  37. public SetMeal get(@PathVariable Long id) {
  38. return setMealRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  39. }
  40. @PostMapping("/del/{id}")
  41. public void del(@PathVariable Long id) {
  42. setMealRepo.softDelete(id);
  43. }
  44. @GetMapping("/excel")
  45. @ResponseBody
  46. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  47. List<SetMeal> data = all(pageQuery).getContent();
  48. ExcelUtils.export(response, data);
  49. }
  50. }