package vn.azteam.tpf.web.rest;

import com.codahale.metrics.annotation.Timed;
import vn.azteam.tpf.service.TBSuppliesUsingDetailsService;
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.TBSuppliesUsingDetailsDTO;
import vn.azteam.tpf.service.dto.TBSuppliesUsingDetailsCriteria;
import vn.azteam.tpf.service.TBSuppliesUsingDetailsQueryService;
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 TBSuppliesUsingDetails.
 */
@RestController
@RequestMapping("/api")
public class TBSuppliesUsingDetailsResource {

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

    private static final String ENTITY_NAME = "tBSuppliesUsingDetails";

    private final TBSuppliesUsingDetailsService tBSuppliesUsingDetailsService;

    private final TBSuppliesUsingDetailsQueryService tBSuppliesUsingDetailsQueryService;

    public TBSuppliesUsingDetailsResource(TBSuppliesUsingDetailsService tBSuppliesUsingDetailsService, TBSuppliesUsingDetailsQueryService tBSuppliesUsingDetailsQueryService) {
        this.tBSuppliesUsingDetailsService = tBSuppliesUsingDetailsService;
        this.tBSuppliesUsingDetailsQueryService = tBSuppliesUsingDetailsQueryService;
    }

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

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

    /**
     * GET  /tb-supplies-using-details : get all the tBSuppliesUsingDetails.
     *
     * @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 tBSuppliesUsingDetails in body
     */
    @GetMapping("/tb-supplies-using-details")
    @Timed
    public ResponseEntity<List<TBSuppliesUsingDetailsDTO>> getAllTBSuppliesUsingDetails(TBSuppliesUsingDetailsCriteria criteria, Pageable pageable) {
        log.debug("REST request to get TBSuppliesUsingDetails by criteria: {}", criteria);
        Page<TBSuppliesUsingDetailsDTO> page = tBSuppliesUsingDetailsQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-supplies-using-details");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
    * GET  /tb-supplies-using-details/count : count all the tBSuppliesUsingDetails.
    *
    * @param criteria the criterias which the requested entities should match
    * @return the ResponseEntity with status 200 (OK) and the count in body
    */
    @GetMapping("/tb-supplies-using-details/count")
    @Timed
    public ResponseEntity<Long> countTBSuppliesUsingDetails(TBSuppliesUsingDetailsCriteria criteria) {
        log.debug("REST request to count TBSuppliesUsingDetails by criteria: {}", criteria);
        return ResponseEntity.ok().body(tBSuppliesUsingDetailsQueryService.countByCriteria(criteria));
    }

    /**
     * GET  /tb-supplies-using-details/:id : get the "id" tBSuppliesUsingDetails.
     *
     * @param id the id of the tBSuppliesUsingDetailsDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the tBSuppliesUsingDetailsDTO, or with status 404 (Not Found)
     */
    @GetMapping("/tb-supplies-using-details/{id}")
    @Timed
    public ResponseEntity<TBSuppliesUsingDetailsDTO> getTBSuppliesUsingDetails(@PathVariable Long id) {
        log.debug("REST request to get TBSuppliesUsingDetails : {}", id);
        Optional<TBSuppliesUsingDetailsDTO> tBSuppliesUsingDetailsDTO = tBSuppliesUsingDetailsService.findOne(id);
        return ResponseUtil.wrapOrNotFound(tBSuppliesUsingDetailsDTO);
    }

    /**
     * DELETE  /tb-supplies-using-details/:id : delete the "id" tBSuppliesUsingDetails.
     *
     * @param id the id of the tBSuppliesUsingDetailsDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/tb-supplies-using-details/{id}")
    @Timed
    public ResponseEntity<Void> deleteTBSuppliesUsingDetails(@PathVariable Long id) {
        log.debug("REST request to delete TBSuppliesUsingDetails : {}", id);
        tBSuppliesUsingDetailsService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

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

}
