package com.purui.prims.headless.server.rest;

import com.purui.prims.auth.api.authentication.pojo.User;
import com.purui.prims.auth.api.authentication.utils.UserHolder;
import com.purui.prims.headless.api.pojo.request.DecoratorReq;
import com.purui.prims.headless.api.pojo.response.DecoratorResp;
import com.purui.prims.headless.server.service.DecoratorService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping({"/api/semantic/decorator"})
public class DecoratorController {

    private final DecoratorService decoratorService;

    public DecoratorController(DecoratorService decoratorService) {
        this.decoratorService = decoratorService;
    }

    @ApiOperation(value = "新建/修改修饰词")
    @PostMapping
    public boolean save(@RequestBody DecoratorReq decoratorReq, HttpServletRequest request,
                        HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        return decoratorService.saveOrUpdate(decoratorReq, user);
    }

    @GetMapping("/getDecorator/{id}")
    public DecoratorResp getDecorator(@PathVariable int id) {
        return decoratorService.getDecorator(id);
    }

    @ApiOperation(value = "修饰词列表")
    @PostMapping("/list")
    public List<DecoratorResp> getDecorators(@RequestBody DecoratorReq decoratorReq) {
        return decoratorService.getDecorators(decoratorReq);
    }
}
