Esempio n. 1
0
        public async Task <double> CreateCard(int password, DateTime finishDate)
        {
            using (CarPartDbContext context = _contextFactory.CreateDbContext())
            {
                if (password < 1000 && password > 9999)
                {
                    throw new Exception("Пин должен быть четырёхзначным");
                }
                if (finishDate < DateTime.Now)
                {
                    throw new Exception("Срок окончания не должен превышать текущую дату");
                }

                double balance = new Random().Next(300, 3300);

                await context.Cards.AddAsync(new Card()
                {
                    Attempts     = 3,
                    Balance      = balance,
                    CreationDate = DateTime.Now.Date,
                    FinishDate   = finishDate,
                    PinCode      = HashPass(password.ToString())
                });

                await context.SaveChangesAsync();

                return(balance);
            }
        }
Esempio n. 2
0
 public async Task <IEnumerable <Provider> > GetAll()
 {
     using (CarPartDbContext context = _contextFactory.CreateDbContext())
     {
         //IEnumerable<Provider> providers = await context.Providers.ToListAsync();
         //return providers;
         return(await context.Providers.ToListAsync());
     }
 }
Esempio n. 3
0
        public async Task <IEnumerable <Cart> > GetAll()
        {
            using (CarPartDbContext context = _contextFactory.CreateDbContext())
            {
                //IEnumerable<Cart> entity = await context.Carts.ToListAsync();
                //return entity;

                return(await context.Carts.ToListAsync());
            }
        }
Esempio n. 4
0
        public async Task <IEnumerable <T> > GetAll()
        {
            using (CarPartDbContext context = _contentFactory.CreateDbContext())
            {
                //IEnumerable<T> entities = await context.Set<T>().ToListAsync();
                //return entities;

                return(await context.Set <T>().ToListAsync());
            }
        }
Esempio n. 5
0
        public async Task <T> Create(T entity)
        {
            using (CarPartDbContext context = _contextFactory.CreateDbContext())
            {
                EntityEntry <T> createdResult = await context.Set <T>().AddAsync(entity);

                await context.SaveChangesAsync();

                return(createdResult.Entity);
            }
        }
Esempio n. 6
0
        public async Task <IEnumerable <Account> > GetAll()
        {
            using (CarPartDbContext context = _contextFactory.CreateDbContext())
            {
                IEnumerable <Account> entity = await context.Accounts
                                               .Include(a => a.Orders)
                                               .Include(b => b.Carts)
                                               .ToListAsync();

                return(entity);
            }
        }
Esempio n. 7
0
        public async Task <IEnumerable <Part> > GetAll()
        {
            using (CarPartDbContext context = _contextFactory.CreateDbContext())
            {
                //IEnumerable<Part> parts = await context
                //                                .Parts.Include(a => a.PartProviders)
                //                                .Include(b => b.CarParts)
                //                                .ToListAsync();

                //return parts;

                return(await context.Parts
                       .Include(a => a.PartProviders)
                       .Include(b => b.CarParts)
                       .ToListAsync());
            }
        }
Esempio n. 8
0
        public async Task <PartProvider> AddPartProvider(Part part, Provider provider)
        {
            using (CarPartDbContext context = _contextFactory.CreateDbContext())
            {
                EntityEntry <PartProvider> createdResult = await context.Set <PartProvider>().AddAsync(new PartProvider()
                {
                    Part     = part,
                    Provider = provider
                });

                await context.SaveChangesAsync();

                return(createdResult.Entity);
            }
        }
Esempio n. 9
0
        public async Task <double> CreateOrder(Account account, List <PartFullInfo> partInCar, Address address)
        {
            using (CarPartDbContext context = _contextFactory.CreateDbContext())
            {
                if (partInCar.Count() == 0)
                {
                    throw new Exception("Вы не выбрали деталей для заказа");
                }

                var results     = new List <ValidationResult>();
                var addrContext = new ValidationContext(address);

                StringBuilder errorResult = new StringBuilder();

                if (!Validator.TryValidateObject(address, addrContext, results, true))
                {
                    foreach (var error in results)
                    {
                        errorResult.Append(error.ErrorMessage + '\n');
                    }
                    throw new Exception(errorResult.ToString());
                }

                StringBuilder idParts = new StringBuilder();
                double        price   = 0;
                Order         order   = new Order()
                {
                    Address           = address,
                    OrderCreationTime = DateTime.Now,
                    Status            = OrderStatus.CREATED
                };
                List <OrderParts> orderParts = new List <OrderParts>();

                foreach (var p in partInCar)
                {
                    PartProvider pp = context.PartProviders.FirstOrDefault(x => x.PartId == p.PartId && x.ProviderId == p.ProviderId);//acc check
                    if (pp.TotalParts < p.ProviderPartAmount)
                    {
                        idParts.Append(pp.PartId.ToString() + ' ');
                    }
                }

                if (idParts.Length != 0)
                {
                    throw new Exception($"Данного числа запчастей нет на складе.\nId: {idParts}\nО поступлении мы вам cообщим по e-mail");
                }

                foreach (var p in partInCar)
                {
                    orderParts.Add(new OrderParts()
                    {
                        AmountPart = p.ProviderPartAmount,
                        OrderId    = order.Id,
                        PartId     = p.PartId,
                        Price      = p.ProviderPartPrice,
                        ProviderId = p.ProviderId
                    });

                    price += p.ProviderPartPrice * p.ProviderPartAmount;
                    PartProvider pp = context.PartProviders.FirstOrDefault(x => x.PartId == p.PartId && x.ProviderId == p.ProviderId);//acc check

                    if (pp != null && (pp.TotalParts > p.ProviderPartAmount))
                    {
                        pp.TotalParts -= p.ProviderPartAmount;
                        context.PartProviders.Update(pp);
                    }
                }

                if (price > account.Balance)
                {
                    throw new Exception("Недостаточно денег для заказа");
                }
                order.Parts      = orderParts;
                account.Balance -= price;
                order.Status     = OrderStatus.PAYED;
                account.Orders.Add(order);
                context.Accounts.Update(account);
                await context.SaveChangesAsync();

                await _emailService.SendOrderEmail(order, "Магазин автозапчастей: оплата заказа", $"Ваш заказ с номером {order.Id} был оплачен. Cтоимость: {price}. Ожидайте смены статуса заказа на DELIVERED.");

                return(price);
            }
        }