/*
 *  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 easydo.technology.system.rest;

import java.io.IOException;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import easydo.technology.annotation.WebLog;
import easydo.technology.system.domain.Dict;
import easydo.technology.system.dto.DictQueryParam;
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.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import easydo.technology.exception.BadRequestException;
import easydo.technology.system.service.DictService;
import lombok.RequiredArgsConstructor;

/**
* @author Zheng Jie
* @date 2019-04-10
*/
@RestController
@RequiredArgsConstructor
@RequestMapping("/dict")
public class DictController {

    private final DictService dictService;
    private static final String ENTITY_NAME = "dict";

    @WebLog
    @GetMapping(value = "/download")
    public void download(HttpServletResponse response, DictQueryParam criteria) throws IOException {
        dictService.download(dictService.queryAll(criteria), response);
    }

    @WebLog
    @GetMapping(value = "/all")
    public ResponseEntity<Object> queryAll(){
        return new ResponseEntity<>(dictService.queryAll(new DictQueryParam()),HttpStatus.OK);
    }

    @WebLog
    @GetMapping
    public ResponseEntity<Object> query(DictQueryParam query, Pageable pageable){
        return new ResponseEntity<>(dictService.queryAll(query,pageable),HttpStatus.OK);
    }

    @WebLog
    @PostMapping
    public ResponseEntity<Object> create(@Validated @RequestBody Dict resources){
        if (resources.getId() != null) {
            throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
        }
        dictService.save(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @WebLog
    @PutMapping
    public ResponseEntity<Object> update(@RequestBody Dict resources){
        dictService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @WebLog
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Set<Long> ids){
        dictService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}