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

Feature 25 edit options #93

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
ee638b7
Start adding the ability to update ingredients
JoyfulReaper Mar 14, 2021
c6c09c6
Implement ingredient editing
JoyfulReaper Mar 14, 2021
8a3e6bd
cleanup
JoyfulReaper Mar 14, 2021
34692e3
Add UpdateRecipe
JoyfulReaper Mar 14, 2021
0a70f37
Update menu -- not tested
JoyfulReaper Mar 14, 2021
a300046
Converting the DTOs seems odd.. Please fix and show me later
JoyfulReaper Mar 14, 2021
38ca994
Added DTO
miguelb97 Mar 24, 2021
810440f
Was lacking a public on RecipeCategoryAddDTO
miguelb97 Mar 24, 2021
f18a340
Merge pull request #90 from chrisK00/feature-86-AddDTO-Class
chrisK00 Apr 4, 2021
7570850
Create dotnet.yml
chrisK00 Apr 5, 2021
ca47b0d
Split BL and DAL
BusschaertTanguy Apr 5, 2021
8e16fc6
Merge branch 'master' of https://github.com/chrisK00/CRUDRecipeEF
BusschaertTanguy Apr 5, 2021
6a34349
Moved dtos namespace and created ingredient repo classes
Apr 6, 2021
224eabe
ingredient repo and service implemented
Apr 6, 2021
0ef947a
updated using statements inside menu and tests
Apr 6, 2021
139420c
commented out test class that was causing errors since we will be rep…
Apr 7, 2021
3f891dd
Merge pull request #99 from chrisK00/feature/97-ingredient-repo
chrisK00 Apr 7, 2021
c76e28b
renamed old services folder to depreaciated. Will be replaced with Mo…
Apr 7, 2021
14e109d
setup servicetest class
Apr 7, 2021
5f75ade
get ingredient by name and delete ingredient throws exception test
Apr 7, 2021
4025668
add ingredient test
Apr 8, 2021
38a7800
Merge pull request #100 from chrisK00/feature/95-ingredientservice-tests
chrisK00 Apr 11, 2021
3e05316
Merge branch 'feature-25-editOptions'
Apr 16, 2021
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
3 changes: 2 additions & 1 deletion CRUDRecipeEF.BL.DL/Services/IIngredientService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using CRUDRecipeEF.BL.DL.DTOs;
using CRUDRecipeEF.BL.DL.Entities;

namespace CRUDRecipeEF.BL.DL.Services
{
Expand All @@ -13,6 +12,8 @@ public interface IIngredientService

Task<string> AddIngredient(IngredientAddDTO ingredient);

Task UpdateIngredient(string name, IngredientAddDTO ingredientAddDTO);

Task DeleteIngredient(string name);
}
}
2 changes: 2 additions & 0 deletions CRUDRecipeEF.BL.DL/Services/IRecipeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public interface IRecipeService

Task<string> AddRecipe(RecipeAddDTO recipe);

Task UpdateRecipe(string name, RecipeAddDTO recipeAddDTO);

Task<string> AddIngredientToRecipe(IngredientAddDTO ingredient, string recipeName);

Task RemoveIngredientFromRecipe(string ingredientName, string recipeName);
Expand Down
13 changes: 13 additions & 0 deletions CRUDRecipeEF.BL.DL/Services/IngredientService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ public async Task<string> AddIngredient(IngredientAddDTO ingredientAddDTO)
return ingredientAddDTO.Name;
}

public async Task UpdateIngredient(string name, IngredientAddDTO ingredientAddDTO)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
var ing = await _context.Ingredients.SingleOrDefaultAsync(i => i.Name == name);

if(ing == null)
{
throw new KeyNotFoundException($"{ingredientAddDTO.Name} does not exist");
}

ing.Name = ingredientAddDTO.Name;
await Save();
}

/// <summary>
/// Delete an Ingredient from the database
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions CRUDRecipeEF.BL.DL/Services/RecipeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ public async Task<string> AddIngredientToRecipe(IngredientAddDTO ingredientAddDT
return recipeName;
}

public async Task UpdateRecipe(string name, RecipeAddDTO recipeAddDTO)
Copy link
Collaborator

Choose a reason for hiding this comment

The 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>
Expand Down
33 changes: 30 additions & 3 deletions CRUDRecipeEF.PL/Menus/IngredientMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class IngredientMenu : IIngredientMenu
private readonly IIngredientService _ingredientService;
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, EditIngredient = 4, DeleteIngredient = 5, GoBack = 6 };

public IngredientMenu(IIngredientService ingredientService)
{
Expand All @@ -27,9 +27,10 @@ public async Task Show()
ConsoleHelper.ColorWriteLine("1.) New Ingredient");
ConsoleHelper.ColorWriteLine("2.) Lookup Ingredient");
ConsoleHelper.ColorWriteLine("3.) Show Ingredient List");
ConsoleHelper.ColorWriteLine("4.) Delete Ingredient");
ConsoleHelper.ColorWriteLine("4.) Rename Ingredient");
ConsoleHelper.ColorWriteLine("5.) Delete 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 @@ -74,6 +75,9 @@ private async Task ExecuteMenuSelection(IngredientMenuOption option)
case IngredientMenuOption.DeleteIngredient:
await DeleteIngredient();
break;
case IngredientMenuOption.EditIngredient:
await EditIngredient();
break;
case IngredientMenuOption.GoBack:
Console.WriteLine();
break;
Expand All @@ -82,6 +86,29 @@ private async Task ExecuteMenuSelection(IngredientMenuOption option)
}
}

private async Task EditIngredient()
{
ConsoleHelper.ColorWrite("What ingredient would you like to edit: ");
var name = Console.ReadLine();

try
{
var ingredient = await _ingredientService.GetIngredientByName(name);

ConsoleHelper.ColorWrite("What would you like to re-name the ingredient: ");
var newName = Console.ReadLine();

await _ingredientService.UpdateIngredient(name, new IngredientAddDTO { Name = newName });
}
catch (KeyNotFoundException)
{
ConsoleHelper.ColorWriteLine(ConsoleColor.DarkYellow, $"{name} does not exist.");
}

Console.WriteLine();
await this.Show();
}

private async Task LookupIngredient()
{
ConsoleHelper.ColorWrite("What ingredient would you like to lookup: ");
Expand Down
100 changes: 97 additions & 3 deletions CRUDRecipeEF.PL/Menus/RecipeMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;

namespace CRUDRecipeEF.PL.Menus
{
Expand All @@ -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)
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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 };
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automapper can fix this easily

  1. We need a configuration inside the AutoMapperProfiles CreateMap<from, to>
  2. use the mapper like this: _mapper.Map(fromObject)


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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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();
Expand Down