-
Notifications
You must be signed in to change notification settings - Fork 0
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
Feature 25 edit options #93
Changes from 6 commits
ee638b7
c6c09c6
8a3e6bd
34692e3
0a70f37
a300046
38ca994
810440f
f18a340
7570850
ca47b0d
8e16fc6
6a34349
224eabe
0ef947a
139420c
3f891dd
c76e28b
14e109d
5f75ade
4025668
38a7800
3e05316
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,19 @@ public async Task<string> AddIngredientToRecipe(IngredientAddDTO ingredientAddDT | |
return recipeName; | ||
} | ||
|
||
public async Task UpdateRecipe(string name, RecipeAddDTO recipeAddDTO) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I recommend doing it the way we did with automapper in the ingredientservice. Msg me on discord if you want me to explain if it isnt clear whats happening |
||
{ | ||
var recipe = await _context.Recipes.SingleOrDefaultAsync(i => i.Name == name); | ||
|
||
if (recipe == null) | ||
{ | ||
throw new KeyNotFoundException($"{recipeAddDTO.Name} does not exist"); | ||
} | ||
|
||
recipe.Name = recipeAddDTO.Name; | ||
await Save(); | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
using System.Linq; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using AutoMapper; | ||
|
||
namespace CRUDRecipeEF.PL.Menus | ||
{ | ||
|
@@ -13,7 +14,7 @@ public class RecipeMenu : IRecipeMenu | |
private readonly IIngredientService _ingredientService; | ||
private readonly int _recipePerPage = 8; | ||
|
||
private enum RecipeMenuOption { InValid = 0, NewRecipe = 1, LookUpRecipe = 2, ShowRecipe = 3, DeleteRecipe = 4, GoBack = 5 }; | ||
private enum RecipeMenuOption { InValid = 0, NewRecipe = 1, LookUpRecipe = 2, ShowRecipe = 3, EditRecipe = 4, DeleteRecipe = 5, GoBack = 6 }; | ||
|
||
public RecipeMenu(IRecipeService recipeService, | ||
IIngredientService ingredientService) | ||
|
@@ -30,9 +31,10 @@ public async Task Show() | |
ConsoleHelper.ColorWriteLine("1.) New Recipe"); | ||
ConsoleHelper.ColorWriteLine("2.) Lookup Recipe"); | ||
ConsoleHelper.ColorWriteLine("3.) Show Recipe List"); | ||
ConsoleHelper.ColorWriteLine("4.) Delete Recipe"); | ||
ConsoleHelper.ColorWriteLine("4.) Edit Recipe"); | ||
ConsoleHelper.ColorWriteLine("5.) Delete Recipe"); | ||
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; | ||
|
@@ -81,6 +83,9 @@ private async Task ExecuteMenuSelection(RecipeMenuOption option) | |
Console.WriteLine(); | ||
await DeleteRecipe(); | ||
break; | ||
case RecipeMenuOption.EditRecipe: | ||
await EditRecipe(); | ||
break; | ||
case RecipeMenuOption.GoBack: | ||
Console.WriteLine(); | ||
break; | ||
|
@@ -89,6 +94,95 @@ private async Task ExecuteMenuSelection(RecipeMenuOption option) | |
} | ||
} | ||
|
||
private async Task EditRecipe() | ||
{ | ||
ConsoleHelper.ColorWrite("What Recipe would you like to edit: "); | ||
var name = Console.ReadLine(); | ||
|
||
try | ||
{ | ||
// this is super shit code, sorry | ||
//TODO: Fix this mess | ||
var recipe = await _recipeService.GetRecipeByName(name); | ||
RecipeAddDTO recipeAdd = new RecipeAddDTO() { Name = recipe.Name }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Automapper can fix this easily
|
||
|
||
foreach(var i in recipe.Ingredients) | ||
{ | ||
recipeAdd.Ingredients.Add(new IngredientAddDTO { Name = i.Name }); | ||
} | ||
|
||
|
||
ConsoleHelper.ColorWrite("What would you like to edit (N)ame or reset (I)ngredients: "); | ||
var editWhat = Console.ReadLine(); | ||
|
||
if (char.ToUpper(editWhat[0]) == 'N') | ||
{ | ||
|
||
Console.Write("What would you like to re-name the recipe: "); | ||
var newName = Console.ReadLine(); | ||
recipeAdd.Name = newName; | ||
} | ||
else if (char.ToUpper(editWhat[0]) == 'I') | ||
{ | ||
//TODO: Not DRY, extract this and use it in common locations | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea, some sort of generic method or factory is probably the way to go. |
||
bool another = true; | ||
List<IngredientAddDTO> ingredients = new List<IngredientAddDTO>(); | ||
|
||
while (another) | ||
{ | ||
ConsoleHelper.ColorWrite("What ingredeient would you like to add: "); | ||
var input = Console.ReadLine(); | ||
|
||
try | ||
{ | ||
var ingredient = await _ingredientService.GetIngredientByName(input); | ||
var ingredientToAdd = new IngredientAddDTO { Name = ingredient.Name }; | ||
ingredients.Add(ingredientToAdd); | ||
} | ||
catch (KeyNotFoundException) | ||
{ | ||
ConsoleHelper.ColorWriteLine(ConsoleColor.DarkYellow, "The ingredient does not exist!"); | ||
ConsoleHelper.ColorWrite("Would you like to add it? (Y/n): "); | ||
var add = Console.ReadLine(); | ||
|
||
if (Char.ToUpperInvariant(add[0]) == 'N') | ||
{ | ||
ConsoleHelper.ColorWriteLine(ConsoleColor.Red, "Recipe not added."); | ||
Console.WriteLine(); | ||
return; | ||
} | ||
|
||
ingredients.Add(new IngredientAddDTO { Name = input }); | ||
} | ||
|
||
ConsoleHelper.ColorWrite("Would you like to add another ingredient? (y/N): "); | ||
var addAnother = Console.ReadLine(); | ||
|
||
if (Char.ToUpperInvariant(addAnother[0]) != 'Y') | ||
{ | ||
another = false; | ||
} | ||
} | ||
|
||
recipeAdd.Ingredients = ingredients; | ||
} | ||
else | ||
{ | ||
// TODO: I will add a loop later, just want this done for now | ||
Console.WriteLine("Invalid choice!"); | ||
} | ||
|
||
await _recipeService.UpdateRecipe(name, recipeAdd); | ||
} | ||
catch (KeyNotFoundException) | ||
{ | ||
ConsoleHelper.ColorWriteLine(ConsoleColor.DarkYellow, $"{name} does not exist."); | ||
} | ||
|
||
Console.WriteLine(); | ||
await this.Show(); | ||
} | ||
|
||
private async Task ListRecipe() | ||
{ | ||
Console.WriteLine(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ingredient services currently has this update method https://github.com/chrisK00/CRUDRecipeEF/blob/master/CRUDRecipeEF.BL.DL/Services/IIngredientService.cs