<commit_before>package com.nokia.springboot.training.d02.s05.controller;

import com.nokia.springboot.training.d02.s05.service.ProductService;
import com.nokia.springboot.training.d02.s05.model.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;

/**
 * A Spring {@link RestController} used to showcase the modeling of a REST controller for CRUD operations
 *
 * @author bogdan.solga
 */
@RestController
@RequestMapping(
        path = "/product"
)
public class ProductController {

    private final Executor executor;
    private final ProductService productService;

    @Autowired
    public ProductController(final Executor executor, final ProductService productService) {
        this.executor = executor; this.productService = productService;
    }

    /**
     * Creates the referenced {@link Product}
     *
     * @param product the {@link Product} to be created
     *
     * @return a {@link ResponseEntity} with the appropriate {@link HttpStatus}
     */
    @RequestMapping(
            method = RequestMethod.POST,
            path = ""
    )
    public ResponseEntity create(@RequestBody Product product) {
        productService.create(product);
        return ResponseEntity.ok(HttpStatus.OK);
    }

    /**
     * Reads the {@link Product} with the specified id
     *
     * @param id the id of the requested {@link Product}
     *
     * @return the serialized {@link Product}
     */
    @RequestMapping(
            method = RequestMethod.GET,
            path = "/{id}"
    )
    public DeferredResult<Product> getProduct(@PathVariable final int id)
            throws ExecutionException, InterruptedException {
        final CompletableFuture<Product> futureProduct = CompletableFuture.supplyAsync(() -> productService.get(id), executor);

        final DeferredResult<Product> deferredResult = new DeferredResult<>();
        deferredResult.setResult(futureProduct.get());

        return deferredResult;
    }

    /**
     * Reads all the existing {@link Product}s
     *
     * @return the serialized {@link Product}s
     */
    @RequestMapping(
            method = RequestMethod.GET,
            path = ""
    )
    public Iterable<Product> getAll() {
        return productService.getAll();
    }

    /**
     * Updates the {@link Product} with the specified ID with the details from the referenced {@link Product}
     *
     * @param id the ID of the updated {@link Product}<commit_msg>[clean] Removed the executor usage<commit_after>package com.nokia.springboot.training.d02.s05.controller;

import com.nokia.springboot.training.d02.s05.service.ProductService;
import com.nokia.springboot.training.d02.s05.model.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * A Spring {@link RestController} used to showcase the modeling of a REST controller for CRUD operations
 *
 * @author bogdan.solga
 */
@RestController
@RequestMapping(
        path = "/product"
)
public class ProductController {

    private final ProductService productService;

    @Autowired
    public ProductController(final ProductService productService) {
        this.productService = productService;
    }

    /**
     * Creates the referenced {@link Product}
     *
     * @param product the {@link Product} to be created
     *
     * @return a {@link ResponseEntity} with the appropriate {@link HttpStatus}
     */
    @RequestMapping(
            method = RequestMethod.POST,
            path = ""
    )
    public ResponseEntity create(@RequestBody Product product) {
        productService.create(product);
        return ResponseEntity.ok(HttpStatus.OK);
    }

    /**
     * Reads the {@link Product} with the specified id
     *
     * @param id the id of the requested {@link Product}
     *
     * @return the serialized {@link Product}
     */
    @RequestMapping(
            method = RequestMethod.GET,
            path = "/{id}"
    )
    public DeferredResult<Product> getProduct(@PathVariable final int id)
            throws ExecutionException, InterruptedException {
        final CompletableFuture<Product> futureProduct = CompletableFuture.supplyAsync(() -> productService.get(id));

        final DeferredResult<Product> deferredResult = new DeferredResult<>();
        deferredResult.setResult(futureProduct.get());

        return deferredResult;
    }

    /**
     * Reads all the existing {@link Product}s
     *
     * @return the serialized {@link Product}s
     */
    @RequestMapping(
            method = RequestMethod.GET,
            path = ""
    )
    public Iterable<Product> getAll() {
        return productService.getAll();
    }

    /**
     * Updates the {@link Product} with the specified ID with the details from the referenced {@link Product}
     *
     * @param id the ID of the updated {@link Product}
{'fraction_non_alphanumeric': 0.09697375020899515, 'fraction_numerical': 0.004012706905199799, 'mean_word_length': 3.291248206599713, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5942111', 'n_tokens_mistral': 1585, 'n_tokens_neox': 1544, 'n_words': 399}