-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCursoController.java
More file actions
56 lines (44 loc) · 1.81 KB
/
CursoController.java
File metadata and controls
56 lines (44 loc) · 1.81 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
56
package br.com.openenade.api.curso;
import java.util.List;
import java.util.Optional;
import javax.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import br.com.openenade.api.modalidade.Modalidade;
@RestController
@CrossOrigin("*")
@RequestMapping(path = CursoController.ENDPOINT)
public class CursoController {
public static final String ENDPOINT = "cursos";
@Autowired
private CursoService service;
@PostMapping
public Curso postCurso(@Valid @RequestBody Curso newCurso) {
return this.service.save(newCurso);
}
@GetMapping
@ResponseBody
public ResponseEntity<List<Curso>> getAll() {
return new ResponseEntity<>(this.service.getAll(), HttpStatus.OK);
}
@ResponseBody
@GetMapping(path = "/{codigo}/{modalidade}")
public ResponseEntity<Curso> getCursoByCodigo(@PathVariable(name = "codigo") Long codigo,
@PathVariable(name = "modalidade") Modalidade modalidade) {
Optional<Curso> optCurso = this.service.getByCodigo(codigo, modalidade);
if (optCurso.isPresent()) {
return new ResponseEntity<>(optCurso.get(), HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
}