Beispiel #1
0
        internal async Task <ProductViewModel> GetProductById(int productId)
        {
            using (var db = new SupplierbaseContext()) {
                var model = await db.Products.FirstOrDefaultAsync(p => p.Id == productId);

                if (model != null)
                {
                    var result = new ProductViewModel {
                        Id     = model.Id,
                        Name   = model.Name,
                        Limits = db.Limits.Where(l => l.ProductId == productId)
                                 .Select(l => new LimitModel {
                            LimitValue    = l.LimitValue,
                            RangeCapacity = l.RangeCapacity
                        }).ToList()
                    };

                    return(result);
                }
                else
                {
                    throw new Exception($"Product with ID:{productId} not exist");
                }
            }
        }
Beispiel #2
0
        internal async Task <List <OrderViewModel> > CalculateOrder(OrderModel model)
        {
            using (var db = new SupplierbaseContext()) {
                var result = new List <OrderViewModel>();

                var limits = await(from sd in db.ShopDistributions
                                   join l in db.Limits on sd.LimitId equals l.Id
                                   join s in db.Shops on sd.ShopId equals s.Id
                                   where sd.ProductId == model.ProductId && sd.Percentage > 0
                                   select new DistributionModel {
                    LimitValue             = l.LimitValue,
                    RangeCapacity          = l.RangeCapacity,
                    PercentageDistribution = sd.Percentage,
                    ShopId   = sd.ShopId,
                    ShopName = s.Name
                }).GroupBy(l => new { l.ShopId, l.ShopName })
                             .ToListAsync();

                foreach (var item in limits)
                {
                    var shop = new OrderViewModel {
                        ShopId   = item.Key.ShopId,
                        ShopName = item.Key.ShopName,
                        Quantity = item.ToList().CalculateQuantity(model.Quantity.Value)
                    };
                    result.Add(shop);
                }

                return(result);
            }
        }
Beispiel #3
0
        public async Task SaveProduct(ProductPostModel model)
        {
            using (var db = new SupplierbaseContext()) {
                try {
                    var entity = await db.Products.FirstOrDefaultAsync(p => p.Id == model.Id);

                    if (entity == null)
                    {
                        entity = new Product {
                            Name = model.Name,
                        };
                        await db.Products.AddAsync(entity);
                    }
                    else
                    {
                        entity.Name = model.Name;
                        db.Products.Update(entity);
                    }

                    await db.SaveChangesAsync();

                    model.Id = entity.Id;

                    await AddProductLimits(model);
                } catch (Exception ex) {
                    throw ex;
                }
            }
        }
Beispiel #4
0
        internal async Task SaveShop(ShopPostModel model)
        {
            using (var db = new SupplierbaseContext()) {
                var entity = await db.Shops.FirstOrDefaultAsync(p => p.Id == model.Id);

                if (entity == null)
                {
                    entity = new Shop {
                        Name = model.Name,
                    };
                    await db.Shops.AddAsync(entity);
                }
                else
                {
                    entity.Name = model.Name;
                    db.Shops.Update(entity);
                }

                await db.SaveChangesAsync();

                model.Id = entity.Id;

                await AddShopProducts(model);
            }
        }
Beispiel #5
0
        private async Task <List <DistributionModel> > GetProductDistribution(int shopId, int productId)
        {
            using (var db = new SupplierbaseContext()) {
                var result = await(from sd in db.ShopDistributions
                                   join l in db.Limits on sd.LimitId equals l.Id
                                   where sd.ProductId == productId && sd.ShopId == shopId
                                   select new DistributionModel {
                    LimitId                = l.Id,
                    LimitValue             = l.LimitValue,
                    PercentageDistribution = sd.Percentage
                }).ToListAsync();

                if (!result.Any())
                {
                    result = await(from l in db.Limits
                                   where l.ProductId == productId
                                   select new DistributionModel {
                        LimitId                = l.Id,
                        LimitValue             = l.LimitValue,
                        PercentageDistribution = 0
                    }).ToListAsync();
                }

                return(result);
            }
        }
Beispiel #6
0
        internal async Task <List <DropdownModel> > GetDropdownProducts()
        {
            using (var db = new SupplierbaseContext()) {
                var result = await db.Products.Select(p => new DropdownModel {
                    Id   = p.Id,
                    Name = p.Name
                }).ToListAsync();

                return(result);
            }
        }
Beispiel #7
0
        internal async Task <List <ProductViewModel> > GetProducts()
        {
            using (var db = new SupplierbaseContext()) {
                var result = await(from p in db.Products
                                   join l in db.Limits on p.Id equals l.ProductId into limits
                                   select new ProductViewModel {
                    Id     = p.Id,
                    Name   = p.Name,
                    Limits = limits.Select(l => new LimitModel {
                        LimitValue    = l.LimitValue,
                        RangeCapacity = l.RangeCapacity
                    }).ToList()
                }).ToListAsync();

                return(result);
            }
        }
Beispiel #8
0
        internal async Task DeleteShop(int shopId)
        {
            using (var db = new SupplierbaseContext()) {
                var shopProducts = await db.ShopProducts.Where(s => s.ShopId == shopId).ToListAsync();

                db.RemoveRange(shopProducts);
                var shopDistributions = await db.ShopDistributions.Where(s => s.ShopId == shopId).ToListAsync();

                db.RemoveRange(shopDistributions);

                var shops = await db.Shops.Where(s => s.Id == shopId).ToListAsync();

                db.RemoveRange(shops);

                await db.SaveChangesAsync();
            }
        }
Beispiel #9
0
        public async Task <List <ShopViewModel> > GetShops()
        {
            using (var db = new SupplierbaseContext()) {
                var shops = await(from s in db.Shops
                                  let products = (from p in db.Products
                                                  join sp in db.ShopProducts on p.Id equals sp.ProductId
                                                  where sp.ShopId == s.Id
                                                  select p.Name).ToList()
                                                 select new ShopViewModel {
                    Id       = s.Id,
                    Name     = s.Name,
                    Products = string.Join(", ", products)
                }).ToListAsync();

                return(shops);
            }
        }
Beispiel #10
0
        private async Task AddShopProducts(ShopPostModel model)
        {
            using (var db = new SupplierbaseContext()) {
                var shopProducts = await db.ShopProducts.Where(s => s.ShopId == model.Id).ToListAsync();

                db.RemoveRange(shopProducts);

                var models = model.Products.Select(productId => new ShopProduct {
                    ShopId    = model.Id,
                    ProductId = productId,
                }).ToList();

                await db.ShopProducts.AddRangeAsync(models);

                await db.SaveChangesAsync();
            }
        }
Beispiel #11
0
        internal async Task SaveDistribution(DistributionPostModel model)
        {
            using (var db = new SupplierbaseContext()) {
                var shopDistribution = await db.ShopDistributions.Where(s => s.ProductId == model.ProductId).ToListAsync();

                db.RemoveRange(shopDistribution);

                var result = model.Shops.SelectMany(s => s.ProductDistribution.Select(d => new ShopDistribution {
                    ProductId  = model.ProductId,
                    ShopId     = s.ShopId,
                    LimitId    = d.LimitId,
                    Percentage = d.PercentageDistribution
                })).ToList();

                await db.ShopDistributions.AddRangeAsync(result);

                await db.SaveChangesAsync();
            }
        }
Beispiel #12
0
        private async Task AddProductLimits(ProductPostModel model)
        {
            using (var db = new SupplierbaseContext()) {
                var productLimits = await db.Limits.Where(c => c.ProductId == model.Id).ToListAsync();

                db.RemoveRange(productLimits);
                var limits = model.Limits.CalculateRangeCapacity();

                foreach (var limit in limits)
                {
                    var entity = new Limit {
                        ProductId     = model.Id.Value,
                        LimitValue    = limit.LimitValue,
                        RangeCapacity = limit.RangeCapacity,
                    };
                    db.Limits.Add(entity);
                }
                await db.SaveChangesAsync();
            }
        }
Beispiel #13
0
        internal async Task <List <ShopDistributionModel> > GetShopsByProductId(int productId)
        {
            var result = new List <ShopDistributionModel>();

            using (var db = new SupplierbaseContext()) {
                var shops = await(from s in db.Shops
                                  join sp in db.ShopProducts on s.Id equals sp.ShopId
                                  where sp.ProductId == productId
                                  select s).ToListAsync();

                foreach (var shop in shops)
                {
                    var model = new ShopDistributionModel {
                        ShopId              = shop.Id,
                        ShopName            = shop.Name,
                        ProductDistribution = await GetProductDistribution(shop.Id, productId)
                    };
                    result.Add(model);
                }

                return(result);
            }
        }