-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCursoController.java
More file actions
58 lines (49 loc) · 2.4 KB
/
Copy pathCursoController.java
File metadata and controls
58 lines (49 loc) · 2.4 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
57
58
package com.exemple.forohub.controller;
import com.exemple.forohub.DTO.DatosListadoCurso;
import com.exemple.forohub.DTO.DatosRegistroCurso;
import com.exemple.forohub.model.Curso;
import com.exemple.forohub.repository.ICursoRepository;
import com.exemple.forohub.service.CursoService;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.validation.Valid;
import java.net.URI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;
@RestController
@RequestMapping("/curso")
@SecurityRequirement(name = "bearer-key")
public class CursoController {
@Autowired
private CursoService cursoService;
@Autowired
private ICursoRepository cursoRepo;
@PostMapping("/registrar")
public ResponseEntity registrarCurso(@RequestBody @Valid DatosRegistroCurso datosRegistroCurso, UriComponentsBuilder uriComponentsBuilder) {
System.out.println(datosRegistroCurso.subcategoria());
Curso curso = cursoService.registrarCurso(datosRegistroCurso);
System.out.println(curso.getSubcategoria());
DatosRegistroCurso datosRespuesta = new DatosRegistroCurso(
curso.getNombre(),
curso.getCategoriaPrincipal(),
curso.getSubcategoria());
URI url = uriComponentsBuilder.path("/curso/{id}").buildAndExpand(curso.getId()).toUri();
return ResponseEntity.created(url).body(datosRegistroCurso);
}
@GetMapping("/listar")
public ResponseEntity<Page<DatosListadoCurso>> listarCursos(
@PageableDefault(size = 10, sort = "nombre", direction = Sort.Direction.ASC) Pageable paginacion) {
Page<Curso> curso = cursoRepo.findByStatusTrue(paginacion);
Page<DatosListadoCurso> datosListadoCurso = curso.map(DatosListadoCurso::new);
return ResponseEntity.ok().body(datosListadoCurso);
}
}