Ejemplo n.º 1
0
        public ActionResult ViewBookComment(string id)
        {
            var model = new MultipleModel.CommentsVM();

            model.Book = db.Books.Where(b => b.Id == new Guid(id) && b.Delete == false).SingleOrDefault();
            return(PartialView("_ViewBookComment", model));
        }
Ejemplo n.º 2
0
        public ActionResult AddNewComment(MultipleModel.CommentsVM request)
        {
            var errorList = new List <string>();

            if (ModelState.IsValid)
            {
                var getUser = db.Users.Where(u => u.Id == request.NewComment.UserId && u.Deleted == false).SingleOrDefault();
                if (getUser != null)
                {
                    var getBook = db.Books.Where(b => b.Id == request.NewComment.BookId && b.Delete == false).SingleOrDefault();
                    if (getBook != null)
                    {
                        if (!(db.Comments.Where(c => c.UserId == getUser.Id && c.BookId == getBook.Id).Where(c => c.Comment.ToLower() == request.NewComment.Comment.ToLower()).Any()))
                        {
                            var newComment = db.Comments.Create();
                            newComment.Book      = getBook;
                            newComment.User      = getUser;
                            newComment.Comment   = request.NewComment.Comment;
                            newComment.CreatedAt = DateTime.UtcNow;

                            db.Comments.Add(newComment);
                            db.SaveChanges();
                            ModelState.Clear();
                            request.Error = false;
                            string message = "You have successfully add a new comment";
                            errorList.Add(message);
                            request.Message = errorList;
                        }
                        else if (db.Comments.Where(c => c.UserId == getUser.Id && c.BookId == getBook.Id).Where(c => c.Comment.ToLower() == request.NewComment.Comment.ToLower()).Any())
                        {
                            request.Error = true;
                            string message = "Duplicate comments are not allowed!!";
                            errorList.Add(message);
                            request.Message = errorList;
                        }
                    }
                    else if (getBook == null)
                    {
                        request.Error = true;
                        string message = "The selected book is not existing!!";
                        errorList.Add(message);
                        request.Message = errorList;
                    }
                }
                else if (getUser == null)
                {
                    request.Error = true;
                    string message = "Invalid user!! Kindly sign in again";
                    errorList.Add(message);
                    request.Message = errorList;
                }
            }
            else if (!(ModelState.IsValid))
            {
                request.Error   = true;
                request.Message = CustomValidationMessage.GetErrorList(ViewData.ModelState);
            }
            request.Book = db.Books.Where(b => b.Id == request.NewComment.BookId && b.Delete == false).SingleOrDefault();
            return(PartialView("_ViewBookComment", request));
        }