public ActionResult RecommendBookForm(Guid id) { var model = new MultipleModel.RecommendBookVM(); model.Book = db.Books.Where(b => b.Id == id).Where(b => b.Delete == false).SingleOrDefault(); return(PartialView("_RecommendBookForm", model)); }
public ActionResult RecommendBook(MultipleModel.RecommendBookVM req) { var model = new MultipleModel.RecommendBookVM(); var messageList = new List <string>(); var identity = (System.Security.Claims.ClaimsIdentity)User.Identity; var getUserId = identity.Claims.Where(c => c.Type == System.Security.Claims.ClaimTypes.NameIdentifier).Select(c => c.Value).SingleOrDefault(); model.Error = true; if (ModelState.IsValid) { var getUser = db.Users.Where(u => u.Email == req.Recommend.Email).Where(u => u.Deleted == false).SingleOrDefault(); if (getUser != null) { if (!(db.BookRecommendations.Where(b => b.FromUserId == new Guid(getUserId)).Where(b => b.ToUserId == getUser.Id).Where(b => b.BookId == req.Recommend.BookId).Where(b => b.Deleted == false).Any())) { var newRecommend = db.BookRecommendations.Create(); newRecommend.BookId = req.Recommend.BookId; newRecommend.ToUserId = getUser.Id; newRecommend.FromUserId = new Guid(getUserId); newRecommend.CreatedAt = DateTime.UtcNow; newRecommend.Deleted = false; db.BookRecommendations.Add(newRecommend); db.SaveChanges(); string message = "You have successfully recommend this book to " + getUser.Email; model.Error = false; messageList.Add(message); } else { string message = "You have already recommended this book to " + req.Recommend.Email; messageList.Add(message); } } else { string message = "The user is not existing!!"; messageList.Add(message); } } else { messageList = CustomValidationMessage.GetErrorList(ViewData.ModelState); } model.Message = messageList; model.Book = db.Books.Where(b => b.Id == req.Recommend.BookId).Where(b => b.Delete == false).SingleOrDefault(); return(PartialView("_RecommendBookForm", model)); }