コード例 #1
0
        public async Task <IActionResult> EditAuction(int id)
        {
            var auction = await context.auctions
                          .Where(auction => auction.id == id).FirstOrDefaultAsync();

            CreateEditAuctionModel model = new CreateEditAuctionModel()
            {
                id             = id,
                name           = auction.name,
                description    = auction.description,
                starting_price = auction.starting_price,
                opens_at       = auction.opens_at,
                closes_at      = auction.closes_at
            };

            TempData["editable"] = auction.state == state.DRAFT;

            return(View(model));
        }
コード例 #2
0
        public async Task <IActionResult> EditAuction(CreateEditAuctionModel model)
        {
            if (model == null)
            {
                return(NotFound());
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var auction = await context.auctions
                          .Where(auction => auction.id == model.id).FirstOrDefaultAsync();

            auction.name        = model.name;
            auction.description = model.description;

            using (BinaryReader reader = new BinaryReader(model.image.OpenReadStream())) {
                auction.image = reader.ReadBytes(Convert.ToInt32(reader.BaseStream.Length));
            }

            auction.starting_price = model.starting_price;

            if (model.opens_at > model.closes_at)
            {
                ModelState.AddModelError("", "Auction must be opened before it is closed!");
                return(View(model));
            }

            auction.opens_at = new DateTime(model.opens_at.Year, model.opens_at.Month, model.opens_at.Day,
                                            model.opens_at_time.Hour, model.opens_at_time.Minute, model.opens_at_time.Second);

            auction.closes_at = new DateTime(model.closes_at.Year, model.closes_at.Month, model.closes_at.Day,
                                             model.closes_at_time.Hour, model.closes_at_time.Minute, model.closes_at_time.Second);

            await context.SaveChangesAsync();

            TempData["button"] = "success";
            TempData["action"] = string.Format("{0} successfully updated!", auction.name);

            return(RedirectToAction(nameof(UserController.MyAuctions), "User"));
        }
コード例 #3
0
        public async Task <IActionResult> CreateAuction(CreateEditAuctionModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            User user = await user_manager.GetUserAsync(base.User);

            if (model.opens_at > model.closes_at)
            {
                ModelState.AddModelError("", "Auction must open before it is closed!");
                return(View(model));
            }

            DateTime opens_at = new DateTime(model.opens_at.Year, model.opens_at.Month, model.opens_at.Day,
                                             model.opens_at_time.Hour, model.opens_at_time.Minute, model.opens_at_time.Second);

            DateTime closes_at = new DateTime(model.closes_at.Year, model.closes_at.Month, model.closes_at.Day,
                                              model.closes_at_time.Hour, model.closes_at_time.Minute, model.closes_at_time.Second);

            using (BinaryReader reader = new BinaryReader(model.image.OpenReadStream())) {
                Auction auction = new Auction()
                {
                    name           = model.name,
                    description    = model.description,
                    image          = reader.ReadBytes(Convert.ToInt32(reader.BaseStream.Length)),
                    created_at     = DateTime.Now,
                    opens_at       = opens_at,
                    closes_at      = closes_at,
                    starting_price = model.starting_price,
                    user_id        = user.Id
                };

                await context.auctions.AddAsync(auction);

                await context.SaveChangesAsync();
            }
            TempData["button"] = "success";
            TempData["action"] = string.Format("{0} successfully created!", model.name);

            return(RedirectToAction(nameof(UserController.MyAuctions), "User"));
        }