Commit 88f01d4a by Rubén Ramírez

feat: [SeguimientoController]: Añadidas las funciones para la red social

parent 6e80b2d4
package com.ujaen.tfg.mangaffinity.rest;
import com.ujaen.tfg.mangaffinity.entidades.Usuario;
import com.ujaen.tfg.mangaffinity.excepciones.SeguimientoExiste;
import com.ujaen.tfg.mangaffinity.excepciones.SeguimientoNoExiste;
import com.ujaen.tfg.mangaffinity.excepciones.UsuarioNoExiste;
import com.ujaen.tfg.mangaffinity.rest.DTO.DTOUsuario;
import com.ujaen.tfg.mangaffinity.rest.DTO.Mapper;
import com.ujaen.tfg.mangaffinity.servicios.ServicioSeguimiento;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;
@RestController
@RequestMapping("/seguimientos")
public class SeguimientoController {
@Autowired
private ServicioSeguimiento servicioSeguimientos;
@Autowired
private Mapper mapper;
/**
* Permite a un usuario seguir a otro.
*/
@PostMapping("/{nombreSeguido}")
public ResponseEntity<?> seguir(@RequestHeader("nombreUsuario") String nombreSeguidor,
@PathVariable String nombreSeguido) {
try {
servicioSeguimientos.seguir(nombreSeguidor, nombreSeguido);
return ResponseEntity.status(HttpStatus.CREATED).build();
} catch (SeguimientoExiste e) {
return ResponseEntity.status(HttpStatus.CONFLICT).build();
} catch (UsuarioNoExiste | IllegalArgumentException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
/**
* Permite dejar de seguir a un usuario.
*/
@DeleteMapping("/{nombreSeguido}")
public ResponseEntity<?> dejarDeSeguir(@RequestHeader("nombreUsuario") String nombreSeguidor,
@PathVariable String nombreSeguido) {
try {
servicioSeguimientos.dejarDeSeguir(nombreSeguidor, nombreSeguido);
return ResponseEntity.noContent().build();
} catch (SeguimientoNoExiste e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
} catch (UsuarioNoExiste e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
/**
* Lista de usuarios que sigue un usuario.
*/
@GetMapping("/seguidos/{nombreUsuario}")
public ResponseEntity<List<DTOUsuario>> obtenerSeguidos(@PathVariable String nombreUsuario) {
try {
List<Usuario> seguidos = servicioSeguimientos.obtenerSeguidos(nombreUsuario);
List<DTOUsuario> dto = seguidos.stream().map(mapper::dto).collect(Collectors.toList());
return ResponseEntity.ok(dto);
} catch (UsuarioNoExiste e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
}
/**
* Lista de seguidores de un usuario.
*/
@GetMapping("/seguidores/{nombreUsuario}")
public ResponseEntity<List<DTOUsuario>> obtenerSeguidores(@PathVariable String nombreUsuario) {
try {
List<Usuario> seguidores = servicioSeguimientos.obtenerSeguidores(nombreUsuario);
List<DTOUsuario> dto = seguidores.stream().map(mapper::dto).collect(Collectors.toList());
return ResponseEntity.ok(dto);
} catch (UsuarioNoExiste e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
}
/**
* Verifica si un usuario sigue a otro.
*/
@GetMapping("/existe")
public ResponseEntity<Boolean> estaSiguiendo(@RequestParam String seguidor,
@RequestParam String seguido) {
try {
boolean resultado = servicioSeguimientos.estaSiguiendo(seguidor, seguido);
return ResponseEntity.ok(resultado);
} catch (UsuarioNoExiste e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment