private static void ToRow(Dish obj, DishRow row) { row.Id = obj.Id.Value; row.RestaurantId = obj.RestaurantId.Value; row.CategoryId = obj.CategoryId.Value; row.Name = obj.Name; row.Description = obj.Description; row.ProductInfo = obj.ProductInfo; row.Variants = obj.Variants .Select(variant => new DishVariantRow() { DishId = obj.Id.Value, VariantId = variant.VariantId, Name = variant.Name, Price = variant.Price, Extras = variant.Extras .Select(extra => new DishVariantExtraRow() { DishId = obj.Id.Value, VariantId = variant.VariantId, ExtraId = extra.ExtraId, Name = extra.Name, ProductInfo = extra.ProductInfo, Price = extra.Price }).ToList() }).ToList(); }
public Task StoreAsync(Dish dish, CancellationToken cancellationToken = default) { return(Task.Factory.StartNew(() => { var dbSet = dbContext.Dishes; var row = dbSet.FirstOrDefault(x => x.Id == dish.Id.Value); if (row != null) { ToRow(dish, row); dbSet.Update(row); } else { row = new DishRow(); ToRow(dish, row); dbSet.Add(row); } dbContext.SaveChanges(); }, cancellationToken)); }
private static Dish FromRow(DishRow row) { return(new Dish( new DishId(row.Id), new RestaurantId(row.RestaurantId), new DishCategoryId(row.CategoryId), row.Name, row.Description, row.ProductInfo, row.Variants != null ? row.Variants .Select(variantRow => new DishVariant( variantRow.VariantId, variantRow.Name, variantRow.Price, variantRow.Extras != null ? variantRow.Extras .Select(extraRow => new DishVariantExtra( extraRow.ExtraId, extraRow.Name, extraRow.ProductInfo, extraRow.Price) ).ToList() : new List <DishVariantExtra>() )).ToList() : new List <DishVariant>() )); }