public async Task TestUpdatesRecipeWithoutIngredients() { DatabaseFixture fixture = new DatabaseFixture(); var options = fixture.options; var recipesContext = new RecipesContext(options); var emptyIngredientList = new List <Ingredient>(); var existingIngredient = new Recipe { Description = "existing description", Name = "existing title", Ingredients = emptyIngredientList, Id = 9 }; await recipesContext.Recipes.AddAsync(existingIngredient); await recipesContext.SaveChangesAsync(); var controller = new RecipesController(recipesContext); var emptyList = Array.Empty <IngredientDto>(); var newRecipe = new RecipeDto("Updated recipe", "Updated description", emptyList) { Id = 9 }; await controller.PatchRecipe(9, newRecipe); var updatedRecipe = await recipesContext.Recipes.FindAsync((long)9); Assert.Equal("Updated recipe", updatedRecipe.Name); Assert.Equal("Updated description", updatedRecipe.Description); Assert.Empty(updatedRecipe.Ingredients); }
public async Task TestUpdatesRecipeWithoutIngredients() { var options = _fixture.Options; var recipesContext = new RecipesContext(options); var controller = CreateRecipesController(recipesContext); var emptyIngredientList = new List <Ingredient>(); var existingRecipe = new Recipe { Name = "existing title", Description = "existing description", Ingredients = emptyIngredientList, Id = 9 }; await recipesContext.Recipes.AddAsync(existingRecipe); await recipesContext.SaveChangesAsync(); var emptyList = new List <UpdatedIngredientDto>(); var newRecipe = new UpdatedRecipeDto("Updated recipe", "Updated description", emptyList, 9); var result = await controller.PatchRecipe(9, newRecipe); Assert.IsType <ActionResult <Recipe> >(result); Assert.Equal("Updated recipe", result.Value.Name); Assert.Equal("Updated description", result.Value.Description); Assert.Empty(result.Value.Ingredients); }
public async Task <ActionResult <Recipe> > DeleteRecipe(int id) { var recipe = await _context.Recipes.FindAsync(id); if (recipe == null) { return(NotFound()); } _context.Recipes.Remove(recipe); await _context.SaveChangesAsync(); return(recipe); }
private static async Task LoadItemsAsync(RecipesContext context, ZipArchive zip) { var items = zip.Entries.Where(e => e.FullName.StartsWith("assets/minecraft/models/item") && e.FullName.EndsWith(".json")) .Select(entry => { return(new Item { Name = entry.Name.Replace(".json", string.Empty) }); }); context.Items.AddRange(items); var count = await context.SaveChangesAsync(); Console.WriteLine($"Saved {count} items."); }
public async Task TestUpdatesRecipeWithNewIngredients() { DatabaseFixture fixture = new DatabaseFixture(); var options = fixture.options; var recipesContext = new RecipesContext(options); // create existing recipe without ingredients var emptyIngredientList = new List <Ingredient>(); var existingRecipe = new Recipe { Description = "existing description", Name = "existing title", Ingredients = emptyIngredientList, Id = 8 }; await recipesContext.Recipes.AddAsync(existingRecipe); await recipesContext.SaveChangesAsync(); var controller = new RecipesController(recipesContext); var ingredientList = new IngredientDto[2]; var ingredient1 = new IngredientDto("Ingredient1", 100, "gr"); var ingredient2 = new IngredientDto("Ingredient2", 200, "kg"); ingredientList[0] = ingredient1; ingredientList[1] = ingredient2; var newRecipe = new RecipeDto("Updated recipe", "Updated description", ingredientList) { Id = 8 }; await controller.PatchRecipe(8, newRecipe); var updatedRecipe = await recipesContext.Recipes.FindAsync((long)8); Assert.Equal("Updated recipe", updatedRecipe.Name); Assert.Equal("Updated description", updatedRecipe.Description); Assert.Equal(2, updatedRecipe.Ingredients.Count); Assert.Equal("Ingredient1", updatedRecipe.Ingredients[0].Name); Assert.Equal(100, updatedRecipe.Ingredients[0].Amount); Assert.Equal("gr", updatedRecipe.Ingredients[0].Unit); Assert.Equal("Ingredient2", updatedRecipe.Ingredients[1].Name); Assert.Equal(200, updatedRecipe.Ingredients[1].Amount); Assert.Equal("kg", updatedRecipe.Ingredients[1].Unit); }
public async Task <ActionResult <TagView> > PostTag([FromBody] string tag) { try { var newTag = new Tag { Text = tag }; _context.Tags.Add(newTag); await _context.SaveChangesAsync(); return(StatusCode(StatusCodes.Status201Created, new TagView { ID = newTag.ID, Text = newTag.Text })); } catch (Exception e) { return(StatusCode(StatusCodes.Status500InternalServerError, e)); } }
public async Task TestUpdatesRecipeWithNewIngredients() { var options = _fixture.Options; var recipesContext = new RecipesContext(options); var controller = CreateRecipesController(recipesContext); // create existing recipe without ingredients var emptyIngredientList = new List <Ingredient>(); var existingRecipe = new Recipe { Name = "existing title", Description = "existing description", Ingredients = emptyIngredientList, Id = 8 }; await recipesContext.Recipes.AddAsync(existingRecipe); await recipesContext.SaveChangesAsync(); var ingredientList = new List <UpdatedIngredientDto>(); var ingredient1 = new UpdatedIngredientDto("Ingredient1", 100, "gr"); var ingredient2 = new UpdatedIngredientDto("Ingredient2", 200, "kg"); ingredientList.Add(ingredient1); ingredientList.Add(ingredient2); var newRecipe = new UpdatedRecipeDto("Updated recipe", "Updated description", ingredientList, 8); var result = await controller.PatchRecipe(8, newRecipe); Assert.IsType <ActionResult <Recipe> >(result); Assert.Equal("Updated recipe", result.Value.Name); Assert.Equal("Updated description", result.Value.Description); Assert.NotNull(result.Value.Ingredients); Assert.Equal(2, result.Value.Ingredients.Count); Assert.Equal("Ingredient1", result.Value.Ingredients.ToList()[0].Name); Assert.Equal(100, result.Value.Ingredients.ToList()[0].Amount); Assert.Equal("gr", result.Value.Ingredients.ToList()[0].Unit); Assert.Equal("Ingredient2", result.Value.Ingredients.ToList()[1].Name); Assert.Equal(200, result.Value.Ingredients.ToList()[1].Amount); Assert.Equal("kg", result.Value.Ingredients.ToList()[1].Unit); }
public async Task CreateRecipe(Recipe recipe) { await _context.Recipes.AddAsync(recipe); await _context.SaveChangesAsync(); }
public async Task TestUpdatesRecipeWithExistingIngredients() { var options = _fixture.Options; var recipesContext = new RecipesContext(options); var controller = CreateRecipesController(recipesContext); // add existing ingredients to database var ingredientList = new List <Ingredient>(); var existingIngredient1 = new Ingredient("Ingredient1", 100, "gr") { Id = 6 }; var existingIngredient2 = new Ingredient("Ingredient2", 200, "kg") { Id = 7 }; ingredientList.Add(existingIngredient1); ingredientList.Add(existingIngredient2); await recipesContext.Ingredients.AddAsync(existingIngredient1); await recipesContext.Ingredients.AddAsync(existingIngredient2); // have existing recipe with ingredients var existingRecipe = new Recipe { Name = "existing title", Description = "existing description", Ingredients = ingredientList, Id = 12 }; await recipesContext.Recipes.AddAsync(existingRecipe); await recipesContext.SaveChangesAsync(); var updatedIngredientList = new List <UpdatedIngredientDto>(); var ingredient1 = new UpdatedIngredientDto("Ingredient1 updated", 333, "gr") { Id = 6 }; var ingredient2 = new UpdatedIngredientDto("Ingredient2 updated", 555, "kg") { Id = 7 }; updatedIngredientList.Add(ingredient1); updatedIngredientList.Add(ingredient2); var newRecipe = new UpdatedRecipeDto("Updated recipe", "Updated description", updatedIngredientList, 12); var result = await controller.PatchRecipe(12, newRecipe); Assert.IsType <ActionResult <Recipe> >(result); Assert.Equal("Updated recipe", result.Value.Name); Assert.Equal("Updated description", result.Value.Description); Assert.Equal(2, result.Value.Ingredients.Count); Assert.Equal("Ingredient1 updated", result.Value.Ingredients.ToList()[0].Name); Assert.Equal(333, result.Value.Ingredients.ToList()[0].Amount); Assert.Equal("gr", result.Value.Ingredients.ToList()[0].Unit); Assert.Equal("Ingredient2 updated", result.Value.Ingredients.ToList()[1].Name); Assert.Equal(555, result.Value.Ingredients.ToList()[1].Amount); Assert.Equal("kg", result.Value.Ingredients.ToList()[1].Unit); }
private static async Task LoadRecipesAndPatternKeysAsync( ZipArchive zip, JsonSerializer serializer, RecipesContext context) { var objects = zip.Entries .Where(e => e.FullName.StartsWith("data/minecraft/recipes/") && e.FullName.EndsWith(".json")) .Select(entry => { var stream = zip.GetEntry(entry.FullName)?.Open(); using var streamReader = new StreamReader( stream ?? throw new NullReferenceException($"Can't find zipped file {entry.FullName}")); using var jsonReader = new JsonTextReader(streamReader); dynamic obj = serializer.Deserialize(jsonReader); stream.Close(); return(obj, entry.Name); }) .Where(o => o.obj.type == "minecraft:crafting_shaped") .Select(o => { var(obj, name) = o; string resultItemName = null; if (obj.result != null) { resultItemName = obj.result.item.ToString(); } var resultItem = resultItemName != null ? context.Items.FirstOrDefault(i => i.Name == resultItemName.Replace("minecraft:", "")) : null; var recipe = new Recipe { Group = obj.group, Name = name.Replace(".json", ""), Type = obj.type, Pattern = string.Join('\n', obj.pattern), Result = resultItem, ResultCount = obj.result?.count ?? 0 }; var keys = new List <PatternKey>(); var chars = new HashSet <char>(); foreach (var line in obj.pattern) { var s = (string)line?.ToString(); if (s == null || !s.Any()) { continue; } foreach (var c in s) { chars.Add(c); } } foreach (var c in chars) { if (c == '\n') { continue; } var objectKey = c.ToString(); try { if (obj.key == null || obj.key[objectKey] == null) { continue; } var type = (Type)obj.key[objectKey].GetType(); if (type == typeof(JArray) || obj.key[objectKey].item == null) { continue; } var jobject = obj.key[objectKey].item; var itemName = ((string)jobject).Replace("minecraft:", ""); var item = context.Items.FirstOrDefault(i => i.Name == itemName); if (item != null) { keys.Add(new PatternKey { Character = c.ToString(), Item = item, Recipe = recipe }); } } catch (Exception e) { Console.WriteLine(e); Console.WriteLine( $"Values: \nrecipe name:{name}\nkey: {c}\nvalue: {obj.key[objectKey].item}."); throw; } } return(recipe, keys); }); var keys = new List <PatternKey>(); var recipes = new List <Recipe>(); foreach (var o in objects) { recipes.Add(o.recipe); keys.AddRange(o.keys); } await context.Recipes.AddRangeAsync(recipes); await context.PatternKeys.AddRangeAsync(keys); var count = await context.SaveChangesAsync(); Console.WriteLine($"Saved {count} recipes and pattern keys."); }
public async Task TestUpdatesRecipeWithExistingIngredients() { DatabaseFixture fixture = new DatabaseFixture(); var options = fixture.options; var recipesContext = new RecipesContext(options); // add existing ingredients to database var ingredientList = new List <Ingredient>(); var existingIngredient1 = new Ingredient { Name = "Ingredient1", Amount = 100, Unit = "gr", Id = 6 }; var existingIngredient2 = new Ingredient { Name = "Ingredient2", Amount = 200, Unit = "kg", Id = 7 }; ingredientList.Add(existingIngredient1); ingredientList.Add(existingIngredient2); await recipesContext.Ingredients.AddAsync(existingIngredient1); await recipesContext.Ingredients.AddAsync(existingIngredient2); // have existing recipe with ingredients var existingRecipe = new Recipe { Description = "existing description", Name = "existing title", Ingredients = ingredientList, Id = 4 }; await recipesContext.Recipes.AddAsync(existingRecipe); await recipesContext.SaveChangesAsync(); var controller = new RecipesController(recipesContext); var updatedIngredientList = new IngredientDto[2]; var ingredient1 = new IngredientDto("Ingredient1 updated", 333, "gr") { Id = 6 }; var ingredient2 = new IngredientDto("Ingredient2 updated", 555, "kg") { Id = 7 }; updatedIngredientList[0] = ingredient1; updatedIngredientList[1] = ingredient2; var newRecipe = new RecipeDto("Updated recipe", "Updated description", updatedIngredientList) { Id = 4 }; await controller.PatchRecipe(4, newRecipe); var updatedRecipe = await recipesContext.Recipes.FindAsync((long)4); Assert.Equal("Updated recipe", updatedRecipe.Name); Assert.Equal("Updated description", updatedRecipe.Description); Assert.Equal(2, updatedRecipe.Ingredients.Count); Assert.Equal("Ingredient1 updated", updatedRecipe.Ingredients[0].Name); Assert.Equal(333, updatedRecipe.Ingredients[0].Amount); Assert.Equal("gr", updatedRecipe.Ingredients[0].Unit); Assert.Equal("Ingredient2 updated", updatedRecipe.Ingredients[1].Name); Assert.Equal(555, updatedRecipe.Ingredients[1].Amount); Assert.Equal("kg", updatedRecipe.Ingredients[1].Unit); }
public async Task <bool> SaveChangesAsync() { // return true if 1 or more entities were changed return(await _recipiesContext.SaveChangesAsync() > 0); }
public async Task <ActionResult <Recipe> > PutRecipe(long id, RecipeDto updatedRecipe) { if (id != updatedRecipe.Id) { return(BadRequest()); } var currentRecipe = await _context.Recipes.FindAsync(id); if (currentRecipe == null) { return(NotFound()); } currentRecipe.Name = updatedRecipe.Name; currentRecipe.Description = updatedRecipe.Description; var ingredients = updatedRecipe.Ingredients; var updatedIngredients = new List <Ingredient>(); foreach (var ingredient in ingredients) { if (ingredient.Id == null) { var newIngredient = new Ingredient { Name = ingredient.Name, Amount = ingredient.Amount, Unit = ingredient.Unit }; await _context.Ingredients.AddAsync(newIngredient); updatedIngredients.Add(newIngredient); } else { var updatedIngredient = await _context.Ingredients.FindAsync(ingredient.Id); if (updatedIngredient == null) { continue; } updatedIngredient.Amount = ingredient.Amount; updatedIngredient.Name = ingredient.Name; updatedIngredient.Unit = ingredient.Unit; updatedIngredients.Add(updatedIngredient); _context.Ingredients.Update(updatedIngredient); } currentRecipe.Ingredients = updatedIngredients; } await _context.SaveChangesAsync(); { } _context.Entry(currentRecipe).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!RecipeExists(id)) { return(NotFound()); } throw; } return(currentRecipe); }