Repository navigation
Expand file tree
/
Copy pathTopicoController.java
More file actions
208 lines (180 loc) · 8.82 KB
/
Copy pathTopicoController.java
File metadata and controls
208 lines (180 loc) · 8.82 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package com.exemple.forohub.controller;
import com.exemple.forohub.DTO.DatosListadoTopico;
import com.exemple.forohub.DTO.DatosRegistroTopico;
import com.exemple.forohub.DTO.DatosRespuestaRespuestas;
import com.exemple.forohub.DTO.DatosRespuestaTopico;
import com.exemple.forohub.enumerador.Categoria;
import com.exemple.forohub.model.Topico;
import com.exemple.forohub.repository.ITopicoRepository;
import com.exemple.forohub.service.TopicoService;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.transaction.Transactional;
import jakarta.validation.Valid;
import java.net.URI;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
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.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;
@RestController
@RequestMapping("/topico")
@SecurityRequirement(name = "bearer-key")
public class TopicoController {
@Autowired
private ITopicoRepository topicoRepo;
@Autowired
private TopicoService topicoService;
@PostMapping
public ResponseEntity registrarTopico(@RequestBody @Valid DatosRegistroTopico datosRegistroTopico, UriComponentsBuilder uriComponentsBuilder) {
Topico topico = topicoService.registrarTopico(datosRegistroTopico);
if (topico != null) {
DatosRegistroTopico datosRespuestaTopico = new DatosRegistroTopico(
topico.getTitulo(),
topico.getMensaje(),
topico.getAutor().getId(),
topico.getCurso().getId()
);
URI url = uriComponentsBuilder.path("/topico/{id}").buildAndExpand(topico.getId()).toUri();
return ResponseEntity.created(url).body(datosRespuestaTopico);
} else {
return ResponseEntity.status(HttpStatus.CONFLICT).body("Ya existe un tópico con ese título y mensaje");
}
}
@GetMapping("/listar")
public ResponseEntity<Page<DatosListadoTopico>> listarTopico(
@PageableDefault(size = 10, sort = "fechaCreacion", direction = Sort.Direction.ASC) Pageable paginacion) {
Page<Topico> topicos = topicoRepo.findByStatusTrue(paginacion);
Page<DatosListadoTopico> datosListadoTopicos = topicos.map(DatosListadoTopico::new);
return ResponseEntity.ok().body(datosListadoTopicos);
}
@GetMapping("/listarPorCurso")
public ResponseEntity<?> listarTopicoPorCursoYFecha(
@RequestParam(required = false) Categoria categoriaPrincipal,
@RequestParam int anio,
@PageableDefault(size = 10, sort = {"curso.categoriaPrincipal", "fechaCreacion"}, direction = Sort.Direction.ASC) Pageable pageable) {
Page<Topico> topicos;
if (categoriaPrincipal != null) {
topicos = topicoRepo.findByCursoCategoriaPrincipalAndAnio(categoriaPrincipal, anio, pageable);
} else {
topicos = topicoRepo.findByFechaCreacionYear(anio, pageable);
}
if (topicos.isEmpty()) {
return ResponseEntity.status(HttpStatus.CONFLICT).body("No se encontraron resultados para la categoría principal proporcionada.");
}
Page<DatosListadoTopico> datosListadoTopicos = topicos.map(DatosListadoTopico::new);
return ResponseEntity.ok().body(datosListadoTopicos);
}
@PutMapping("/actualizar")
@Transactional
public ResponseEntity actualizarTopico(@RequestBody @Valid DatosRegistroTopico.DatosActualizarTopico datosActualizarTopico) {
Optional<Topico> topicoOptional = topicoRepo.findById(datosActualizarTopico.id());
if (topicoOptional.isPresent()) {
Topico topico = topicoOptional.get();
topico.actualizarDatos(datosActualizarTopico);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
List<DatosRespuestaRespuestas> respuestasDTO = topico.getRespuestas().stream().map(respuesta
-> new DatosRespuestaRespuestas(
respuesta.getId(),
respuesta.getMensaje(),
respuesta.getFechaCreacion().format(formatter),
respuesta.getSolucion(),
respuesta.getAutor().getNombre(),
respuesta.getAutor().getPerfil(),
respuesta.getTopico().getTitulo()
)
).collect(Collectors.toList());
DatosRespuestaTopico datosTopico = new DatosRespuestaTopico(
topico.getId(),
topico.getTitulo(),
topico.getMensaje(),
topico.getFechaCreacion().format(formatter),
topico.getAutor().getNombre(),
topico.getCurso().getNombre(),
respuestasDTO
);
return ResponseEntity.ok(datosTopico);
} else {
return ResponseEntity.status(HttpStatus.CONFLICT).body("No se encontró un topico con el id proporcionado.");
}
}
@DeleteMapping("/eliminar/{id}")
@Transactional
public ResponseEntity eliminarTopico(@PathVariable Long id) {
Optional<Topico> topicoOptional = topicoRepo.findById(id);
if (topicoOptional.isPresent()) {
Topico topico = topicoOptional.get();
topicoRepo.deleteById(topico.getId());
return ResponseEntity.ok().body("El topico se eliminó exitosamente");
}
return ResponseEntity.noContent().build();
}
@DeleteMapping("/baja/{id}")
@Transactional
public ResponseEntity darDeBajaTopico(@PathVariable Long id) {
Optional<Topico> topicoOptional = topicoRepo.findById(id);
if (topicoOptional.isPresent()) {
Topico topico = topicoOptional.get();
topico.desactivarTopico();
return ResponseEntity.ok().body("El topico se dió de baja exitosamente");
}
return ResponseEntity.noContent().build();
}
@GetMapping("/alta/{id}")
@Transactional
public ResponseEntity darDeAltaTopico(@PathVariable Long id) {
Optional<Topico> topicoOptional = topicoRepo.findById(id);
if (topicoOptional.isPresent()) {
Topico topico = topicoOptional.get();
topico.activarTopico();
return ResponseEntity.ok().body("El topico se dió de alta exitosamente");
}
return ResponseEntity.noContent().build();
}
@GetMapping("/detalle/{id}")
public ResponseEntity retornarDatosTopico(@PathVariable Long id) {
Optional<Topico> optionalTopico = topicoRepo.findById(id);
if (optionalTopico.isPresent()) {
Topico topico = optionalTopico.get();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
List<DatosRespuestaRespuestas> respuestasDTO = topico.getRespuestas().stream().map(respuesta
-> new DatosRespuestaRespuestas(
respuesta.getId(),
respuesta.getMensaje(),
respuesta.getFechaCreacion().format(formatter),
respuesta.getSolucion(),
respuesta.getAutor().getNombre(),
respuesta.getAutor().getPerfil(),
respuesta.getTopico().getTitulo()
)
).collect(Collectors.toList());
DatosRespuestaTopico datosTopico = new DatosRespuestaTopico(
topico.getId(),
topico.getTitulo(),
topico.getMensaje(),
topico.getFechaCreacion().format(formatter),
topico.getAutor().getNombre(),
topico.getCurso().getNombre(),
respuestasDTO
);
return ResponseEntity.ok(datosTopico);
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("No se encontró ningún tópico con el ID proporcionado.");
}
}
}