public IActionResult Like(int id) { try { var userID = int.Parse(_userManager.GetUserId(HttpContext.User));//Alterar passar id da sessao var bookExiste = _context.BookLike.FirstOrDefault(x => x.BookID == id && x.UserId == userID); BookLike like = new BookLike(); if (bookExiste == null) { like.BookID = id; like.LikeDate = DateTime.Now; like.UserId = userID; like.LikeEnabled = true; _context.BookLike.Add(like); } else { bookExiste.LikeEnabled = !bookExiste.LikeEnabled; _context.BookLike.Update(bookExiste); } //var bookLike = await _context.BookLike.FindAsync(id); _context.SaveChanges(); return(Json("Salvo com sucesso!")); }catch (Exception ex) { return(Json(ex.Message)); } }
public async Task AddBookLike(string bookId, string userId) { var bookLike = new BookLike { Id = Guid.NewGuid().ToString(), BookId = bookId, UserId = userId, Book = _applicationContext.Books.FindAsync(bookId).Result, User = _applicationContext.Users.FindAsync(userId).Result }; await _applicationContext.BookLikes.AddAsync(bookLike); await _applicationContext.SaveChangesAsync(); }