UserAddressController.java 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package com.izouma.nineth.web;
  2. import com.izouma.nineth.domain.UserAddress;
  3. import com.izouma.nineth.service.UserAddressService;
  4. import com.izouma.nineth.dto.PageQuery;
  5. import com.izouma.nineth.exception.BusinessException;
  6. import com.izouma.nineth.repo.UserAddressRepo;
  7. import com.izouma.nineth.utils.ObjUtils;
  8. import com.izouma.nineth.utils.SecurityUtils;
  9. import com.izouma.nineth.utils.excel.ExcelUtils;
  10. import lombok.AllArgsConstructor;
  11. import org.springframework.data.domain.Page;
  12. import org.springframework.security.access.prepost.PreAuthorize;
  13. import org.springframework.web.bind.annotation.*;
  14. import javax.servlet.http.HttpServletResponse;
  15. import java.io.IOException;
  16. import java.util.List;
  17. @RestController
  18. @RequestMapping("/userAddress")
  19. @AllArgsConstructor
  20. public class UserAddressController extends BaseController {
  21. private UserAddressService userAddressService;
  22. private UserAddressRepo userAddressRepo;
  23. //@PreAuthorize("hasRole('ADMIN')")
  24. @PostMapping("/save")
  25. public UserAddress save(@RequestBody UserAddress record) {
  26. if (record.getId() != null) {
  27. UserAddress orig = userAddressRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
  28. ObjUtils.merge(orig, record);
  29. if (orig.isDef()) {
  30. userAddressRepo.unsetDefault(SecurityUtils.getAuthenticatedUser().getId());
  31. }
  32. return userAddressRepo.save(orig);
  33. }
  34. if (record.isDef()) {
  35. userAddressRepo.unsetDefault(SecurityUtils.getAuthenticatedUser().getId());
  36. }
  37. return userAddressRepo.save(record);
  38. }
  39. //@PreAuthorize("hasRole('ADMIN')")
  40. @PostMapping("/all")
  41. public Page<UserAddress> all(@RequestBody PageQuery pageQuery) {
  42. return userAddressService.all(pageQuery);
  43. }
  44. @GetMapping("/get/{id}")
  45. public UserAddress get(@PathVariable Long id) {
  46. return userAddressRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  47. }
  48. @PostMapping("/del/{id}")
  49. public void del(@PathVariable Long id) {
  50. userAddressRepo.softDelete(id);
  51. }
  52. @GetMapping("/excel")
  53. @ResponseBody
  54. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  55. List<UserAddress> data = all(pageQuery).getContent();
  56. ExcelUtils.export(response, data);
  57. }
  58. }