-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathDepartmentController.java
More file actions
55 lines (44 loc) · 2.33 KB
/
Copy pathDepartmentController.java
File metadata and controls
55 lines (44 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.booleanuk.controllers;
import com.booleanuk.models.Department;
import com.booleanuk.repositories.DepartmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import java.util.List;
@RestController
@RequestMapping("departments")
public class DepartmentController {
@Autowired
private DepartmentRepository departmentRepository;
@GetMapping
public ResponseEntity<List<Department>> getAllDepartments() {
return ResponseEntity.ok(this.departmentRepository.findAll());
}
@PostMapping
public ResponseEntity<Department> createDepartment(@RequestBody Department department) {
return new ResponseEntity<>(this.departmentRepository.save(department), HttpStatus.CREATED);
}
@GetMapping("{id}")
public ResponseEntity<Department> getDepartmentById(@PathVariable int id) {
Department department = this.departmentRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Did not find any department with the given id"));
return ResponseEntity.ok(department);
}
@PutMapping("{id}")
public ResponseEntity<Department> updateDepartment(@PathVariable int id, @RequestBody Department department) {
Department departmentToUpdate = this.departmentRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Did not find any department with the given id to update"));
departmentToUpdate.setName(department.getName());
departmentToUpdate.setLocation(department.getLocation());
return new ResponseEntity<>(this.departmentRepository.save(departmentToUpdate), HttpStatus.CREATED);
}
@DeleteMapping("{id}")
public ResponseEntity<Department> deleteDepartment(@PathVariable int id) {
Department departmentToDelete = this.departmentRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Did not find any department with the given id to delete"));
this.departmentRepository.delete(departmentToDelete);
return ResponseEntity.ok(departmentToDelete);
}
}