package com.purui.prims.auth.authentication.rest;

import com.purui.prims.auth.api.authentication.pojo.User;
import com.purui.prims.auth.api.authentication.request.RoleReq;
import com.purui.prims.auth.api.authentication.response.RoleResp;
import com.purui.prims.auth.api.authentication.service.RoleService;
import com.purui.prims.auth.api.authentication.utils.UserHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.DeleteMapping;
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/role")
@Api(tags = "角色管理")
public class RoleController {

    private final RoleService roleService;

    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    @ApiOperation(value = "角色创建/修改")
    @PostMapping("/saveOrUpdate")
    public boolean saveOrUpdate(@RequestBody RoleReq roleReq,
                             HttpServletRequest request,
                             HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        roleService.saveOrUpdate(roleReq, user);
        return true;
    }

    @ApiOperation(value = "角色列表")
    @GetMapping
    public List<RoleResp> getList() {
        return roleService.getRoles();
    }

    @ApiOperation(value = "删除角色")
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable("id") Long id) {
        roleService.delete(id);
        return true;
    }

}
