Example #1
0
        public ActionResult SuggestExchange(ExchangeModel model)
        {
            if (ModelState.IsValid)
            {
                ApplicationUser user = db.Users.Find(UserId);
                model.CreateBooksList(db, UserId);
                // если такая заявка уже существует
                var bid = db.Bids.ToList().Find(x => (x.UserCreate == user &&
                    (x.WantedBook != null && x.WantedBook.ID == model.WantedBookID) &&
                    x.SuggestedBook == model.Books.Find(y => y.Title == model.Title)));
                if (bid != null)
                {
                    return View("NewBid", bid);
                }

                bid = new Bid
                {
                    Status = EStatus.Created,
                    CreateAt = DateTime.Now,
                    IsChecked = false,
                    UserCreate = user,
                    WantedBook = db.Books.Find(model.WantedBookID),
                    SuggestedBook = model.Books.Find(y => y.Title == model.Title)
                };

                Book wantedBook = db.Books.Find(bid.WantedBook.ID);
                Book suggestedBook = db.Books.Find(bid.SuggestedBook.ID);

                db.Bids.Add(bid);
                wantedBook.Bids.Add(bid);
                db.Entry(wantedBook).State = EntityState.Modified;

                db.SaveChanges();

                bid = db.Bids.ToList().Find(x => (x.WantedBook == wantedBook && x.SuggestedBook == suggestedBook));

                return View("NewBid", bid);
            }

            model.CreateBooksList(db, UserId);
            return View(model);
        }
Example #2
0
        public ActionResult SuggestExchange(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Book book = db.Books.Find(id);
            if (book == null)
            {
                return HttpNotFound();
            }
            var model = new ExchangeModel()
            {
                WantedBookID = book.ID
            };
            model.CreateBooksList(db, UserId);

            return View(model);
        }