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