package com.izouma.nineth.web; import com.izouma.nineth.domain.UserAddress; import com.izouma.nineth.service.UserAddressService; import com.izouma.nineth.dto.PageQuery; import com.izouma.nineth.exception.BusinessException; import com.izouma.nineth.repo.UserAddressRepo; import com.izouma.nineth.utils.ObjUtils; import com.izouma.nineth.utils.SecurityUtils; import com.izouma.nineth.utils.excel.ExcelUtils; import lombok.AllArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; @RestController @RequestMapping("/userAddress") @AllArgsConstructor public class UserAddressController extends BaseController { private UserAddressService userAddressService; private UserAddressRepo userAddressRepo; //@PreAuthorize("hasRole('ADMIN')") @PostMapping("/save") public UserAddress save(@RequestBody UserAddress record) { if (record.getId() != null) { UserAddress orig = userAddressRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录")); ObjUtils.merge(orig, record); if (orig.isDef()) { userAddressRepo.unsetDefault(SecurityUtils.getAuthenticatedUser().getId()); } return userAddressRepo.save(orig); } if (record.isDef()) { userAddressRepo.unsetDefault(SecurityUtils.getAuthenticatedUser().getId()); } return userAddressRepo.save(record); } //@PreAuthorize("hasRole('ADMIN')") @PostMapping("/all") public Page all(@RequestBody PageQuery pageQuery) { return userAddressService.all(pageQuery); } @GetMapping("/get/{id}") public UserAddress get(@PathVariable Long id) { return userAddressRepo.findById(id).orElseThrow(new BusinessException("无记录")); } @PostMapping("/del/{id}") public void del(@PathVariable Long id) { userAddressRepo.softDelete(id); } @GetMapping("/excel") @ResponseBody public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException { List data = all(pageQuery).getContent(); ExcelUtils.export(response, data); } }