예제 #1
0
        public async Task <ActionResult <Manga_Favorito> > DeleteManga_Favoritos(int mangaId)
        {
            Claim correo = User.Claims.FirstOrDefault(c => c.Type == "Email");

            if (correo != null)
            {
                Usuario usuario = await _userManager.FindByEmailAsync(correo.Value);

                Manga_Favorito manga_Favorito = await _context.Manga_Favorito.Where(af => af.UsuarioId == usuario.Id && af.MangaID == mangaId).FirstOrDefaultAsync();

                if (manga_Favorito == null)
                {
                    return(NotFound(new ApiResponseFormat()
                    {
                        Estado = StatusCodes.Status404NotFound, Mensaje = "No esta en tu lista de favoritos"
                    }));
                }

                try
                {
                    _context.Manga_Favorito.Remove(manga_Favorito);
                    await _context.SaveChangesAsync();

                    return(Ok(new ApiResponseFormat()
                    {
                        Estado = StatusCodes.Status200OK, Mensaje = $"Manga {manga_Favorito.Manga.Nombre} eliminado de favoritos"
                    }));
                }
                catch (Exception ex)
                {
                    return(BadRequest(new ApiResponseFormat()
                    {
                        Estado = ex.HResult, Mensaje = ex.Message
                    }));
                }
            }

            return(Unauthorized(new ApiResponseFormat()
            {
                Estado = StatusCodes.Status401Unauthorized, Mensaje = "Inicie sesion para usar esta opcion."
            }));
        }
예제 #2
0
        public async Task <ActionResult <Manga_Favorito> > PostManga_Favoritos(int mangaId)
        {
            Claim correo = User.Claims.FirstOrDefault(c => c.Type == "Email");

            if (correo != null)
            {
                Usuario usuario = await _userManager.FindByEmailAsync(correo.Value);

                Manga manga = _context.Mangas.Find(mangaId);

                Manga_Favorito af = new Manga_Favorito()
                {
                    Usuario  = usuario,
                    Manga    = manga,
                    Favorito = true
                };

                try
                {
                    _context.Add(af);
                    await _context.SaveChangesAsync();

                    return(Ok(new ApiResponseFormat()
                    {
                        Estado = StatusCodes.Status201Created, Mensaje = $"{manga.Nombre} agregado a favoritos", Dato = af.Favorito
                    }));
                }
                catch (Exception ex)
                {
                    return(BadRequest(new ApiResponseFormat()
                    {
                        Estado = ex.HResult, Mensaje = ex.Message
                    }));
                }
            }

            return(Unauthorized(new { Result = StatusCodes.Status401Unauthorized, Data = "Inicie sesion para usar esta opcion." }));
        }
예제 #3
0
        public async Task <ActionResult <Manga_Favorito> > GetManga_Favorito(int mangaId)
        {
            Claim correo = User.Claims.FirstOrDefault(c => c.Type == "Email");

            if (correo != null)
            {
                Manga_Favorito favorito = await _context.Manga_Favorito.Where(ef => ef.Usuario.Email == correo.Value && ef.MangaID == mangaId).FirstOrDefaultAsync();

                if (favorito == null)
                {
                    favorito = new Manga_Favorito()
                    {
                        Favorito = false
                    };
                }

                return(Ok(favorito.Favorito));
            }

            return(Unauthorized(new ApiResponseFormat()
            {
                Estado = StatusCodes.Status401Unauthorized, Mensaje = "Inicie sesion para usar esta funcion."
            }));
        }