-
Notifications
You must be signed in to change notification settings - Fork 0
/
Courses API Controllers
54 lines (45 loc) · 1.73 KB
/
Courses API Controllers
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
@RestController
@RequestMapping("/api/courses")
public class CourseController {
@Autowired
private CourseRepository courseRepository;
@PostMapping
public Course createCourse(@RequestBody Course course) {
return courseRepository.save(course);
}
@GetMapping
public List<Course> getAllCourses() {
return courseRepository.findAll();
}
@GetMapping("/{id}")
public Course getCourse(@PathVariable Long id) {
return courseRepository.findById(id).orElseThrow();
}
@DeleteMapping("/{id}")
public void deleteCourse(@PathVariable Long id) {
courseRepository.deleteById(id);
}
}
@RestController
@RequestMapping("/api/instances")
public class InstanceController {
@Autowired
private InstanceRepository instanceRepository;
@PostMapping
public Instance createInstance(@RequestBody Instance instance) {
return
instanceRepository.save(instance);
}
@GetMapping("/{year}/{semester}")
public List<Instance> getInstances(@PathVariable Integer year, @PathVariable Integer semester) {
return instanceRepository.findByYearAndSemester(year, semester);
}
@GetMapping("/{year}/{semester}/{id}")
public Instance getInstance(@PathVariable Integer year, @PathVariable Integer semester, @PathVariable Long id) {
return instanceRepository.findByIdAndYearAndSemester(id, year, semester).orElseThrow();
}
@DeleteMapping("/{year}/{semester}/{id}")
public void deleteInstance(@PathVariable Integer year, @PathVariable Integer semester, @PathVariable Long id) {
instanceRepository.deleteByIdAndYearAndSemester(id, year, semester);
}
}