package com.bares.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.bares.service.ArticuloCategoriaService;
import com.bares.web.rest.errors.BadRequestAlertException;
import com.bares.web.rest.util.HeaderUtil;
import com.bares.web.rest.util.PaginationUtil;
import com.bares.service.dto.ArticuloCategoriaDTO;
import com.bares.service.dto.ArticuloCategoriaCriteria;
import com.bares.service.ArticuloCategoriaQueryService;
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;

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

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

    private static final String ENTITY_NAME = "articuloCategoria";

    private final ArticuloCategoriaService articuloCategoriaService;

    private final ArticuloCategoriaQueryService articuloCategoriaQueryService;

    public ArticuloCategoriaResource(ArticuloCategoriaService articuloCategoriaService, ArticuloCategoriaQueryService articuloCategoriaQueryService) {
        this.articuloCategoriaService = articuloCategoriaService;
        this.articuloCategoriaQueryService = articuloCategoriaQueryService;
    }

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

    /**
     * PUT  /articulo-categorias : Updates an existing articuloCategoria.
     *
     * @param articuloCategoriaDTO the articuloCategoriaDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated articuloCategoriaDTO,
     * or with status 400 (Bad Request) if the articuloCategoriaDTO is not valid,
     * or with status 500 (Internal Server Error) if the articuloCategoriaDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/articulo-categorias")
    @Timed
    public ResponseEntity<ArticuloCategoriaDTO> updateArticuloCategoria(@RequestBody ArticuloCategoriaDTO articuloCategoriaDTO) throws URISyntaxException {
        log.debug("REST request to update ArticuloCategoria : {}", articuloCategoriaDTO);
        if (articuloCategoriaDTO.getId() == null) {
            return createArticuloCategoria(articuloCategoriaDTO);
        }
        ArticuloCategoriaDTO result = articuloCategoriaService.save(articuloCategoriaDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, articuloCategoriaDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /articulo-categorias : get all the articuloCategorias.
     *
     * @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 articuloCategorias in body
     */
    @GetMapping("/articulo-categorias")
    @Timed
    public ResponseEntity<List<ArticuloCategoriaDTO>> getAllArticuloCategorias(ArticuloCategoriaCriteria criteria, Pageable pageable) {
        log.debug("REST request to get ArticuloCategorias by criteria: {}", criteria);
        Page<ArticuloCategoriaDTO> page = articuloCategoriaQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/articulo-categorias");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /articulo-categorias/:id : get the "id" articuloCategoria.
     *
     * @param id the id of the articuloCategoriaDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the articuloCategoriaDTO, or with status 404 (Not Found)
     */
    @GetMapping("/articulo-categorias/{id}")
    @Timed
    public ResponseEntity<ArticuloCategoriaDTO> getArticuloCategoria(@PathVariable Long id) {
        log.debug("REST request to get ArticuloCategoria : {}", id);
        Optional<ArticuloCategoriaDTO> articuloCategoriaDTO = articuloCategoriaService.findOne(id);
        return ResponseUtil.wrapOrNotFound(articuloCategoriaDTO);
    }

    /**
     * DELETE  /articulo-categorias/:id : delete the "id" articuloCategoria.
     *
     * @param id the id of the articuloCategoriaDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/articulo-categorias/{id}")
    @Timed
    public ResponseEntity<Void> deleteArticuloCategoria(@PathVariable Long id) {
        log.debug("REST request to delete ArticuloCategoria : {}", id);
        articuloCategoriaService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
