-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCohortController.java
More file actions
37 lines (33 loc) · 1.38 KB
/
CohortController.java
File metadata and controls
37 lines (33 loc) · 1.38 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
package com.booleanuk.cohorts.controllers;
import com.booleanuk.cohorts.models.*;
import com.booleanuk.cohorts.payload.response.*;
import com.booleanuk.cohorts.repository.CohortRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("cohorts")
public class CohortController {
@Autowired
private CohortRepository cohortRepository;
@GetMapping
public ResponseEntity<CohortListResponse> getAllCohorts() {
CohortListResponse cohortListResponse = new CohortListResponse();
cohortListResponse.set(this.cohortRepository.findAll());
return ResponseEntity.ok(cohortListResponse);
}
@GetMapping("{id}")
public ResponseEntity<Response> getCohortById(@PathVariable int id) {
Cohort cohort = this.cohortRepository.findById(id).orElse(null);
if (cohort == null || cohort.getUsers().isEmpty()) {
ErrorResponse error = new ErrorResponse();
error.set("not found");
return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);
}
CohortResponse cohortResponse = new CohortResponse();
cohortResponse.set(cohort);
return ResponseEntity.ok(cohortResponse);
}
}