public async Task <IActionResult> SaveDistribution([FromBody] DistributionPostModel model) { var validator = new DistributionValidator(); var result = await validator.ValidateAsync(model); if (!result.IsValid) { var error = string.Join(';', result.Errors); throw new Exception(error); } await _service.SaveDistribution(model); return(Ok()); }
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(); } }