示例#1
0
        public async Task <IActionResult> Create(CategoryVM categoryVM)
        {
            ViewBag.CategoryAdd  = "Failed";
            categoryVM.CreatedAt = DateTime.Now;
            if (categoryVM != null)
            {
                var category = CategoryUtility.MapVMtoModel(categoryVM);

                //stop admin add 3rd category
                if (category.ParentId == null)
                {
                    category.Level = 1;
                }
                else if (CategoryExists(category.ParentId) == true)
                {
                    category.Level = 2;
                }

                db.Add(category);
                await db.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            return(View(categoryVM));
        }
示例#2
0
        public async Task <bool> Create(AuctionHouseCreateInputModel inputModel)
        {
            City cityFromDb =
                context
                .Cities
                .FirstOrDefault(city => city.Name == inputModel.City);

            if (cityFromDb == null)
            {
                throw new ArgumentNullException(nameof(cityFromDb));
            }

            AuctionHouse auctionHouse = new AuctionHouse
            {
                Name        = inputModel.Name,
                Address     = inputModel.Address,
                Description = inputModel.Description,
            };

            auctionHouse.City = cityFromDb;

            context.AuctionHouses.Add(auctionHouse);
            int result = await context
                         .SaveChangesAsync();

            return(result > 0);
        }
示例#3
0
        public async Task <IActionResult> Create(ItemVM itemVM)
        {
            ViewBag.ItemAdd  = "Failed";
            itemVM.CreatedAt = DateTime.Now;
            itemVM.Status    = false;
            if (itemVM != null)
            {
                string fileName = await UploadFile.UploadAsync(webHostEnvironment, itemVM.Photo, photoPath);

                string documentName = await UploadFile.UploadAsync(webHostEnvironment, itemVM.Document, documentPath);

                var item = ItemUtility.MapVMToModel(itemVM);

                item.AccountId     = (int)HttpContext.Session.GetInt32("checkiduser");
                item.MinimumBid    = itemVM.MinimumBid;
                item.Photo         = fileName;
                item.Document      = documentName;
                item.CategoryItems = new List <CategoryItem>();

                foreach (var id in itemVM.SelectedCategoryIds)
                {
                    item.CategoryItems.Add(new CategoryItem
                    {
                        CategoryId = id,
                        ItemId     = item.Id
                    });
                }
                db.Items.Add(item);
                await db.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["AccountId"] = new SelectList(db.Accounts, "Id", "Id", itemVM.AccountId);
            return(View(itemVM));
        }
示例#4
0
        public async Task <IActionResult> Create([Bind("CategoryId,Name")] Category category)
        {
            if (ModelState.IsValid)
            {
                _context.Add(category);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }
        public async Task <IActionResult> Create([Bind("AuthorId,FirstName,LastName,DateOfBirth")] Author author)
        {
            if (ModelState.IsValid)
            {
                _context.Add(author);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(author));
        }
示例#6
0
        public async Task <IActionResult> Create(ItemVM itemVM)
        {
            if (ModelState.IsValid)
            {
                int?bidSession = null;

                var latestSessionBid = db.Bids
                                       .Where(x => x.ItemId == itemVM.Id)
                                       .OrderByDescending(x => x.BidSession)
                                       .FirstOrDefault();

                if (latestSessionBid == null)
                {
                    bidSession = 1;
                }
                else
                {
                    var latestPriceBid = db.Bids
                                         .Where(x => x.BidSession == latestSessionBid.BidSession && x.ItemId == itemVM.Id)
                                         .OrderByDescending(x => x.CurrentBid)
                                         .FirstOrDefault();

                    if (latestPriceBid != null)
                    {
                        if (!latestPriceBid.IsWinned)
                        {
                            bidSession = latestPriceBid.BidSession;
                        }
                        else
                        {
                            bidSession = latestPriceBid.BidSession + 1;
                        }
                    }
                }

                var availableBid = new Bid
                {
                    AccountId    = HttpContext.Session.GetInt32("checkiduser").Value,
                    ItemId       = itemVM.Id,
                    CurrentBid   = itemVM.BidPrice,
                    BidSession   = bidSession.Value,
                    BidStartDate = itemVM.BidStartDate.Value,
                    BidEndDate   = itemVM.BidEndDate.Value,
                    CreatedAt    = DateTime.Now
                };

                db.Bids.Add(availableBid);
                await db.SaveChangesAsync();
            }

            return(RedirectToAction("Detail", "Item", new { id = itemVM.Id }));
        }
 private async Task SeedData(AuctionDbContext context)
 {
     context.Cities.Add(GetDummyCity());
     context.AuctionHouses.Add(GetDummyAuctionHouse());
     context.AddRange(GetDummyData());
     await context.SaveChangesAsync();
 }
 private async Task SeedData(AuctionDbContext context)
 {
     context.Users.Add(SeedUser());
     context.AddRange(GetDummyData());
     context.AddRange(GetDummyReceipts());
     await context.SaveChangesAsync();
 }
示例#9
0
        public async Task <IdentityResult> CreateTrader(string email, string password)
        {
            var user = new ApplicationUser()
            {
                UserName = email,
                Email    = email
            };
            var result = await _userManager.CreateAsync(user, password);

            var trader = new Trader()
            {
                IdentityGuid = Guid.Parse(await _userManager.GetUserIdAsync(user))
            };
            await _dbContext.Traders.AddAsync(trader);

            await _dbContext.SaveChangesAsync();

            return(result);
        }
示例#10
0
        public async Task <IActionResult> Create(CreateArtwork artWork)
        {
            if (ModelState.IsValid)
            {
                string type = artWork.Image.ContentType.Split("/")[0];
                if (type != "image")
                {
                    throw new InvalidDataException();
                }
                string currentFileName = artWork.Image.FileName.Trim('"');
                string fileExtension   = Path.GetExtension(currentFileName);
                string newFileName     = Guid.NewGuid().ToString() + fileExtension;
                string semiPath        = $@"images\artworkimages\{newFileName}";
                string filePath        = Path.Combine(hostingEnvironment.WebRootPath, semiPath);
                string dbPath          = $"/images/artworkimages/{newFileName}";
                using (var stream = new FileStream(filePath, FileMode.Create))
                {
                    artWork.Image.CopyTo(stream);
                    stream.Flush();
                }
                var newArtwork = new ArtWork
                {
                    Name       = artWork.Name,
                    CategoryId = artWork.CategoryId,
                    AuthorId   = artWork.AuthorId,
                    Caption    = artWork.Caption,
                    Sold       = false,
                    Image      = dbPath
                };
                _context.Add(newArtwork);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["Author"] = _context.Authors.Select(a => new SelectListItem
            {
                Text  = $"{a.FirstName} {a.LastName}",
                Value = a.AuthorId.ToString()
            });
            ViewData["Category"] = new SelectList(_context.Categories, "CategoryId", "Name");
            return(View(artWork));
        }
示例#11
0
        public async Task <bool> CreateCity(CityCreateInputModel inputModel)
        {
            City city = new City
            {
                Name = inputModel.Name
            };

            context.Cities.Add(city);
            int result = await context.SaveChangesAsync();

            return(result > 0);
        }
        public async Task <IActionResult> Register(AccountVM accountVM)
        {
            var account = db.Accounts.SingleOrDefault(a => a.Username.Equals(accountVM.Username));
            var email   = db.Accounts.SingleOrDefault(a => a.Email.Equals(accountVM.Email));

            if (account != null)
            {
                ViewBag.failed = "Username is already existed ";
                return(View());
            }
            else if (email != null)
            {
                ViewBag.failed = "Email is already existed";
                return(View());
            }
            else
            {
                if (accountVM != null)
                {
                    var acccount     = AccountUtility.MapVMToModel(accountVM);
                    var hashpassword = BCrypt.Net.BCrypt.HashPassword(acccount.Password);
                    acccount.Password  = hashpassword;
                    acccount.Status    = true;
                    acccount.RoleId    = 1;
                    acccount.IsBlocked = false;
                    acccount.CreatedAt = DateTime.Now;
                    db.Accounts.Add(acccount);
                    await db.SaveChangesAsync();

                    ViewBag.success = "Success";
                    return(RedirectToAction(nameof(Login)));
                }

                return(View(accountVM));
            }
        }
示例#13
0
 public async Task SaveAsync()
 {
     await _db.SaveChangesAsync();
 }
示例#14
0
        public async Task Create(TEntity entity)
        {
            await _dbContext.Set <TEntity>().AddAsync(entity);

            await _dbContext.SaveChangesAsync();
        }
示例#15
0
 private async Task SeedData(AuctionDbContext context)
 {
     context.AddRange(GetDummyData());
     await context.SaveChangesAsync();
 }
示例#16
0
 public async Task <int> CommitAsync()
 {
     return(await _context.SaveChangesAsync());
 }