PicConfigController.java 2.14 KB
package com.viontech.storage.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.viontech.storage.entity.Context;
import com.viontech.storage.entity.Message;
import com.viontech.storage.model.PicConfig;
import com.viontech.storage.service.PicConfigService;
import com.viontech.storage.vo.PicConfigVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * .
 *
 * @author 谢明辉
 * @date 2021/11/9
 */

@RestController
@RequestMapping("/picConfigs")
public class PicConfigController {

    @Resource
    private PicConfigService picConfigService;


    @GetMapping("/{id}")
    public Message<PicConfig> getById(@PathVariable("id") Long id) {
        PicConfig byId = picConfigService.getById(id);
        PicConfigVo copy = PicConfigVo.copy(byId);
        return Message.success(copy);
    }

    @GetMapping
    public Message<List<PicConfig>> listAll(PicConfigVo captionVo) {
        List<PicConfig> list = picConfigService.list();
        List<PicConfig> collect = list.stream().map(PicConfigVo::copy).collect(Collectors.toList());
        return Message.success(collect);
    }

    @PostMapping
    public Message<PicConfig> add(@RequestBody PicConfigVo item) {
        List<Context> contexts = item.getContexts();
        if (CollectionUtil.isNotEmpty(contexts)) {
            item.setContext(JSONUtil.toJsonStr(contexts));
        }
        boolean save = picConfigService.save(item);
        return Message.success(item);
    }

    @PutMapping("/{id}")
    public Message<PicConfig> updateById(@PathVariable Long id, @RequestBody PicConfigVo item) {
        List<Context> contexts = item.getContexts();
        if (CollectionUtil.isNotEmpty(contexts)) {
            item.setContext(JSONUtil.toJsonStr(contexts));
        }
        item.setId(id);
        picConfigService.updateById(item);
        return Message.success(item);
    }

    @DeleteMapping("/{id}")
    public Message<PicConfig> deleteById(@PathVariable Long id) {
        boolean b = picConfigService.removeById(id);
        return Message.success();
    }

}