From 5f51a71731c2e5249edab0c69bb515d428d553d9 Mon Sep 17 00:00:00 2001 From: Yannis Date: Fri, 16 Apr 2021 21:41:34 +0300 Subject: [PATCH 1/7] Started creating the update menu --- CRUDRecipeEF.API/CRUDRecipeEF.API.csproj | 1 + CRUDRecipeEF.BL/Services/IRecipeService.cs | 1 - .../Services/IUpdateRecipeMenuService.cs | 14 +++ CRUDRecipeEF.BL/Services/RecipeService.cs | 7 -- .../Services/UpdateRecipeMenuService.cs | 37 +++++++ CRUDRecipeEF.PL/Menus/IUpdateMenu.cs | 13 +++ CRUDRecipeEF.PL/Menus/IngredientMenu.cs | 10 +- CRUDRecipeEF.PL/Menus/RecipeMenu.cs | 22 ++-- CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs | 99 ++++++++++++++++++ CRUDRecipeEF.PL/Recipe.db-shm | Bin 32768 -> 0 bytes CRUDRecipeEF.PL/Recipe.db-wal | 0 11 files changed, 180 insertions(+), 24 deletions(-) create mode 100644 CRUDRecipeEF.BL/Services/IUpdateRecipeMenuService.cs create mode 100644 CRUDRecipeEF.BL/Services/UpdateRecipeMenuService.cs create mode 100644 CRUDRecipeEF.PL/Menus/IUpdateMenu.cs create mode 100644 CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs delete mode 100644 CRUDRecipeEF.PL/Recipe.db-shm delete mode 100644 CRUDRecipeEF.PL/Recipe.db-wal diff --git a/CRUDRecipeEF.API/CRUDRecipeEF.API.csproj b/CRUDRecipeEF.API/CRUDRecipeEF.API.csproj index 8371e89..7ba47e8 100644 --- a/CRUDRecipeEF.API/CRUDRecipeEF.API.csproj +++ b/CRUDRecipeEF.API/CRUDRecipeEF.API.csproj @@ -10,6 +10,7 @@ + diff --git a/CRUDRecipeEF.BL/Services/IRecipeService.cs b/CRUDRecipeEF.BL/Services/IRecipeService.cs index b1e9746..c8adc2f 100644 --- a/CRUDRecipeEF.BL/Services/IRecipeService.cs +++ b/CRUDRecipeEF.BL/Services/IRecipeService.cs @@ -20,6 +20,5 @@ public interface IRecipeService Task RemoveIngredientFromRecipe(string ingredientName, string recipeName); Task DeleteRecipe(string name); - Task UpdateRecipe(RecipeDTO recipeDTO, string recipeName); } } \ No newline at end of file diff --git a/CRUDRecipeEF.BL/Services/IUpdateRecipeMenuService.cs b/CRUDRecipeEF.BL/Services/IUpdateRecipeMenuService.cs new file mode 100644 index 0000000..6d37f62 --- /dev/null +++ b/CRUDRecipeEF.BL/Services/IUpdateRecipeMenuService.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CRUDRecipeEF.BL.Services +{ + public interface IUpdateRecipeMenuService + { + Task UpdateRecipeName(); + Task UpdateRecipeIngredient(); + } +} diff --git a/CRUDRecipeEF.BL/Services/RecipeService.cs b/CRUDRecipeEF.BL/Services/RecipeService.cs index 43b2d8a..60b8812 100644 --- a/CRUDRecipeEF.BL/Services/RecipeService.cs +++ b/CRUDRecipeEF.BL/Services/RecipeService.cs @@ -75,13 +75,6 @@ public async Task AddRecipe(RecipeDTO recipeAddDTO) return recipeAddDTO.Name; } - public async Task UpdateRecipe (RecipeDTO recipeDTO, string recipeName) - { - var recipe = await GetRecipeByNameIfExists(recipeDTO.Name); - recipe.Name = recipeName; - - await Save(); - } /// /// diff --git a/CRUDRecipeEF.BL/Services/UpdateRecipeMenuService.cs b/CRUDRecipeEF.BL/Services/UpdateRecipeMenuService.cs new file mode 100644 index 0000000..dbcd3e8 --- /dev/null +++ b/CRUDRecipeEF.BL/Services/UpdateRecipeMenuService.cs @@ -0,0 +1,37 @@ +using AutoMapper; +using CRUDRecipeEF.DAL.Data; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CRUDRecipeEF.BL.Services +{ + public class UpdateRecipeMenuService : IUpdateRecipeMenuService + { + private readonly RecipeContext _context; + private readonly ILogger _logger; + private readonly IMapper _mapper; + + public UpdateRecipeMenuService(RecipeContext context, + IMapper mapper, + ILogger logger) + { + _context = context; + _mapper = mapper; + _logger = logger; + } + + public Task UpdateRecipeIngredient() + { + throw new NotImplementedException(); + } + + public Task UpdateRecipeName() + { + throw new NotImplementedException(); + } + } +} diff --git a/CRUDRecipeEF.PL/Menus/IUpdateMenu.cs b/CRUDRecipeEF.PL/Menus/IUpdateMenu.cs new file mode 100644 index 0000000..d7c99c6 --- /dev/null +++ b/CRUDRecipeEF.PL/Menus/IUpdateMenu.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CRUDRecipeEF.PL.Menus +{ + public interface IUpdateMenu + { + Task Show(); + } +} diff --git a/CRUDRecipeEF.PL/Menus/IngredientMenu.cs b/CRUDRecipeEF.PL/Menus/IngredientMenu.cs index 085ddf4..b289661 100644 --- a/CRUDRecipeEF.PL/Menus/IngredientMenu.cs +++ b/CRUDRecipeEF.PL/Menus/IngredientMenu.cs @@ -14,7 +14,7 @@ public class IngredientMenu : IIngredientMenu private readonly ILogger _logger; private readonly int _ingredientsPerPage = 8; - private enum IngredientMenuOption { InValid = 0, NewIngredient = 1, LookUpIngredient = 2, ShowIngredient = 3, DeleteIngredient = 4, GoBack = 5 }; + private enum IngredientMenuOption { InValid = 0, NewIngredient = 1, LookUpIngredient = 2, ShowIngredient = 3, DeleteIngredient = 4, UpdateIngredient = 5, GoBack = 6 }; public IngredientMenu(IIngredientService ingredientService, ILogger logger) @@ -32,8 +32,9 @@ public async Task Show() ConsoleHelper.ColorWriteLine("2.) Lookup Ingredient"); ConsoleHelper.ColorWriteLine("3.) Show Ingredient List"); ConsoleHelper.ColorWriteLine("4.) Delete Ingredient"); + ConsoleHelper.ColorWriteLine("5.) Update Ingredient"); Console.WriteLine(); - ConsoleHelper.ColorWriteLine(ConsoleColor.Red, "5.) Back to Main Menu"); + ConsoleHelper.ColorWriteLine(ConsoleColor.Red, "6.) Back to Main Menu"); Console.WriteLine(); string input = string.Empty; @@ -78,6 +79,9 @@ private async Task ExecuteMenuSelection(IngredientMenuOption option) case IngredientMenuOption.DeleteIngredient: await DeleteIngredient(); break; + case IngredientMenuOption.UpdateIngredient: + //await UpdateIngredient(); + break; case IngredientMenuOption.GoBack: Console.WriteLine(); break; @@ -166,5 +170,7 @@ private async Task ListIngredients() Console.WriteLine(); await this.Show(); } + + } } diff --git a/CRUDRecipeEF.PL/Menus/RecipeMenu.cs b/CRUDRecipeEF.PL/Menus/RecipeMenu.cs index f796a36..976a812 100644 --- a/CRUDRecipeEF.PL/Menus/RecipeMenu.cs +++ b/CRUDRecipeEF.PL/Menus/RecipeMenu.cs @@ -12,6 +12,7 @@ public class RecipeMenu : IRecipeMenu { private readonly IRecipeService _recipeService; private readonly IIngredientService _ingredientService; + private readonly IUpdateMenu _updateMenu; private readonly ILogger _logger; private readonly int _recipePerPage = 8; @@ -21,12 +22,15 @@ private enum RecipeMenuOption UpdateRecipe = 5, GoBack = 6 }; + public RecipeMenu(IRecipeService recipeService, - IIngredientService ingredientService, + IIngredientService ingredientService, + IUpdateMenu updateMenu, ILogger logger) { _recipeService = recipeService; _ingredientService = ingredientService; + _updateMenu = updateMenu; _logger = logger; } @@ -67,6 +71,7 @@ public async Task Show() await ExecuteMenuSelection(choice); } + private async Task ExecuteMenuSelection(RecipeMenuOption option) { switch (option) @@ -92,7 +97,7 @@ private async Task ExecuteMenuSelection(RecipeMenuOption option) break; case RecipeMenuOption.UpdateRecipe: Console.WriteLine(); - UpdateRecipe(); + await _updateMenu.Show(); break; case RecipeMenuOption.GoBack: Console.WriteLine(); @@ -102,19 +107,8 @@ private async Task ExecuteMenuSelection(RecipeMenuOption option) } } - private async void UpdateRecipe() - { - ConsoleHelper.ColorWrite("What recipe would you like to update: "); - var input = Console.ReadLine(); - - var recipe = await _recipeService.GetRecipeByName(input); + - ConsoleHelper.ColorWrite("What is the new name of the recipe: "); - Console.WriteLine(); - var recipeName = Console.ReadLine(); - - await _recipeService.UpdateRecipe(recipe, recipeName); - } private async Task ListRecipe() { diff --git a/CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs b/CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs new file mode 100644 index 0000000..bf5edd8 --- /dev/null +++ b/CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs @@ -0,0 +1,99 @@ +using CRUDRecipeEF.BL.Services; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CRUDRecipeEF.PL.Menus +{ + public class UpdateRecipeMenu : IUpdateMenu + { + private readonly IRecipeService _recipeService; + private readonly IIngredientService _ingredientService; + private readonly IUpdateRecipeMenuService _updateMenuService; + private readonly ILogger _logger; + + private enum RecipeUpdateOption + { + InValid = 0, Name = 1, Ingredient = 2, GoBack = 3 + }; + + public UpdateRecipeMenu(IRecipeService recipeService, + IIngredientService ingredientService, + ILogger logger) + { + _recipeService = recipeService; + _ingredientService = ingredientService; + _logger = logger; + } + + public async Task Show() + { + ConsoleHelper.DefaultColor = ConsoleColor.Blue; + ConsoleHelper.ColorWriteLine(ConsoleColor.Yellow, "Recipe Change"); + Console.WriteLine(); + ConsoleHelper.ColorWriteLine("1.) Change Name"); + ConsoleHelper.ColorWriteLine("2.) Change Ingredient"); + ConsoleHelper.ColorWriteLine(ConsoleColor.Red, "6.) Back to Main Menu"); + Console.WriteLine(); + + string input = string.Empty; + int option = 0; + bool valid = false; + + while (!valid) + { + ConsoleHelper.ColorWrite(ConsoleColor.Yellow, "Please select an option: "); + input = Console.ReadLine(); + + valid = ConsoleHelper.ValidateInt(input, (int)RecipeUpdateOption.Name, (int)RecipeUpdateOption.GoBack, out option); + + if (!Enum.IsDefined(typeof(RecipeUpdateOption), option)) + { + _logger.LogWarning("Option is not in enum"); + valid = false; + } + + } + + RecipeUpdateOption choice = (RecipeUpdateOption)option; + await ExecuteUpdateRecipeSelection(choice); + } + + private async Task ExecuteUpdateRecipeSelection(RecipeUpdateOption recipeUpdateOption) + { + switch (recipeUpdateOption) + { + case RecipeUpdateOption.InValid: + _logger.LogWarning("Attempted to execute invalid menu selection"); + break; + case RecipeUpdateOption.Name: + Console.WriteLine(); + await UpdateRecipeName(); + break; + case RecipeUpdateOption.Ingredient: + Console.WriteLine(); + await UpdateRecipeIngredient(); + break; + case RecipeUpdateOption.GoBack: + Console.WriteLine(); + break; + default: + break; + } + } + + private async Task UpdateRecipeIngredient() + { + _updateMenuService.UpdateRecipeIngredient(); + } + + private async Task UpdateRecipeName() + { + _updateMenuService.UpdateRecipeName(); + } + + } +} diff --git a/CRUDRecipeEF.PL/Recipe.db-shm b/CRUDRecipeEF.PL/Recipe.db-shm deleted file mode 100644 index fe9ac2845eca6fe6da8a63cd096d9cf9e24ece10..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 32768 zcmeIuAr62r3 Date: Fri, 16 Apr 2021 22:19:15 +0300 Subject: [PATCH 2/7] Inserted in the Program.cs the services --- .../Menus/{IUpdateMenu.cs => IUpdateRecipeMenu.cs} | 2 +- CRUDRecipeEF.PL/Menus/RecipeMenu.cs | 4 ++-- CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs | 3 ++- CRUDRecipeEF.PL/Program.cs | 2 ++ 4 files changed, 7 insertions(+), 4 deletions(-) rename CRUDRecipeEF.PL/Menus/{IUpdateMenu.cs => IUpdateRecipeMenu.cs} (82%) diff --git a/CRUDRecipeEF.PL/Menus/IUpdateMenu.cs b/CRUDRecipeEF.PL/Menus/IUpdateRecipeMenu.cs similarity index 82% rename from CRUDRecipeEF.PL/Menus/IUpdateMenu.cs rename to CRUDRecipeEF.PL/Menus/IUpdateRecipeMenu.cs index d7c99c6..c6be208 100644 --- a/CRUDRecipeEF.PL/Menus/IUpdateMenu.cs +++ b/CRUDRecipeEF.PL/Menus/IUpdateRecipeMenu.cs @@ -6,7 +6,7 @@ namespace CRUDRecipeEF.PL.Menus { - public interface IUpdateMenu + public interface IUpdateRecipeMenu { Task Show(); } diff --git a/CRUDRecipeEF.PL/Menus/RecipeMenu.cs b/CRUDRecipeEF.PL/Menus/RecipeMenu.cs index 976a812..91c7812 100644 --- a/CRUDRecipeEF.PL/Menus/RecipeMenu.cs +++ b/CRUDRecipeEF.PL/Menus/RecipeMenu.cs @@ -12,7 +12,7 @@ public class RecipeMenu : IRecipeMenu { private readonly IRecipeService _recipeService; private readonly IIngredientService _ingredientService; - private readonly IUpdateMenu _updateMenu; + private readonly IUpdateRecipeMenu _updateMenu; private readonly ILogger _logger; private readonly int _recipePerPage = 8; @@ -25,7 +25,7 @@ private enum RecipeMenuOption public RecipeMenu(IRecipeService recipeService, IIngredientService ingredientService, - IUpdateMenu updateMenu, + IUpdateRecipeMenu updateMenu, ILogger logger) { _recipeService = recipeService; diff --git a/CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs b/CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs index bf5edd8..344f0c6 100644 --- a/CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs +++ b/CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs @@ -8,7 +8,7 @@ namespace CRUDRecipeEF.PL.Menus { - public class UpdateRecipeMenu : IUpdateMenu + public class UpdateRecipeMenu : IUpdateRecipeMenu { private readonly IRecipeService _recipeService; private readonly IIngredientService _ingredientService; @@ -36,6 +36,7 @@ public async Task Show() Console.WriteLine(); ConsoleHelper.ColorWriteLine("1.) Change Name"); ConsoleHelper.ColorWriteLine("2.) Change Ingredient"); + Console.WriteLine(); ConsoleHelper.ColorWriteLine(ConsoleColor.Red, "6.) Back to Main Menu"); Console.WriteLine(); diff --git a/CRUDRecipeEF.PL/Program.cs b/CRUDRecipeEF.PL/Program.cs index a48cb6e..e73d5fa 100644 --- a/CRUDRecipeEF.PL/Program.cs +++ b/CRUDRecipeEF.PL/Program.cs @@ -72,6 +72,8 @@ public static IHostBuilder CreateHostBuilder(string[] args) .AddTransient() .AddTransient() .AddTransient() + .AddTransient() + .AddTransient() .ConfigureDal(); services.AddDbContext(options => From f868246b1fb42296c257c400aa4181633bb1b7fd Mon Sep 17 00:00:00 2001 From: Yannis Date: Sat, 17 Apr 2021 21:28:03 +0300 Subject: [PATCH 3/7] Completed the UpdateRecipeMenu --- CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs | 63 +++++++++++++++-------- 1 file changed, 42 insertions(+), 21 deletions(-) diff --git a/CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs b/CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs index 344f0c6..6f3fa79 100644 --- a/CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs +++ b/CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs @@ -1,4 +1,6 @@ using CRUDRecipeEF.BL.Services; +using CRUDRecipeEF.DAL.Data; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -13,6 +15,7 @@ public class UpdateRecipeMenu : IUpdateRecipeMenu private readonly IRecipeService _recipeService; private readonly IIngredientService _ingredientService; private readonly IUpdateRecipeMenuService _updateMenuService; + private readonly RecipeContext _context; private readonly ILogger _logger; private enum RecipeUpdateOption @@ -22,45 +25,63 @@ private enum RecipeUpdateOption public UpdateRecipeMenu(IRecipeService recipeService, IIngredientService ingredientService, + RecipeContext context, ILogger logger) { _recipeService = recipeService; _ingredientService = ingredientService; _logger = logger; + _context = context; } public async Task Show() { - ConsoleHelper.DefaultColor = ConsoleColor.Blue; - ConsoleHelper.ColorWriteLine(ConsoleColor.Yellow, "Recipe Change"); - Console.WriteLine(); - ConsoleHelper.ColorWriteLine("1.) Change Name"); - ConsoleHelper.ColorWriteLine("2.) Change Ingredient"); - Console.WriteLine(); - ConsoleHelper.ColorWriteLine(ConsoleColor.Red, "6.) Back to Main Menu"); - Console.WriteLine(); + ConsoleHelper.ColorWriteLine("Please provide the name of the recipe that you want to update: "); + var recipe = Console.ReadLine(); - string input = string.Empty; - int option = 0; - bool valid = false; + var findRecipe = await _context.Recipes.Include(i => i.Ingredients) + .SingleOrDefaultAsync(r => r.Name.ToLower() == recipe.ToLower().Trim()); - while (!valid) + if (findRecipe != null) { - ConsoleHelper.ColorWrite(ConsoleColor.Yellow, "Please select an option: "); - input = Console.ReadLine(); + Console.WriteLine(); + ConsoleHelper.DefaultColor = ConsoleColor.Blue; + ConsoleHelper.ColorWriteLine(ConsoleColor.Yellow, "Recipe Change Menu"); + Console.WriteLine(); + ConsoleHelper.ColorWriteLine("1.) Change Name"); + ConsoleHelper.ColorWriteLine("2.) Change Ingredient"); + Console.WriteLine(); + ConsoleHelper.ColorWriteLine(ConsoleColor.Red, "3.) Back to Main Menu"); + Console.WriteLine(); - valid = ConsoleHelper.ValidateInt(input, (int)RecipeUpdateOption.Name, (int)RecipeUpdateOption.GoBack, out option); + string input = string.Empty; + int option = 0; + bool valid = false; - if (!Enum.IsDefined(typeof(RecipeUpdateOption), option)) + while (!valid) { - _logger.LogWarning("Option is not in enum"); - valid = false; + ConsoleHelper.ColorWrite(ConsoleColor.Yellow, "Please select an option: "); + input = Console.ReadLine(); + + valid = ConsoleHelper.ValidateInt(input, (int)RecipeUpdateOption.Name, (int)RecipeUpdateOption.GoBack, out option); + + if (!Enum.IsDefined(typeof(RecipeUpdateOption), option)) + { + _logger.LogWarning("Option is not in enum"); + valid = false; + } + } + RecipeUpdateOption choice = (RecipeUpdateOption)option; + await ExecuteUpdateRecipeSelection(choice); + } + else + { + Console.WriteLine(); + ConsoleHelper.ColorWriteLine(ConsoleColor.DarkYellow, $"{recipe} does not exist."); + Console.WriteLine(); } - - RecipeUpdateOption choice = (RecipeUpdateOption)option; - await ExecuteUpdateRecipeSelection(choice); } private async Task ExecuteUpdateRecipeSelection(RecipeUpdateOption recipeUpdateOption) From 75444ddd3fd00aceaa8e79c219c800aa16f26243 Mon Sep 17 00:00:00 2001 From: Yannis Date: Sun, 18 Apr 2021 21:40:12 +0300 Subject: [PATCH 4/7] -Completed IngredientService and UpdateRecipeManuService --- .../Services/IIngredientService.cs | 1 - .../Services/IUpdateRecipeMenuService.cs | 9 ++++-- CRUDRecipeEF.BL/Services/IngredientService.cs | 8 +---- .../Services/UpdateRecipeMenuService.cs | 30 +++++++++++++++---- CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs | 17 +++++++++-- 5 files changed, 47 insertions(+), 18 deletions(-) diff --git a/CRUDRecipeEF.BL/Services/IIngredientService.cs b/CRUDRecipeEF.BL/Services/IIngredientService.cs index 3ef3c55..07f3b83 100644 --- a/CRUDRecipeEF.BL/Services/IIngredientService.cs +++ b/CRUDRecipeEF.BL/Services/IIngredientService.cs @@ -28,6 +28,5 @@ public interface IIngredientService /// /// Task DeleteIngredient(string name); - Task UpdateIngredient(IngredientDTO ingredientDTO, string ingredientName); } } \ No newline at end of file diff --git a/CRUDRecipeEF.BL/Services/IUpdateRecipeMenuService.cs b/CRUDRecipeEF.BL/Services/IUpdateRecipeMenuService.cs index 6d37f62..356de4a 100644 --- a/CRUDRecipeEF.BL/Services/IUpdateRecipeMenuService.cs +++ b/CRUDRecipeEF.BL/Services/IUpdateRecipeMenuService.cs @@ -1,4 +1,5 @@ -using System; +using CRUDRecipeEF.DAL.DTOs; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -8,7 +9,9 @@ namespace CRUDRecipeEF.BL.Services { public interface IUpdateRecipeMenuService { - Task UpdateRecipeName(); - Task UpdateRecipeIngredient(); + Task UpdateIngredient(IngredientDTO ingredientDTO, string ingredientName); + Task UpdateRecipeName(RecipeDTO recipeDTO, string name); + + } } diff --git a/CRUDRecipeEF.BL/Services/IngredientService.cs b/CRUDRecipeEF.BL/Services/IngredientService.cs index fffe3d4..92eac42 100644 --- a/CRUDRecipeEF.BL/Services/IngredientService.cs +++ b/CRUDRecipeEF.BL/Services/IngredientService.cs @@ -62,13 +62,7 @@ public Task GetIngredientDTOByNameAsync(string name) return _ingredientRepo.GetIngredientDTOByNameAsync(name); } - public async Task UpdateIngredient(IngredientDTO ingredientDTO, string ingredientName) - { - var ingredient = await GetIngredientByNameIfExistsAsync(ingredientName); - - _mapper.Map(ingredientDTO, ingredient); - await _unitOfWork.SaveAsync(); - } + /// /// Gets an ingredient by name if it exists diff --git a/CRUDRecipeEF.BL/Services/UpdateRecipeMenuService.cs b/CRUDRecipeEF.BL/Services/UpdateRecipeMenuService.cs index dbcd3e8..47809b2 100644 --- a/CRUDRecipeEF.BL/Services/UpdateRecipeMenuService.cs +++ b/CRUDRecipeEF.BL/Services/UpdateRecipeMenuService.cs @@ -1,5 +1,7 @@ using AutoMapper; using CRUDRecipeEF.DAL.Data; +using CRUDRecipeEF.DAL.DTOs; +using CRUDRecipeEF.DAL.Repositories; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -14,24 +16,42 @@ public class UpdateRecipeMenuService : IUpdateRecipeMenuService private readonly RecipeContext _context; private readonly ILogger _logger; private readonly IMapper _mapper; + private readonly IIngredientService _ingredientService; + private readonly IUnitOfWork _unitOfWork; + private readonly IRecipeService _recipeService; public UpdateRecipeMenuService(RecipeContext context, IMapper mapper, - ILogger logger) + IIngredientService ingredientService, + ILogger logger, + IUnitOfWork unitOfWork, + IRecipeService recipeService) { _context = context; _mapper = mapper; + _ingredientService = ingredientService; _logger = logger; + _unitOfWork = unitOfWork; + _recipeService = recipeService; } - public Task UpdateRecipeIngredient() + public async Task UpdateIngredient(IngredientDTO ingredientDTO, string ingredientName) { - throw new NotImplementedException(); + + var ingredient = await _ingredientService.GetIngredientDTOByNameAsync(ingredientDTO.Name); + + _mapper.Map(ingredientDTO, ingredient); + + await _unitOfWork.SaveAsync(); } - public Task UpdateRecipeName() + public async Task UpdateRecipeName(RecipeDTO recipeDTO, string newName) { - throw new NotImplementedException(); + var recipe = await _recipeService.GetRecipeByName(newName); + + _mapper.Map(recipeDTO, recipe); + + await _unitOfWork.SaveAsync(); } } } diff --git a/CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs b/CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs index 6f3fa79..f3ff6ac 100644 --- a/CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs +++ b/CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs @@ -1,5 +1,6 @@ using CRUDRecipeEF.BL.Services; using CRUDRecipeEF.DAL.Data; +using CRUDRecipeEF.DAL.DTOs; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using System; @@ -33,6 +34,7 @@ public UpdateRecipeMenu(IRecipeService recipeService, _logger = logger; _context = context; } + RecipeDTO findRecipe = new RecipeDTO(); public async Task Show() { @@ -109,12 +111,23 @@ private async Task ExecuteUpdateRecipeSelection(RecipeUpdateOption recipeUpdateO private async Task UpdateRecipeIngredient() { - _updateMenuService.UpdateRecipeIngredient(); + ConsoleHelper.ColorWriteLine("Which one is the ingredient you want to change: "); + var input = Console.ReadLine(); + + var ingredient = await _ingredientService.GetIngredientDTOByNameAsync(input); + + ConsoleHelper.ColorWriteLine("Which is the new name of the ingredient: "); + var newName = Console.ReadLine(); + + await _updateMenuService.UpdateIngredient(ingredient, newName); } private async Task UpdateRecipeName() { - _updateMenuService.UpdateRecipeName(); + ConsoleHelper.ColorWriteLine("Which is the new recipe name: "); + var newName = Console.ReadLine(); + + await _updateMenuService.UpdateRecipeName(findRecipe, newName); } } From 9ea071b52b628421ef81de1d32b73cba2037f6a7 Mon Sep 17 00:00:00 2001 From: Yannis Date: Mon, 19 Apr 2021 22:44:14 +0300 Subject: [PATCH 5/7] -Resolve the issue with nullException --- .../Services/IUpdateRecipeMenuService.cs | 2 -- .../Services/UpdateRecipeMenuService.cs | 5 ++++- CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs | 14 +++++++++----- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/CRUDRecipeEF.BL/Services/IUpdateRecipeMenuService.cs b/CRUDRecipeEF.BL/Services/IUpdateRecipeMenuService.cs index 356de4a..0bd6ce0 100644 --- a/CRUDRecipeEF.BL/Services/IUpdateRecipeMenuService.cs +++ b/CRUDRecipeEF.BL/Services/IUpdateRecipeMenuService.cs @@ -11,7 +11,5 @@ public interface IUpdateRecipeMenuService { Task UpdateIngredient(IngredientDTO ingredientDTO, string ingredientName); Task UpdateRecipeName(RecipeDTO recipeDTO, string name); - - } } diff --git a/CRUDRecipeEF.BL/Services/UpdateRecipeMenuService.cs b/CRUDRecipeEF.BL/Services/UpdateRecipeMenuService.cs index 47809b2..8d4c5ea 100644 --- a/CRUDRecipeEF.BL/Services/UpdateRecipeMenuService.cs +++ b/CRUDRecipeEF.BL/Services/UpdateRecipeMenuService.cs @@ -1,6 +1,7 @@ using AutoMapper; using CRUDRecipeEF.DAL.Data; using CRUDRecipeEF.DAL.DTOs; +using CRUDRecipeEF.DAL.Entities; using CRUDRecipeEF.DAL.Repositories; using Microsoft.Extensions.Logging; using System; @@ -40,7 +41,9 @@ public async Task UpdateIngredient(IngredientDTO ingredientDTO, string ingredien var ingredient = await _ingredientService.GetIngredientDTOByNameAsync(ingredientDTO.Name); - _mapper.Map(ingredientDTO, ingredient); + ingredientDTO.Name = ingredientName; + + //_mapper.Map(ingredient); await _unitOfWork.SaveAsync(); } diff --git a/CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs b/CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs index f3ff6ac..014486f 100644 --- a/CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs +++ b/CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs @@ -15,7 +15,7 @@ public class UpdateRecipeMenu : IUpdateRecipeMenu { private readonly IRecipeService _recipeService; private readonly IIngredientService _ingredientService; - private readonly IUpdateRecipeMenuService _updateMenuService; + private readonly IUpdateRecipeMenuService _updateRecipeMenuService; private readonly RecipeContext _context; private readonly ILogger _logger; @@ -27,12 +27,14 @@ private enum RecipeUpdateOption public UpdateRecipeMenu(IRecipeService recipeService, IIngredientService ingredientService, RecipeContext context, - ILogger logger) + ILogger logger, + IUpdateRecipeMenuService updateRecipeMenuService) { _recipeService = recipeService; _ingredientService = ingredientService; _logger = logger; _context = context; + _updateRecipeMenuService = updateRecipeMenuService; } RecipeDTO findRecipe = new RecipeDTO(); @@ -114,12 +116,14 @@ private async Task UpdateRecipeIngredient() ConsoleHelper.ColorWriteLine("Which one is the ingredient you want to change: "); var input = Console.ReadLine(); - var ingredient = await _ingredientService.GetIngredientDTOByNameAsync(input); + IngredientDTO ingredient = new IngredientDTO(); + + ingredient = await _ingredientService.GetIngredientDTOByNameAsync(input); ConsoleHelper.ColorWriteLine("Which is the new name of the ingredient: "); var newName = Console.ReadLine(); - await _updateMenuService.UpdateIngredient(ingredient, newName); + await _updateRecipeMenuService.UpdateIngredient(ingredient, newName); } private async Task UpdateRecipeName() @@ -127,7 +131,7 @@ private async Task UpdateRecipeName() ConsoleHelper.ColorWriteLine("Which is the new recipe name: "); var newName = Console.ReadLine(); - await _updateMenuService.UpdateRecipeName(findRecipe, newName); + await _updateRecipeMenuService.UpdateRecipeName(findRecipe, newName); } } From dd33bab301f01659f7db3fe9be0fec1cda0d1b50 Mon Sep 17 00:00:00 2001 From: Yannis Date: Tue, 20 Apr 2021 23:21:17 +0300 Subject: [PATCH 6/7] Completed UpdateIngredient --- .../Services/IUpdateRecipeMenuService.cs | 2 +- .../Services/UpdateRecipeMenuService.cs | 14 +++++++------- CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs | 18 ++++++++++++------ 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/CRUDRecipeEF.BL/Services/IUpdateRecipeMenuService.cs b/CRUDRecipeEF.BL/Services/IUpdateRecipeMenuService.cs index 0bd6ce0..fc9fa06 100644 --- a/CRUDRecipeEF.BL/Services/IUpdateRecipeMenuService.cs +++ b/CRUDRecipeEF.BL/Services/IUpdateRecipeMenuService.cs @@ -9,7 +9,7 @@ namespace CRUDRecipeEF.BL.Services { public interface IUpdateRecipeMenuService { - Task UpdateIngredient(IngredientDTO ingredientDTO, string ingredientName); + Task UpdateIngredient(string name, string ingredientName); Task UpdateRecipeName(RecipeDTO recipeDTO, string name); } } diff --git a/CRUDRecipeEF.BL/Services/UpdateRecipeMenuService.cs b/CRUDRecipeEF.BL/Services/UpdateRecipeMenuService.cs index 8d4c5ea..e3dff0d 100644 --- a/CRUDRecipeEF.BL/Services/UpdateRecipeMenuService.cs +++ b/CRUDRecipeEF.BL/Services/UpdateRecipeMenuService.cs @@ -20,13 +20,15 @@ public class UpdateRecipeMenuService : IUpdateRecipeMenuService private readonly IIngredientService _ingredientService; private readonly IUnitOfWork _unitOfWork; private readonly IRecipeService _recipeService; + private readonly IIngredientRepo _ingredientRepo; public UpdateRecipeMenuService(RecipeContext context, IMapper mapper, IIngredientService ingredientService, ILogger logger, IUnitOfWork unitOfWork, - IRecipeService recipeService) + IRecipeService recipeService, + IIngredientRepo ingredientRepo) { _context = context; _mapper = mapper; @@ -34,16 +36,14 @@ public UpdateRecipeMenuService(RecipeContext context, _logger = logger; _unitOfWork = unitOfWork; _recipeService = recipeService; + _ingredientRepo = ingredientRepo; } - public async Task UpdateIngredient(IngredientDTO ingredientDTO, string ingredientName) + public async Task UpdateIngredient(string input, string ingredientName) { + Ingredient ingredient = await _ingredientRepo.GetIngredientByNameAsync(input); - var ingredient = await _ingredientService.GetIngredientDTOByNameAsync(ingredientDTO.Name); - - ingredientDTO.Name = ingredientName; - - //_mapper.Map(ingredient); + ingredient.Name = ingredientName; await _unitOfWork.SaveAsync(); } diff --git a/CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs b/CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs index 014486f..8b5937e 100644 --- a/CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs +++ b/CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs @@ -114,22 +114,28 @@ private async Task ExecuteUpdateRecipeSelection(RecipeUpdateOption recipeUpdateO private async Task UpdateRecipeIngredient() { ConsoleHelper.ColorWriteLine("Which one is the ingredient you want to change: "); - var input = Console.ReadLine(); + var input = Console.ReadLine().ToLower().Trim(); - IngredientDTO ingredient = new IngredientDTO(); + var ingredient = await _ingredientService.GetIngredientDTOByNameAsync(input); - ingredient = await _ingredientService.GetIngredientDTOByNameAsync(input); + if (ingredient == null) + { + ConsoleHelper.ColorWriteLine(ConsoleColor.Red, $"Attempted to update an ingredient that does not exist: {input}"); + Console.WriteLine(); + _logger.LogDebug($"Attempted to get ingredient that does not exist: {input}"); + throw new KeyNotFoundException("Ingredient doesnt exist"); + } ConsoleHelper.ColorWriteLine("Which is the new name of the ingredient: "); - var newName = Console.ReadLine(); + var newName = Console.ReadLine().ToLower().Trim(); - await _updateRecipeMenuService.UpdateIngredient(ingredient, newName); + await _updateRecipeMenuService.UpdateIngredient(input, newName); } private async Task UpdateRecipeName() { ConsoleHelper.ColorWriteLine("Which is the new recipe name: "); - var newName = Console.ReadLine(); + var newName = Console.ReadLine().ToLower().Trim(); await _updateRecipeMenuService.UpdateRecipeName(findRecipe, newName); } From 4019c983a2ae980f656e7a7365860e2a27218134 Mon Sep 17 00:00:00 2001 From: Yannis Date: Thu, 29 Apr 2021 17:40:57 +0300 Subject: [PATCH 7/7] -Removed DataContext from menu and unused items --- .../Services/UpdateRecipeMenuService.cs | 10 +--------- CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs | 15 ++++++--------- 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/CRUDRecipeEF.BL/Services/UpdateRecipeMenuService.cs b/CRUDRecipeEF.BL/Services/UpdateRecipeMenuService.cs index e3dff0d..6b97250 100644 --- a/CRUDRecipeEF.BL/Services/UpdateRecipeMenuService.cs +++ b/CRUDRecipeEF.BL/Services/UpdateRecipeMenuService.cs @@ -14,26 +14,18 @@ namespace CRUDRecipeEF.BL.Services { public class UpdateRecipeMenuService : IUpdateRecipeMenuService { - private readonly RecipeContext _context; - private readonly ILogger _logger; private readonly IMapper _mapper; - private readonly IIngredientService _ingredientService; private readonly IUnitOfWork _unitOfWork; private readonly IRecipeService _recipeService; private readonly IIngredientRepo _ingredientRepo; - public UpdateRecipeMenuService(RecipeContext context, + public UpdateRecipeMenuService( IMapper mapper, - IIngredientService ingredientService, - ILogger logger, IUnitOfWork unitOfWork, IRecipeService recipeService, IIngredientRepo ingredientRepo) { - _context = context; _mapper = mapper; - _ingredientService = ingredientService; - _logger = logger; _unitOfWork = unitOfWork; _recipeService = recipeService; _ingredientRepo = ingredientRepo; diff --git a/CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs b/CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs index 8b5937e..9539679 100644 --- a/CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs +++ b/CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs @@ -13,7 +13,6 @@ namespace CRUDRecipeEF.PL.Menus { public class UpdateRecipeMenu : IUpdateRecipeMenu { - private readonly IRecipeService _recipeService; private readonly IIngredientService _ingredientService; private readonly IUpdateRecipeMenuService _updateRecipeMenuService; private readonly RecipeContext _context; @@ -24,13 +23,11 @@ private enum RecipeUpdateOption InValid = 0, Name = 1, Ingredient = 2, GoBack = 3 }; - public UpdateRecipeMenu(IRecipeService recipeService, - IIngredientService ingredientService, + public UpdateRecipeMenu(IIngredientService ingredientService, RecipeContext context, ILogger logger, IUpdateRecipeMenuService updateRecipeMenuService) { - _recipeService = recipeService; _ingredientService = ingredientService; _logger = logger; _context = context; @@ -114,22 +111,22 @@ private async Task ExecuteUpdateRecipeSelection(RecipeUpdateOption recipeUpdateO private async Task UpdateRecipeIngredient() { ConsoleHelper.ColorWriteLine("Which one is the ingredient you want to change: "); - var input = Console.ReadLine().ToLower().Trim(); + var ingredientToBeChanged = Console.ReadLine().ToLower().Trim(); - var ingredient = await _ingredientService.GetIngredientDTOByNameAsync(input); + var ingredient = await _ingredientService.GetIngredientDTOByNameAsync(ingredientToBeChanged); if (ingredient == null) { - ConsoleHelper.ColorWriteLine(ConsoleColor.Red, $"Attempted to update an ingredient that does not exist: {input}"); + ConsoleHelper.ColorWriteLine(ConsoleColor.Red, $"Attempted to update an ingredient that does not exist: {ingredientToBeChanged}"); Console.WriteLine(); - _logger.LogDebug($"Attempted to get ingredient that does not exist: {input}"); + _logger.LogDebug($"Attempted to get ingredient that does not exist: {ingredientToBeChanged}"); throw new KeyNotFoundException("Ingredient doesnt exist"); } ConsoleHelper.ColorWriteLine("Which is the new name of the ingredient: "); var newName = Console.ReadLine().ToLower().Trim(); - await _updateRecipeMenuService.UpdateIngredient(input, newName); + await _updateRecipeMenuService.UpdateIngredient(ingredientToBeChanged, newName); } private async Task UpdateRecipeName()