package vn.azteam.tpf.web.rest;

import com.codahale.metrics.annotation.Timed;
import vn.azteam.tpf.service.TBLimitsService;
import vn.azteam.tpf.web.rest.errors.BadRequestAlertException;
import vn.azteam.tpf.web.rest.util.HeaderUtil;
import vn.azteam.tpf.web.rest.util.PaginationUtil;
import vn.azteam.tpf.service.dto.TBLimitsDTO;
import vn.azteam.tpf.service.dto.TBLimitsCriteria;
import vn.azteam.tpf.service.TBLimitsQueryService;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing TBLimits.
 */
@RestController
@RequestMapping("/api")
public class TBLimitsResource {

    private final Logger log = LoggerFactory.getLogger(TBLimitsResource.class);

    private static final String ENTITY_NAME = "tBLimits";

    private final TBLimitsService tBLimitsService;

    private final TBLimitsQueryService tBLimitsQueryService;

    public TBLimitsResource(TBLimitsService tBLimitsService, TBLimitsQueryService tBLimitsQueryService) {
        this.tBLimitsService = tBLimitsService;
        this.tBLimitsQueryService = tBLimitsQueryService;
    }

    /**
     * POST  /tb-limits : Create a new tBLimits.
     *
     * @param tBLimitsDTO the tBLimitsDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new tBLimitsDTO, or with status 400 (Bad Request) if the tBLimits has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/tb-limits")
    @Timed
    public ResponseEntity<TBLimitsDTO> createTBLimits(@RequestBody TBLimitsDTO tBLimitsDTO) throws URISyntaxException {
        log.debug("REST request to save TBLimits : {}", tBLimitsDTO);
        if (tBLimitsDTO.getId() != null) {
            throw new BadRequestAlertException("A new tBLimits cannot already have an ID", ENTITY_NAME, "idexists");
        }
        TBLimitsDTO result = tBLimitsService.save(tBLimitsDTO);
        return ResponseEntity.created(new URI("/api/tb-limits/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /tb-limits : Updates an existing tBLimits.
     *
     * @param tBLimitsDTO the tBLimitsDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated tBLimitsDTO,
     * or with status 400 (Bad Request) if the tBLimitsDTO is not valid,
     * or with status 500 (Internal Server Error) if the tBLimitsDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/tb-limits")
    @Timed
    public ResponseEntity<TBLimitsDTO> updateTBLimits(@RequestBody TBLimitsDTO tBLimitsDTO) throws URISyntaxException {
        log.debug("REST request to update TBLimits : {}", tBLimitsDTO);
        if (tBLimitsDTO.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        TBLimitsDTO result = tBLimitsService.save(tBLimitsDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, tBLimitsDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /tb-limits : get all the tBLimits.
     *
     * @param pageable the pagination information
     * @param criteria the criterias which the requested entities should match
     * @return the ResponseEntity with status 200 (OK) and the list of tBLimits in body
     */
    @GetMapping("/tb-limits")
    @Timed
    public ResponseEntity<List<TBLimitsDTO>> getAllTBLimits(TBLimitsCriteria criteria, Pageable pageable) {
        log.debug("REST request to get TBLimits by criteria: {}", criteria);
        Page<TBLimitsDTO> page = tBLimitsQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-limits");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
    * GET  /tb-limits/count : count all the tBLimits.
    *
    * @param criteria the criterias which the requested entities should match
    * @return the ResponseEntity with status 200 (OK) and the count in body
    */
    @GetMapping("/tb-limits/count")
    @Timed
    public ResponseEntity<Long> countTBLimits(TBLimitsCriteria criteria) {
        log.debug("REST request to count TBLimits by criteria: {}", criteria);
        return ResponseEntity.ok().body(tBLimitsQueryService.countByCriteria(criteria));
    }

    /**
     * GET  /tb-limits/:id : get the "id" tBLimits.
     *
     * @param id the id of the tBLimitsDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the tBLimitsDTO, or with status 404 (Not Found)
     */
    @GetMapping("/tb-limits/{id}")
    @Timed
    public ResponseEntity<TBLimitsDTO> getTBLimits(@PathVariable Long id) {
        log.debug("REST request to get TBLimits : {}", id);
        Optional<TBLimitsDTO> tBLimitsDTO = tBLimitsService.findOne(id);
        return ResponseUtil.wrapOrNotFound(tBLimitsDTO);
    }

    /**
     * DELETE  /tb-limits/:id : delete the "id" tBLimits.
     *
     * @param id the id of the tBLimitsDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/tb-limits/{id}")
    @Timed
    public ResponseEntity<Void> deleteTBLimits(@PathVariable Long id) {
        log.debug("REST request to delete TBLimits : {}", id);
        tBLimitsService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/tb-limits?query=:query : search for the tBLimits corresponding
     * to the query.
     *
     * @param query the query of the tBLimits search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/tb-limits")
    @Timed
    public ResponseEntity<List<TBLimitsDTO>> searchTBLimits(@RequestParam String query, Pageable pageable) {
        log.debug("REST request to search for a page of TBLimits for query {}", query);
        Page<TBLimitsDTO> page = tBLimitsService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/tb-limits");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

}
