content: package com.example.demo.web.rest;
  
  import com.example.demo.domain.Task;
  import com.example.demo.repository.TaskRepository;
  import com.example.demo.service.TaskService;
  import com.example.demo.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 com.example.demo.domain.Task}.
   */
  @RestController
  @RequestMapping("/api")
  public class TaskResource {
  
      private final Logger log = LoggerFactory.getLogger(TaskResource.class);
  
      private static final String ENTITY_NAME = "task";
  
      @Value("${jhipster.clientApp.name}")
      private String applicationName;
  
      private final TaskService taskService;
  
      private final TaskRepository taskRepository;
  
      public TaskResource(TaskService taskService, TaskRepository taskRepository) {
          this.taskService = taskService;
          this.taskRepository = taskRepository;
      }
  
      /**
       * {@code POST  /tasks} : Create a new task.
       *
       * @param task the task to create.
       * @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new task, or with status {@code 400 (Bad Request)} if the task has already an ID.
       * @throws URISyntaxException if the Location URI syntax is incorrect.
       */
      @PostMapping("/tasks")
      public ResponseEntity<Task> createTask(@RequestBody Task task) throws URISyntaxException {
          log.debug("REST request to save Task : {}", task);
          if (task.getId() != null) {
              throw new BadRequestAlertException("A new task cannot already have an ID", ENTITY_NAME, "idexists");
          }
          Task result = taskService.save(task);
          return ResponseEntity
              .created(new URI("/api/tasks/" + result.getId()))
              .headers(HeaderUtil.createEntityCreationAlert(applicationName, true, ENTITY_NAME, result.getId().toString()))
              .body(result);
      }
  
      /**
       * {@code PUT  /tasks/:id} : Updates an existing task.
       *
       * @param id the id of the task to save.
       * @param task the task to update.
       * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated task,
       * or with status {@code 400 (Bad Request)} if the task is not valid,
       * or with status {@code 500 (Internal Server Error)} if the task couldn't be updated.
       * @throws URISyntaxException if the Location URI syntax is incorrect.
       */
      @PutMapping("/tasks/{id}")
      public ResponseEntity<Task> updateTask(@PathVariable(value = "id", required = false) final Long id, @RequestBody Task task)
          throws URISyntaxException {
          log.debug("REST request to update Task : {}, {}", id, task);
          if (task.getId() == null) {
              throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
          }
          if (!Objects.equals(id, task.getId())) {
              throw new BadRequestAlertException("Invalid ID", ENTITY_NAME, "idinvalid");
          }
  
          if (!taskRepository.existsById(id)) {
              throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
          }
  
          Task result = taskService.save(task);
          return ResponseEntity
              .ok()
              .headers(HeaderUtil.createEntityUpdateAlert(applicationName, true, ENTITY_NAME, task.getId().toString()))
              .body(result);
      }
  
      /**
       * {@code PATCH  /tasks/:id} : Partial updates given fields of an existing task, field will ignore if it is null
       *
       * @param id the id of the task to save.
       * @param task the task to update.
       * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated task,
       * or with status {@code 400 (Bad Request)} if the task is not valid,
       * or with status {@code 404 (Not Found)} if the task is not found,
       * or with status {@code 500 (Internal Server Error)} if the task couldn't be updated.
       * @throws URISyntaxException if the Location URI syntax is incorrect.
       */
      @PatchMapping(value = "/tasks/{id}", consumes = { "application/json", "application/merge-patch+json" })
      public ResponseEntity<Task> partialUpdateTask(@PathVariable(value = "id", required = false) final Long id, @RequestBody Task task)
          throws URISyntaxException {
          log.debug("REST request to partial update Task partially : {}, {}", id, task);
          if (task.getId() == null) {
              throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
          }
          if (!Objects.equals(id, task.getId())) {
              throw new BadRequestAlertException("Invalid ID", ENTITY_NAME, "idinvalid");
          }
  
          if (!taskRepository.existsById(id)) {
              throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
          }
  
          Optional<Task> result = taskService.partialUpdate(task);
  
          return ResponseUtil.wrapOrNotFound(
              result,
              HeaderUtil.createEntityUpdateAlert(applicationName, true, ENTITY_NAME, task.getId().toString())
          );
      }
  
      /**
       * {@code GET  /tasks} : get all the tasks.
       *
       * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of tasks in body.
       */
      @GetMapping("/tasks")
      public List<Task> getAllTasks() {
          log.debug("REST request to get all Tasks");
          return taskService.findAll();
      }
  
      /**
       * {@code GET  /tasks/:id} : get the "id" task.
       *
       * @param id the id of the task to retrieve.
       * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the task, or with status {@code 404 (Not Found)}.
       */
      @GetMapping("/tasks/{id}")
      public ResponseEntity<Task> getTask(@PathVariable Long id) {
          log.debug("REST request to get Task : {}", id);
          Optional<Task> task = taskService.findOne(id);
          return ResponseUtil.wrapOrNotFound(task);
      }
  
      /**
       * {@code DELETE  /tasks/:id} : delete the "id" task.
       *
       * @param id the id of the task to delete.
       * @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
       */
      @DeleteMapping("/tasks/{id}")
      public ResponseEntity<Void> deleteTask(@PathVariable Long id) {
          log.debug("REST request to delete Task : {}", id);
          taskService.delete(id);
          return ResponseEntity
              .noContent()
              .headers(HeaderUtil.createEntityDeletionAlert(applicationName, true, ENTITY_NAME, id.toString()))
              .build();
      }
  }
  