package it.sw.pa.comune.artegna.web.rest;

import it.sw.pa.comune.artegna.repository.ModelloLiberatoriaRepository;
import it.sw.pa.comune.artegna.service.ModelloLiberatoriaService;
import it.sw.pa.comune.artegna.service.dto.ModelloLiberatoriaDTO;
import it.sw.pa.comune.artegna.web.rest.errors.BadRequestAlertException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import tech.jhipster.web.util.HeaderUtil;
import tech.jhipster.web.util.ResponseUtil;

/**
 * REST controller for managing {@link it.sw.pa.comune.artegna.domain.ModelloLiberatoria}.
 */
@RestController
@RequestMapping("/api/modello-liberatorias")
public class ModelloLiberatoriaResource {

    private static final Logger LOG = LoggerFactory.getLogger(ModelloLiberatoriaResource.class);

    private static final String ENTITY_NAME = "modelloLiberatoria";

    @Value("${jhipster.clientApp.name:smartbooking}")
    private String applicationName;

    private final ModelloLiberatoriaService modelloLiberatoriaService;

    private final ModelloLiberatoriaRepository modelloLiberatoriaRepository;

    public ModelloLiberatoriaResource(
        ModelloLiberatoriaService modelloLiberatoriaService,
        ModelloLiberatoriaRepository modelloLiberatoriaRepository
    ) {
        this.modelloLiberatoriaService = modelloLiberatoriaService;
        this.modelloLiberatoriaRepository = modelloLiberatoriaRepository;
    }

    /**
     * {@code POST  /modello-liberatorias} : Create a new modelloLiberatoria.
     *
     * @param modelloLiberatoriaDTO the modelloLiberatoriaDTO to create.
     * @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new modelloLiberatoriaDTO, or with status {@code 400 (Bad Request)} if the modelloLiberatoria has already an ID.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("")
    public ResponseEntity<ModelloLiberatoriaDTO> createModelloLiberatoria(@RequestBody ModelloLiberatoriaDTO modelloLiberatoriaDTO)
        throws URISyntaxException {
        LOG.debug("REST request to save ModelloLiberatoria : {}", modelloLiberatoriaDTO);
        if (modelloLiberatoriaDTO.getId() != null) {
            throw new BadRequestAlertException("A new modelloLiberatoria cannot already have an ID", ENTITY_NAME, "idexists");
        }
        modelloLiberatoriaDTO = modelloLiberatoriaService.save(modelloLiberatoriaDTO);
        return ResponseEntity.created(new URI("/api/modello-liberatorias/" + modelloLiberatoriaDTO.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(applicationName, true, ENTITY_NAME, modelloLiberatoriaDTO.getId().toString()))
            .body(modelloLiberatoriaDTO);
    }

    /**
     * {@code PUT  /modello-liberatorias/:id} : Updates an existing modelloLiberatoria.
     *
     * @param id the id of the modelloLiberatoriaDTO to save.
     * @param modelloLiberatoriaDTO the modelloLiberatoriaDTO to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated modelloLiberatoriaDTO,
     * or with status {@code 400 (Bad Request)} if the modelloLiberatoriaDTO is not valid,
     * or with status {@code 500 (Internal Server Error)} if the modelloLiberatoriaDTO couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PutMapping("/{id}")
    public ResponseEntity<ModelloLiberatoriaDTO> updateModelloLiberatoria(
        @PathVariable(value = "id", required = false) final Long id,
        @RequestBody ModelloLiberatoriaDTO modelloLiberatoriaDTO
    ) throws URISyntaxException {
        LOG.debug("REST request to update ModelloLiberatoria : {}, {}", id, modelloLiberatoriaDTO);
        if (modelloLiberatoriaDTO.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        if (!Objects.equals(id, modelloLiberatoriaDTO.getId())) {
            throw new BadRequestAlertException("Invalid ID", ENTITY_NAME, "idinvalid");
        }

        if (!modelloLiberatoriaRepository.existsById(id)) {
            throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
        }

        modelloLiberatoriaDTO = modelloLiberatoriaService.update(modelloLiberatoriaDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(applicationName, true, ENTITY_NAME, modelloLiberatoriaDTO.getId().toString()))
            .body(modelloLiberatoriaDTO);
    }

    /**
     * {@code PATCH  /modello-liberatorias/:id} : Partial updates given fields of an existing modelloLiberatoria, field will ignore if it is null
     *
     * @param id the id of the modelloLiberatoriaDTO to save.
     * @param modelloLiberatoriaDTO the modelloLiberatoriaDTO to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated modelloLiberatoriaDTO,
     * or with status {@code 400 (Bad Request)} if the modelloLiberatoriaDTO is not valid,
     * or with status {@code 404 (Not Found)} if the modelloLiberatoriaDTO is not found,
     * or with status {@code 500 (Internal Server Error)} if the modelloLiberatoriaDTO couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PatchMapping(value = "/{id}", consumes = { "application/json", "application/merge-patch+json" })
    public ResponseEntity<ModelloLiberatoriaDTO> partialUpdateModelloLiberatoria(
        @PathVariable(value = "id", required = false) final Long id,
        @RequestBody ModelloLiberatoriaDTO modelloLiberatoriaDTO
    ) throws URISyntaxException {
        LOG.debug("REST request to partial update ModelloLiberatoria partially : {}, {}", id, modelloLiberatoriaDTO);
        if (modelloLiberatoriaDTO.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        if (!Objects.equals(id, modelloLiberatoriaDTO.getId())) {
            throw new BadRequestAlertException("Invalid ID", ENTITY_NAME, "idinvalid");
        }

        if (!modelloLiberatoriaRepository.existsById(id)) {
            throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
        }

        Optional<ModelloLiberatoriaDTO> result = modelloLiberatoriaService.partialUpdate(modelloLiberatoriaDTO);

        return ResponseUtil.wrapOrNotFound(
            result,
            HeaderUtil.createEntityUpdateAlert(applicationName, true, ENTITY_NAME, modelloLiberatoriaDTO.getId().toString())
        );
    }

    /**
     * {@code GET  /modello-liberatorias} : get all the modelloLiberatorias.
     *
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of modelloLiberatorias in body.
     */
    @GetMapping("")
    public List<ModelloLiberatoriaDTO> getAllModelloLiberatorias() {
        LOG.debug("REST request to get all ModelloLiberatorias");
        return modelloLiberatoriaService.findAll();
    }

    /**
     * {@code GET  /modello-liberatorias/:id} : get the "id" modelloLiberatoria.
     *
     * @param id the id of the modelloLiberatoriaDTO to retrieve.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the modelloLiberatoriaDTO, or with status {@code 404 (Not Found)}.
     */
    @GetMapping("/{id}")
    public ResponseEntity<ModelloLiberatoriaDTO> getModelloLiberatoria(@PathVariable("id") Long id) {
        LOG.debug("REST request to get ModelloLiberatoria : {}", id);
        Optional<ModelloLiberatoriaDTO> modelloLiberatoriaDTO = modelloLiberatoriaService.findOne(id);
        return ResponseUtil.wrapOrNotFound(modelloLiberatoriaDTO);
    }

    /**
     * {@code DELETE  /modello-liberatorias/:id} : delete the "id" modelloLiberatoria.
     *
     * @param id the id of the modelloLiberatoriaDTO to delete.
     * @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteModelloLiberatoria(@PathVariable("id") Long id) {
        LOG.debug("REST request to delete ModelloLiberatoria : {}", id);
        modelloLiberatoriaService.delete(id);
        return ResponseEntity.noContent()
            .headers(HeaderUtil.createEntityDeletionAlert(applicationName, true, ENTITY_NAME, id.toString()))
            .build();
    }
}
