private static async Task Seed(int n = 1000) { for (var i = 0; i < n; i++) { var context = new PlaygroundContext(new DbContextOptionsBuilder <PlaygroundContext>() .UseNpgsql("Host=localhost;Database=playground;Username=postgres;Password=LocalDev123", o => o.UseNodaTime()) .UseSnakeCaseNamingConvention() .EnableSensitiveDataLogging().Options); var recipe = new Recipe(name: Company.Name()); recipe.Description = Company.BS(); recipe.CookTime = Duration.FromMinutes(RandomNumber.Next(1, 60)); recipe.PrepTime = Duration.FromMinutes(RandomNumber.Next(1, 60)); var ingredientCount = RandomNumber.Next(3, 8); for (var j = 0; j < ingredientCount; j++) { var ingredientName = Name.FullName(); var existing = await context.Ingredients.FirstOrDefaultAsync(i => i.Name == ingredientName); var ingredient = existing ?? new Ingredient(ingredientName); var units = Enum.Random <UnitOfMeasure>(); var quantity = RandomNumber.Next(1, 16) / 4.0M; recipe.AddIngredient(ingredient, units, quantity); } Console.WriteLine($"Entering recipe {i} of {n}"); context.Recipes.Add(recipe); await context.SaveChangesAsync(); } }
public async Task <Unit> Handle(RemoveRecipeRequest request, CancellationToken cancellationToken) { var recipe = await _context.GetRecipeForUpdate(request.RecipeKey, cancellationToken); recipe.SoftDelete(); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <ModelUpdateIdentifier> Handle(CreateRecipeCommand request, CancellationToken cancellationToken) { var recipe = _mapper.Map <Recipe>(request); _context.Recipes.Add(recipe); await _context.SaveChangesAsync(cancellationToken); return(new ModelUpdateIdentifier(recipe)); }
public async Task <Unit> Handle(RemoveIngredientRequest request, CancellationToken cancellationToken) { var recipes = await _context.Recipes .Where(x => x.Key == request.RecipeModelKey.Key && x.Version == request.RecipeModelKey.Version) .Include(x => x.RecipeIngredients) .FirstOrDefaultAsync(cancellationToken); if (recipes == null) { throw new RecordNotFoundException(nameof(Recipe), request.RecipeModelKey); } recipes.RemoveIngredient(request.RecipeIngredientModelKey); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <ModelUpdateIdentifier> Handle(AddIngredientRequest request, CancellationToken cancellationToken) { var recipe = await _context.GetRecipeForUpdate(request.RecipeKey); if (recipe == null) { throw new RecordNotFoundException(nameof(Recipe), request.RecipeKey); } var existingIngredient = await _context.Ingredients .Where(x => x.Name == request.Name) .FirstOrDefaultAsync(cancellationToken); var ingredient = existingIngredient ?? _mapper.Map <Ingredient>(request); var recipeIngredient = recipe.AddIngredient(ingredient, request.UnitOfMeasure, request.Quantity); await _context.SaveChangesAsync(cancellationToken); return(new ModelUpdateIdentifier(recipeIngredient.Key, recipeIngredient.Version)); }