package com.its.op.controller.its.database;

import com.its.op.dto.its.link.TbLinkDto;
import com.its.op.service.its.link.TbLinkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.NoSuchElementException;

@Api(tags = "09.기초데이터관리-02.링크")
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/database/link")
public class TbLinkController {

    private final TbLinkService service;

    @ApiOperation(value = "링크정보 전체조회(TB_LINK)", response = TbLinkDto.class, responseContainer = "ArrayList")
    @GetMapping(value = "", produces = {"application/json; charset=utf8"})
    public List<TbLinkDto> findAll() {
        return service.findAll();
    }

    @ApiOperation(value = "링크정보 전체조회-삭제되지 않은 정보(TB_LINK)", response = TbLinkDto.class, responseContainer = "ArrayList")
    @GetMapping(value = "/list", produces = {"application/json; charset=utf8"})
    public List<TbLinkDto> findAllList() {
        return service.findAllList();
    }

    @ApiOperation(value = "링크정보 전체조회-지역센터(TB_LINK)", response = TbLinkDto.class, responseContainer = "ArrayList")
    @GetMapping(value = "/local", produces = {"application/json; charset=utf8"})
    public List<TbLinkDto> findLocalAll() {
        return service.findLocalAll();
    }

    @ApiOperation(value = "링크정보 개별조회(TB_LINK)", response = TbLinkDto.class)
    @GetMapping(value = "/{id}", produces = {"application/json; charset=utf8"})
    public TbLinkDto findById(@PathVariable final Long id) throws NoSuchElementException {
        try {
            return this.service.findById(id);
        }
        catch (NoSuchElementException e) {
            throw e;
        }
    }

    @ApiOperation(value = "링크정보 도로명변경(TB_LINK)", response = TbLinkDto.class)
    @PutMapping(value = "/name/{id}", produces = {"application/json; charset=utf8"})
    public TbLinkDto updateNameById(@PathVariable final Long id, @RequestBody @Valid final TbLinkDto.TbLinkNameUpdReq req)
        throws NoSuchElementException {
        try {
            return this.service.updateNameById(id, req);
        }
        catch (NoSuchElementException e) {
            throw e;
        }
    }

    @ApiOperation(value = "링크정보 구간등급 변경(TB_LINK)", response = TbLinkDto.class)
    @PutMapping(value = "/sect-grad/{id}", produces = {"application/json; charset=utf8"})
    public TbLinkDto updateSectGradById(@PathVariable final Long id, @RequestBody @Valid final TbLinkDto.TbLinkSectGradUpdReq req)
        throws NoSuchElementException {
        try {
            return this.service.updateSectGradById(id, req);
        }
        catch (NoSuchElementException e) {
            throw e;
        }
    }

}
