コード例 #1
0
        public async Task <IActionResult> setearPrincipal(int userId, int id)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var user = await _repo.GetUser(userId);

            if (!user.Fotos.Any(p => p.Id == id))
            {
                return(Unauthorized());
            }

            var fotoRepo = await _repo.GetFoto(id);

            if (fotoRepo.EsPrincipal)
            {
                return(BadRequest("La foto ya es la principal"));
            }

            var fotoPrincipalActual = await _repo.GetFotoPrincipal(userId);

            fotoPrincipalActual.EsPrincipal = false;

            fotoRepo.EsPrincipal = true;

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            return(BadRequest("No se pudo poner la foto como principal"));
        }