/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.dm.dmLane.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.dm.dmLane.domain.DmLane;
import me.zhengjie.modules.dm.dmLane.service.DmLaneService;
import me.zhengjie.modules.dm.dmLane.service.dto.DmLaneQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @website https://el-admin.vip
* @author zhirong
* @date 2021-11-10
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "lane管理")
@RequestMapping("/api/dmLane")
public class DmLaneController {

    private final DmLaneService dmLaneService;

    @GetMapping
    @Log("查询lane")
    @ApiOperation("查询lane")
    @PreAuthorize("@el.check('dmLane:list')")
    public ResponseEntity<Object> query(DmLaneQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(dmLaneService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增lane")
    @ApiOperation("新增lane")
    @PreAuthorize("@el.check('dmLane:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody DmLane resources){
        return new ResponseEntity<>(dmLaneService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改lane")
    @ApiOperation("修改lane")
    @PreAuthorize("@el.check('dmLane:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody DmLane resources) throws IOException {
        dmLaneService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除lane")
    @ApiOperation("删除lane")
    @PreAuthorize("@el.check('dmLane:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Integer[] ids) {
        dmLaneService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}