Beispiel #1
0
        public ActionResult Buy(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Book book = db.Books.Find(id);
            if (book == null)
            {
                return HttpNotFound();
            }

            // оформление заявки на покупку книги

            ApplicationUser user = db.Users.Find(UserId);
            // если такая заявка уже существует
            var bid = db.Bids.ToList().Find(x => (x.UserCreate == user && x.BookToBuy == book));
            if (bid!=null)
            {
                return View("NewBid", bid);
            }

            bid = new Bid
            {
                Status = EStatus.Created,
                IsChecked = false,
                CreateAt = DateTime.Now,
                UserCreate = user,
                BookToBuy = book
            };

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

            db.SaveChanges();

            bid = db.Bids.ToList().Find(x => x.BookToBuy == book);

            return View("NewBid", bid);
        }
Beispiel #2
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);
        }