| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- package com.izouma.nineth.web;
- import com.izouma.nineth.config.MetaConstants;
- import com.izouma.nineth.domain.MetaResourceVersion;
- import com.izouma.nineth.domain.MetaResources;
- import com.izouma.nineth.dto.MetaRestResult;
- import com.izouma.nineth.service.MetaResourcesService;
- import com.izouma.nineth.dto.PageQuery;
- import com.izouma.nineth.exception.BusinessException;
- import com.izouma.nineth.repo.MetaResourcesRepo;
- 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.security.access.prepost.PreAuthorize;
- 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("/metaResources")
- @AllArgsConstructor
- public class MetaResourcesController extends BaseController {
- private MetaResourcesService metaResourcesService;
- private MetaResourcesRepo metaResourcesRepo;
- //@PreAuthorize("hasRole('ADMIN')")
- @PostMapping("/save")
- public MetaResources save(@RequestBody MetaResources record) {
- MetaResources metaResources = metaResourcesRepo.findByNameAndDel(record.getName(), false);
- if (Objects.nonNull(metaResources) && !Objects.equals(metaResources.getId(), record.getId())) {
- throw new BusinessException("当前资源名称已经存在!");
- }
- if (record.getId() != null) {
- MetaResources orig = metaResourcesRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
- ObjUtils.merge(orig, record);
- return metaResourcesRepo.save(orig);
- }
- return metaResourcesRepo.save(record);
- }
- //@PreAuthorize("hasRole('ADMIN')")
- @PostMapping("/all")
- public Page<MetaResources> all(@RequestBody PageQuery pageQuery) {
- return metaResourcesService.all(pageQuery);
- }
- @GetMapping("/get/{id}")
- public MetaResources get(@PathVariable Long id) {
- return metaResourcesRepo.findById(id).orElseThrow(new BusinessException("无记录"));
- }
- @PostMapping("/del/{id}")
- public void del(@PathVariable Long id) {
- metaResourcesRepo.softDelete(id);
- }
- @GetMapping("/excel")
- @ResponseBody
- public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
- List<MetaResources> data = all(pageQuery).getContent();
- ExcelUtils.export(response, data);
- }
- @GetMapping("/getUrl")
- public MetaRestResult<String> getUrl(String name) {
- MetaResources metaResources = metaResourcesRepo.findByNameAndDel(name, false);
- if (Objects.isNull(metaResources)) {
- return MetaRestResult.returnError("不存在该名称的资源");
- }
- if (Objects.isNull(metaResources.getPic())) {
- return MetaRestResult.returnError("该名称的资源文件为空");
- }
- return MetaRestResult.returnSuccess("查询成功", metaResources.getPic().get(0).getUrl());
- }
- }
|