Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update recipe #103

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CRUDRecipeEF.API/CRUDRecipeEF.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<Folder Include="Controllers\" />
<Folder Include="Controllers\" />
</ItemGroup>

</Project>
1 change: 0 additions & 1 deletion CRUDRecipeEF.BL/Services/IIngredientService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,5 @@ public interface IIngredientService
/// <returns></returns>
/// <exception cref="KeyNotFoundException"></exception>
Task DeleteIngredient(string name);
Task UpdateIngredient(IngredientDTO ingredientDTO, string ingredientName);
}
}
1 change: 0 additions & 1 deletion CRUDRecipeEF.BL/Services/IRecipeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ public interface IRecipeService
Task RemoveIngredientFromRecipe(string ingredientName, string recipeName);

Task DeleteRecipe(string name);
Task UpdateRecipe(RecipeDTO recipeDTO, string recipeName);
}
}
15 changes: 15 additions & 0 deletions CRUDRecipeEF.BL/Services/IUpdateRecipeMenuService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using CRUDRecipeEF.DAL.DTOs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CRUDRecipeEF.BL.Services
{
public interface IUpdateRecipeMenuService
{
Task UpdateIngredient(string name, string ingredientName);
Task UpdateRecipeName(RecipeDTO recipeDTO, string name);
}
}
8 changes: 1 addition & 7 deletions CRUDRecipeEF.BL/Services/IngredientService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,7 @@ public Task<IngredientDTO> 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();
}


/// <summary>
/// Gets an ingredient by name if it exists
Expand Down
7 changes: 0 additions & 7 deletions CRUDRecipeEF.BL/Services/RecipeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,6 @@ public async Task<string> 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();
}

/// <summary>
/// </summary>
Expand Down
52 changes: 52 additions & 0 deletions CRUDRecipeEF.BL/Services/UpdateRecipeMenuService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using AutoMapper;
using CRUDRecipeEF.DAL.Data;
using CRUDRecipeEF.DAL.DTOs;
using CRUDRecipeEF.DAL.Entities;
using CRUDRecipeEF.DAL.Repositories;
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 IMapper _mapper;
private readonly IUnitOfWork _unitOfWork;
private readonly IRecipeService _recipeService;
private readonly IIngredientRepo _ingredientRepo;

public UpdateRecipeMenuService(
IMapper mapper,
IUnitOfWork unitOfWork,
IRecipeService recipeService,
IIngredientRepo ingredientRepo)
{
_mapper = mapper;
_unitOfWork = unitOfWork;
_recipeService = recipeService;
_ingredientRepo = ingredientRepo;
}

public async Task UpdateIngredient(string input, string ingredientName)
{
Ingredient ingredient = await _ingredientRepo.GetIngredientByNameAsync(input);

ingredient.Name = ingredientName;

await _unitOfWork.SaveAsync();
}

public async Task UpdateRecipeName(RecipeDTO recipeDTO, string newName)
{
var recipe = await _recipeService.GetRecipeByName(newName);

_mapper.Map(recipeDTO, recipe);

await _unitOfWork.SaveAsync();
}
}
}
13 changes: 13 additions & 0 deletions CRUDRecipeEF.PL/Menus/IUpdateRecipeMenu.cs
Original file line number Diff line number Diff line change
@@ -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 IUpdateRecipeMenu
{
Task Show();
}
}
10 changes: 8 additions & 2 deletions CRUDRecipeEF.PL/Menus/IngredientMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IngredientMenu> logger)
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -166,5 +170,7 @@ private async Task ListIngredients()
Console.WriteLine();
await this.Show();
}


}
}
22 changes: 8 additions & 14 deletions CRUDRecipeEF.PL/Menus/RecipeMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class RecipeMenu : IRecipeMenu
{
private readonly IRecipeService _recipeService;
private readonly IIngredientService _ingredientService;
private readonly IUpdateRecipeMenu _updateMenu;
private readonly ILogger _logger;
private readonly int _recipePerPage = 8;

Expand All @@ -21,12 +22,15 @@ private enum RecipeMenuOption
UpdateRecipe = 5, GoBack = 6
};


public RecipeMenu(IRecipeService recipeService,
IIngredientService ingredientService,
IIngredientService ingredientService,
IUpdateRecipeMenu updateMenu,
ILogger<RecipeMenu> logger)
{
_recipeService = recipeService;
_ingredientService = ingredientService;
_updateMenu = updateMenu;
_logger = logger;
}

Expand Down Expand Up @@ -67,6 +71,7 @@ public async Task Show()
await ExecuteMenuSelection(choice);
}


private async Task ExecuteMenuSelection(RecipeMenuOption option)
{
switch (option)
Expand All @@ -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();
Expand All @@ -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()
{
Expand Down
141 changes: 141 additions & 0 deletions CRUDRecipeEF.PL/Menus/UpdateRecipeMenu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
using CRUDRecipeEF.BL.Services;
using CRUDRecipeEF.DAL.Data;
using CRUDRecipeEF.DAL.DTOs;
using Microsoft.EntityFrameworkCore;
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 : IUpdateRecipeMenu
{
private readonly IIngredientService _ingredientService;
private readonly IUpdateRecipeMenuService _updateRecipeMenuService;
private readonly RecipeContext _context;
private readonly ILogger _logger;

private enum RecipeUpdateOption
{
InValid = 0, Name = 1, Ingredient = 2, GoBack = 3
};

public UpdateRecipeMenu(IIngredientService ingredientService,
RecipeContext context,
ILogger<RecipeMenu> logger,
IUpdateRecipeMenuService updateRecipeMenuService)
{
_ingredientService = ingredientService;
_logger = logger;
_context = context;
_updateRecipeMenuService = updateRecipeMenuService;
}
RecipeDTO findRecipe = new RecipeDTO();

public async Task Show()
{
ConsoleHelper.ColorWriteLine("Please provide the name of the recipe that you want to update: ");
var recipe = Console.ReadLine();

var findRecipe = await _context.Recipes.Include(i => i.Ingredients)
.SingleOrDefaultAsync(r => r.Name.ToLower() == recipe.ToLower().Trim());

if (findRecipe != null)
{
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();

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);
}
else
{
Console.WriteLine();
ConsoleHelper.ColorWriteLine(ConsoleColor.DarkYellow, $"{recipe} does not exist.");
Console.WriteLine();
}
}

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()
{
ConsoleHelper.ColorWriteLine("Which one is the ingredient you want to change: ");
var ingredientToBeChanged = Console.ReadLine().ToLower().Trim();

var ingredient = await _ingredientService.GetIngredientDTOByNameAsync(ingredientToBeChanged);

if (ingredient == null)
{
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: {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(ingredientToBeChanged, newName);
}

private async Task UpdateRecipeName()
{
ConsoleHelper.ColorWriteLine("Which is the new recipe name: ");
var newName = Console.ReadLine().ToLower().Trim();

await _updateRecipeMenuService.UpdateRecipeName(findRecipe, newName);
}

}
}
2 changes: 2 additions & 0 deletions CRUDRecipeEF.PL/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public static IHostBuilder CreateHostBuilder(string[] args)
.AddTransient<IIngredientMenu, IngredientMenu>()
.AddTransient<IRestaurantService, RestaurantService>()
.AddTransient<IRecipeMenu, RecipeMenu>()
.AddTransient<IUpdateRecipeMenu, UpdateRecipeMenu>()
.AddTransient<IUpdateRecipeMenuService, UpdateRecipeMenuService>()
.ConfigureDal();

services.AddDbContext<RecipeContext>(options =>
Expand Down
Binary file removed CRUDRecipeEF.PL/Recipe.db-shm
Binary file not shown.
Empty file removed CRUDRecipeEF.PL/Recipe.db-wal
Empty file.