public IActionResult Favorite(int id)
 {
     try
     {
         var userID                 = int.Parse(_userManager.GetUserId(HttpContext.User));//Alterar passar id da sessao
         var newspaperExiste        = _context.FavoriteNewspapers.FirstOrDefault(x => x.NewspaperID == id && x.UserID == userID);
         FavoriteNewspaper favorite = new FavoriteNewspaper();
         if (newspaperExiste == null)
         {
             favorite.NewspaperID     = id;
             favorite.FavoriteDate    = DateTime.Now;
             favorite.UserID          = userID;
             favorite.FavoriteEnabled = true;
             _context.FavoriteNewspapers.Add(favorite);
         }
         else
         {
             newspaperExiste.FavoriteEnabled = !newspaperExiste.FavoriteEnabled;
             _context.FavoriteNewspapers.Update(newspaperExiste);
         }
         //var bookLike = await _context.BookLike.FindAsync(id);
         _context.SaveChanges();
         return(Json("Marcado como favorito!"));
     }
     catch (Exception ex)
     {
         return(Json(ex.Message));
     }
 }
Ejemplo n.º 2
0
 public IActionResult Like(int id)
 {
     try
     {
         var            userID           = int.Parse(_userManager.GetUserId(HttpContext.User));//Alterar passar id da sessao
         var            multimediaExiste = _context.MultimediaLikes.FirstOrDefault(x => x.MultimediaID == id && x.UserId == userID);
         MultimediaLike like             = new MultimediaLike();
         if (multimediaExiste == null)
         {
             like.MultimediaID = id;
             like.LikeDate     = DateTime.Now;
             like.UserId       = userID;
             like.LikeEnabled  = true;
             _context.MultimediaLikes.Add(like);
         }
         else
         {
             multimediaExiste.LikeEnabled = !multimediaExiste.LikeEnabled;
             _context.MultimediaLikes.Update(multimediaExiste);
         }
         //var bookLike = await _context.BookLike.FindAsync(id);
         _context.SaveChanges();
         return(Json("Salvo com sucesso!"));
     }
     catch (Exception ex)
     {
         return(Json(ex.Message));
     }
 }
        public async Task <IActionResult> Edit(int id, List <IFormFile> AvatarImage, [Bind("FirstName,LastName,User_CPF,URLProfilePicture, AvatarImage, Id,UserName,NormalizedUserName,Email,NormalizedEmail,EmailConfirmed,PasswordHash,SecurityStamp,ConcurrencyStamp,PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEnd,LockoutEnabled,AccessFailedCount")] ApplicationUser applicationUser)
        {
            if (id != applicationUser.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var obj = _context.ApplicationUsers.FirstOrDefault(x => x.Id == id);
                    obj.FirstName         = applicationUser.FirstName;
                    obj.Email             = applicationUser.Email;
                    obj.LastName          = applicationUser.LastName;
                    obj.User_CPF          = applicationUser.User_CPF;
                    obj.URLProfilePicture = applicationUser.URLProfilePicture;

                    foreach (var item in AvatarImage)
                    {
                        if (item.Length > 0)
                        {
                            using (var stream = new MemoryStream())
                            {
                                await item.CopyToAsync(stream);

                                obj.AvatarImage = stream.ToArray();
                            }
                        }
                    }

                    _context.Update(obj);
                    _context.SaveChanges();
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    if (!ApplicationUserExists(applicationUser.Id))
                    {
                        return(Json(ex.Message));
                    }
                    else
                    {
                        throw;
                    }
                }
                //return RedirectToAction(nameof(Index));
                return(RedirectToAction("Details", "ApplicationUsers", new { id = id }));
            }

            return(View(applicationUser));
        }