private PropositionModel GetPropositionModel(string bookId)
        {
            PropositionModel model = new PropositionModel();
            var context            = new AppDbContext();

            model.YourBooks = context.Users.Find(System.Web.HttpContext.Current.User.Identity.GetUserId()).SellingBooks.Where(x => x.isChanged == false && x.isSold == false).ToList();
            int id = 0;

            Int32.TryParse(bookId, out id);
            model.Interested = context.Books.Find(id);
            return(model);
        }
        public ActionResult Proposition(string bookId)
        {
            PropositionModel model = GetPropositionModel(bookId);

            if (model.Interested == null)
            {
                return(RedirectToAction("Information", "Info", new { text = "AccessDenied" }));
            }
            if (model.Interested.Seller.Id == System.Web.HttpContext.Current.User.Identity.GetUserId())
            {
                return(RedirectToAction("Information", "Info", new { text = "Error" }));
            }
            return(View(model));
        }
        public ActionResult Proposition(PropositionModel model)
        {
            if (model.SelectedBooks == null)
            {
                ModelState.AddModelError("", "Musisz zaproponować co najmniej jedną książkę");
                model = GetPropositionModel(Request.QueryString["bookId"]);
                return(View(model));
            }
            AppUser currentUser;
            int     bookId = 0;

            Int32.TryParse(Request.QueryString["bookId"], out bookId);
            Book interested;
            ICollection <Book> proposedBooks = new List <Book>();

            using (var context = new AppDbContext())
            {
                currentUser = context.Users.Find(System.Web.HttpContext.Current.User.Identity.GetUserId());
                interested  = context.Books.Find(bookId);
                foreach (string id in model.SelectedBooks)
                {
                    proposedBooks.Add(context.Books.Find(Int32.Parse(id)));
                }
                if (interested != null && interested.Changeable)
                {
                    ExchangeMessage eMessage = new ExchangeMessage()
                    {
                        Text          = model.Text,
                        SendDate      = DateTime.Now,
                        SenderId      = currentUser.Id,
                        Sender        = currentUser,
                        ReceiverId    = interested.SellerId,
                        Receiver      = interested.Seller,
                        ForBook       = interested,
                        BookId        = interested.BookId,
                        Accepted      = false,
                        ProposedBooks = proposedBooks
                    };
                    context.ExchangeMessages.Add(eMessage);
                    context.SaveChanges();
                }
                else
                {
                    return(RedirectToAction("Information", "Info", new { text = "Error" }));
                }
            }
            return(RedirectToAction("Book", "Home", new { bookId = bookId }));
        }