CaptionController.java 2.12 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.Caption;
import com.viontech.storage.service.CaptionService;
import com.viontech.storage.vo.CaptionVo;
import org.springframework.web.bind.annotation.*;

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

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

@RequestMapping("captions")
@RestController
public class CaptionController {

    @Resource
    private CaptionService captionService;

    @GetMapping("/{id}")
    public Message<Caption> getById(@PathVariable("id") Long id) {
        Caption byId = captionService.getById(id);
        CaptionVo copy = CaptionVo.copy(byId);
        return Message.success(copy);
    }

    @GetMapping
    public Message<List<Caption>> listAll(CaptionVo captionVo) {
        List<Caption> list = captionService.lambdaQuery().eq(Caption::getCaptionSetId, captionVo.getCaptionSetId()).list();
        List<Caption> collect = list.stream().map(CaptionVo::copy).collect(Collectors.toList());
        return Message.success(collect);
    }

    @PostMapping
    public Message<Caption> add(@RequestBody CaptionVo item) {
        List<Context> texts = item.getContexts();
        if (CollectionUtil.isNotEmpty(texts)) {
            item.setContext(JSONUtil.toJsonStr(texts));
        }
        boolean save = captionService.save(item);
        return Message.success(item);
    }

    @PutMapping("/{id}")
    public Message<Caption> updateById(@PathVariable Long id, @RequestBody CaptionVo item) {
        List<Context> texts = item.getContexts();
        if (texts != null) {
            item.setContext(JSONUtil.toJsonStr(texts));
        }
        item.setId(id);
        captionService.updateById(item);
        return Message.success(item);
    }

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