package com.izouma.nineth.web; import com.izouma.nineth.domain.MetaZoumaLight; import com.izouma.nineth.dto.PageQuery; import com.izouma.nineth.exception.BusinessException; import com.izouma.nineth.repo.MetaZoumaLightRepo; import com.izouma.nineth.service.MetaZoumaLightService; import com.izouma.nineth.utils.ObjUtils; import com.izouma.nineth.utils.excel.ExcelUtils; import lombok.AllArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; import java.util.Objects; @RestController @RequestMapping("/metaZoumaLight") @AllArgsConstructor public class MetaZoumaLightController extends BaseController { private MetaZoumaLightService metaZoumaLightService; private MetaZoumaLightRepo metaZoumaLightRepo; //@PreAuthorize("hasRole('ADMIN')") @PostMapping("/save") public MetaZoumaLight save(@RequestBody MetaZoumaLight record) { if (record.isPublish()) { MetaZoumaLight metaZoumaLight = metaZoumaLightRepo.findByPublish(true); if (Objects.nonNull(metaZoumaLight) && !Objects.equals(metaZoumaLight.getId(), record.getId())){ throw new BusinessException("仅允许发布一条!"); } } if (record.getId() != null) { MetaZoumaLight orig = metaZoumaLightRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录")); ObjUtils.merge(orig, record); return metaZoumaLightRepo.save(orig); } return metaZoumaLightRepo.save(record); } //@PreAuthorize("hasRole('ADMIN')") @PostMapping("/all") public Page all(@RequestBody PageQuery pageQuery) { return metaZoumaLightService.all(pageQuery); } @GetMapping("/get/{id}") public MetaZoumaLight get(@PathVariable Long id) { return metaZoumaLightRepo.findById(id).orElseThrow(new BusinessException("无记录")); } @PostMapping("/del/{id}") public void del(@PathVariable Long id) { metaZoumaLightRepo.softDelete(id); } @GetMapping("/excel") @ResponseBody public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException { List data = all(pageQuery).getContent(); ExcelUtils.export(response, data); } }