예제 #1
0
        public async Task <bool> DeleteAll(Expression <Func <T, bool> > query, params Expression <Func <T, object> >[] includes)
        {
            var toRtn = false;

            try
            {
                using (var dbContext = new AuctionsContext())
                {
                    var objs = dbContext.Set <T>().IncludeMultiple(includes).Where(query);

                    foreach (var obj in objs)
                    {
                        dbContext.Set <T>().Remove(obj);
                    }

                    await dbContext.SaveChangesAsync();
                }

                toRtn = true;
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
            }

            return(toRtn);
        }
예제 #2
0
        public async Task <List <T> > SaveAll(List <T> entities)
        {
            try
            {
                using (var dbContext = new AuctionsContext())
                {
                    foreach (var entity in entities)
                    {
                        dbContext.Set <T>().AddOrUpdate(entity);
                    }

                    await dbContext.SaveChangesAsync();
                }
            }
            catch (DbEntityValidationException e)
            {
                var ls = new List <string>();
                foreach (var eve in e.EntityValidationErrors)
                {
                    var errs = eve.ValidationErrors.Select(x => $"Property: {x.PropertyName}, Error:{x.ErrorMessage}");
                    var msg  = $"Entity of type \"{eve.Entry.Entity.GetType().Name}\" in state \"{eve.Entry.State}\" has the following validation errors: {string.Join(Environment.NewLine, errs)}";
                    ls.Add(msg);
                }

                //logger.Error(string.Join(Environment.NewLine, ls));
                //throw;
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
            }

            return(entities);
        }
예제 #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"));
        }
예제 #4
0
        public async Task <IActionResult> DeleteUser(string id)
        {
            var user = await user_manager.FindByIdAsync(id);

            user.is_deleted = true;
            await user_manager.UpdateAsync(user);

            var auctions = await context.auctions
                           .Where(auction => auction.user_id == id).ToListAsync();

            foreach (Auction auction in auctions)
            {
                auction.state = state.SOLD;
            }
            await context.SaveChangesAsync();

            TempData["action"] = string.Format("{0} successfully deleted!", user.first_name + " " + user.last_name);

            return(RedirectToAction(nameof(AdminController.Users), "Admin"));
        }
예제 #5
0
        public async Task <ActionResult <AuctionBundle> > PostAuctionBundle([FromForm] AuctionBundle auctionBundle)
        {
            dynamic r = new System.Dynamic.ExpandoObject();

            try
            {
                auctionBundle.From      = DateTime.Now;
                auctionBundle.CreatedOn = DateTime.Now;
                _context.AuctionBundle.Add(auctionBundle);
                await _context.SaveChangesAsync();

                r.auctionBundleId = auctionBundle.ID.ToString();
            }
            catch (Exception ex)
            {
                r.message = ex.Message;
                //throw;
            }
            //TODO: Return an error object ?
            return(new ObjectResult(r));
        }
예제 #6
0
        public async Task <T> Save(T entity)
        {
            try
            {
                using (var dbContext = new AuctionsContext())
                {
                    dbContext.Set <T>().AddOrUpdate(entity);
                    await dbContext.SaveChangesAsync();
                }

                return(entity);
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                return(null);
            }
        }