content: package com.adyen.demo.store.web.rest;
  
  import java.net.URI;
  import java.net.URISyntaxException;
  import java.util.List;
  import java.util.Objects;
  import java.util.Optional;
  import javax.validation.Valid;
  import javax.validation.constraints.NotNull;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.data.domain.Page;
  import org.springframework.data.domain.Pageable;
  import org.springframework.http.HttpHeaders;
  import org.springframework.http.ResponseEntity;
  import org.springframework.security.access.prepost.PreAuthorize;
  import org.springframework.web.bind.annotation.*;
  import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
  import com.adyen.demo.store.domain.Product;
  import com.adyen.demo.store.repository.ProductRepository;
  import com.adyen.demo.store.security.AuthoritiesConstants;
  import com.adyen.demo.store.service.ProductService;
  import com.adyen.demo.store.web.rest.errors.BadRequestAlertException;
  
  import tech.jhipster.web.util.HeaderUtil;
  import tech.jhipster.web.util.PaginationUtil;
  import tech.jhipster.web.util.ResponseUtil;
  
  /**
   * REST controller for managing {@link com.adyen.demo.store.domain.Product}.
   */
  @RestController
  @RequestMapping("/api")
  public class ProductResource {
  
      private final Logger log = LoggerFactory.getLogger(ProductResource.class);
  
      private static final String ENTITY_NAME = "product";
  
      @Value("${jhipster.clientApp.name}")
      private String applicationName;
  
      private final ProductService productService;
  
      private final ProductRepository productRepository;
  
      public ProductResource(ProductService productService, ProductRepository productRepository) {
          this.productService = productService;
          this.productRepository = productRepository;
      }
  
      /**
       * {@code POST  /products} : Create a new product.
       *
       * @param product the product to create.
       * @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new product, or with status {@code 400 (Bad Request)} if the product has already an ID.
       * @throws URISyntaxException if the Location URI syntax is incorrect.
       */
      @PostMapping("/products")
      @PreAuthorize("hasAuthority(\"" + AuthoritiesConstants.ADMIN + "\")")
      public ResponseEntity<Product> createProduct(@Valid @RequestBody Product product) throws URISyntaxException {
          log.debug("REST request to save Product : {}", product);
          if (product.getId() != null) {
              throw new BadRequestAlertException("A new product cannot already have an ID", ENTITY_NAME, "idexists");
          }
          Product result = productService.save(product);
          return ResponseEntity
              .created(new URI("/api/products/" + result.getId()))
              .headers(HeaderUtil.createEntityCreationAlert(applicationName, false, ENTITY_NAME, result.getId().toString()))
              .body(result);
      }
  
      /**
       * {@code PUT  /products/:id} : Updates an existing product.
       *
       * @param id      the id of the product to save.
       * @param product the product to update.
       * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated product,
       * or with status {@code 400 (Bad Request)} if the product is not valid,
       * or with status {@code 500 (Internal Server Error)} if the product couldn't be updated.
       * @throws URISyntaxException if the Location URI syntax is incorrect.
       */
      @PutMapping("/products/{id}")
      @PreAuthorize("hasAuthority(\"" + AuthoritiesConstants.ADMIN + "\")")
      public ResponseEntity<Product> updateProduct(
          @PathVariable(value = "id", required = false) final Long id,
          @Valid @RequestBody Product product
      ) throws URISyntaxException {
          log.debug("REST request to update Product : {}, {}", id, product);
          if (product.getId() == null) {
              throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
          }
          if (!Objects.equals(id, product.getId())) {
              throw new BadRequestAlertException("Invalid ID", ENTITY_NAME, "idinvalid");
          }
  
          if (!productRepository.existsById(id)) {
              throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
          }
  
          Product result = productService.save(product);
          return ResponseEntity
              .ok()
              .headers(HeaderUtil.createEntityUpdateAlert(applicationName, false, ENTITY_NAME, product.getId().toString()))
              .body(result);
      }
  
      /**
       * {@code PATCH  /products/:id} : Partial updates given fields of an existing product, field will ignore if it is null
       *
       * @param id      the id of the product to save.
       * @param product the product to update.
       * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated product,
       * or with status {@code 400 (Bad Request)} if the product is not valid,
       * or with status {@code 404 (Not Found)} if the product is not found,
       * or with status {@code 500 (Internal Server Error)} if the product couldn't be updated.
       * @throws URISyntaxException if the Location URI syntax is incorrect.
       */
      @PatchMapping(value = "/products/{id}", consumes = "application/merge-patch+json")
      public ResponseEntity<Product> partialUpdateProduct(
          @PathVariable(value = "id", required = false) final Long id,
          @NotNull @RequestBody Product product
      ) throws URISyntaxException {
          log.debug("REST request to partial update Product partially : {}, {}", id, product);
          if (product.getId() == null) {
              throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
          }
          if (!Objects.equals(id, product.getId())) {
              throw new BadRequestAlertException("Invalid ID", ENTITY_NAME, "idinvalid");
          }
  
          if (!productRepository.existsById(id)) {
              throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
          }
  
          Optional<Product> result = productService.partialUpdate(product);
  
          return ResponseUtil.wrapOrNotFound(
              result,
              HeaderUtil.createEntityUpdateAlert(applicationName, false, ENTITY_NAME, product.getId().toString())
          );
      }
  
      /**
       * {@code GET  /products} : get all the products.
       *
       * @param pageable the pagination information.
       * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of products in body.
       */
      @GetMapping("/products")
      public ResponseEntity<List<Product>> getAllProducts(Pageable pageable) {
          log.debug("REST request to get a page of Products");
          Page<Product> page = productService.findAll(pageable);
          HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page);
          return ResponseEntity.ok().headers(headers).body(page.getContent());
      }
  
      /**
       * {@code GET  /products/:id} : get the "id" product.
       *
       * @param id the id of the product to retrieve.
       * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the product, or with status {@code 404 (Not Found)}.
       */
      @GetMapping("/products/{id}")
      public ResponseEntity<Product> getProduct(@PathVariable Long id) {
          log.debug("REST request to get Product : {}", id);
          Optional<Product> product = productService.findOne(id);
          return ResponseUtil.wrapOrNotFound(product);
      }
  
      /**
       * {@code DELETE  /products/:id} : delete the "id" product.
       *
       * @param id the id of the product to delete.
       * @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
       */
      @DeleteMapping("/products/{id}")
      @PreAuthorize("hasAuthority(\"" + AuthoritiesConstants.ADMIN + "\")")
      public ResponseEntity<Void> deleteProduct(@PathVariable Long id) {
          log.debug("REST request to delete Product : {}", id);
          productService.delete(id);
          return ResponseEntity
              .noContent()
              .headers(HeaderUtil.createEntityDeletionAlert(applicationName, false, ENTITY_NAME, id.toString()))
              .build();
      }
  }
  