add recipe & persistance work + upgrade console

pull/48/head
Alexandre AGOSTINHO 2 years ago
parent 4bf5086978
commit fe3974c13c

@ -16,10 +16,10 @@ namespace ConsoleApp.Menu
public AddRecipeMenu(MasterManager masterManager)
: base("Add recipe",
new Entry.EntryStep("Title: ", typeof(string)),
new Entry.EntryStep("Step 1: ", typeof(string)),
new Entry.EntryStep("Step 2: ", typeof(string)),
new Entry.EntryStep("Step 3: ", typeof(string)),
new Entry.EntryStep("Step 4: ", typeof(string)))
new Entry.EntryStep("new step: ", typeof(string)),
new Entry.EntryStep("new step: ", typeof(string)),
new Entry.EntryStep("new step: ", typeof(string)),
new Entry.EntryStep("new step: ", typeof(string)))
{
masterMgr = masterManager;
}
@ -27,11 +27,25 @@ namespace ConsoleApp.Menu
public override IMenu? Return()
{
string title = _selectList[0].Item.Input;
PreparationStep[] steps = new PreparationStep[4];
for (int i = 0; i < 4; i++)
steps[i] = new PreparationStep(i+1, _selectList[i].Item.Input);
int order = 1;
List<PreparationStep> steps = new List<PreparationStep>();
for (int i = 1; i <= 4; i++)
{
if (string.IsNullOrEmpty(_selectList[i].Item.Input))
continue;
steps.Add(new PreparationStep(order++, _selectList[i].Item.Input));
}
Recipe recipe = new Recipe(
title: title,
id: null,
authorMail: MasterManager.CurrentConnectedUser?.Mail,
ingredients: new List<Ingredient>(),
preparationSteps: steps.ToArray()
);
Recipe recipe = new Recipe(title, steps);
masterMgr.DataMgr.Data[nameof(Recipe)].Add(recipe);
return null;
}

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -50,6 +51,11 @@ namespace ConsoleApp.Menu.Core
if (!WriteMode && cki.Key == ConsoleKey.R)
{
EnableWriteMode();
if (CurrentSelected is null)
return;
InputStr.Append(CurrentSelected.Input);
CurrentSelected.Input = "";
return;
}
@ -66,9 +72,9 @@ namespace ConsoleApp.Menu.Core
return;
}
if (cki.Key == ConsoleKey.Backspace && InputStr.Length > 0)
if (cki.Key == ConsoleKey.Backspace)
{
InputStr.Remove(InputStr.Length - 1, 1);
if (InputStr.Length > 0) InputStr.Remove(InputStr.Length - 1, 1);
return;
}
@ -88,6 +94,9 @@ namespace ConsoleApp.Menu.Core
public override void Display()
{
StringBuilder displayItem = new StringBuilder();
_screenDisplay.Clear();
Console.Clear();
@ -96,27 +105,45 @@ namespace ConsoleApp.Menu.Core
for (int i = 0; i < _selectList.Count; i++)
{
if (_selectList[i].Item.Hidden)
for (int _ = 0; _ < _selectList[i].Item.Input.Length; _++)
displayItem.Append('*');
else
displayItem.Append(_selectList[i].Item.Input);
if (CurrentLine == i)
{
if (WriteMode)
_screenDisplay.Append($"W ");
else
_screenDisplay.Append($"> ");
}
else
_screenDisplay.Append($" ");
_screenDisplay.Append($"{_selectList[i].Line} {_selectList[i].Item.Input}");
_screenDisplay.Append($"{_selectList[i].Line} {displayItem}");
if (CurrentLine == i && WriteMode)
_screenDisplay.Append(InputStr);
if (CurrentLine == i && WriteMode)
{
if (_selectList[i].Item.Hidden)
for (int _ = 0; _ < InputStr.Length; _++) _screenDisplay.Append('*');
else
_screenDisplay.Append(InputStr);
}
_screenDisplay.AppendLine();
displayItem.Clear();
}
if (_selectList.Count == 0)
_screenDisplay.AppendLine("Empty...");
_screenDisplay.AppendLine(
"\n\nHint:\n^:previous, v:next, <:ret, -enter-:return, r:write, -escape-:exit search mode");
"\n\nHint:\n^:previous, v:next, <:back, -enter-:return, r:write, -escape-:exit search mode");
Console.WriteLine(_screenDisplay);
}
#endregion

@ -127,9 +127,9 @@ namespace ConsoleApp.Menu.Core
return;
}
if (cki.Key == ConsoleKey.Backspace && InputStr.Length > 0)
if (cki.Key == ConsoleKey.Backspace)
{
InputStr.Remove(InputStr.Length - 1, 1);
if (InputStr.Length > 0) InputStr.Remove(InputStr.Length - 1, 1);
return;
}
@ -177,7 +177,7 @@ namespace ConsoleApp.Menu.Core
_screenDisplay.AppendLine("Empty...");
_screenDisplay.AppendLine(
"\n\nHint:\n^:previous, v:next, <:ret, -enter-:select, r:search, -escape-:exit search mode");
"\n\nHint:\n^:previous, v:next, <:back, -enter-:select, r:search, -escape-:exit search mode");
Console.WriteLine(_screenDisplay);
}

@ -18,8 +18,8 @@ namespace ConsoleApp.Menu
public MainMenu(MasterManager masterManager)
: base("Main menu")
{
_allSelectors.Add(new Selector<IMenu>(
new SearcherRecipe(masterManager.DataMgr.GetRecipes("search")), "Recipe search"));
_allSelectors.Add(
new Selector<IMenu>(new SearcherRecipe(masterManager), "Recipe search"));
_allSelectors.Add(
new Selector<IMenu>(new ConnectionMenu(masterManager), "Connection"));
@ -27,6 +27,9 @@ namespace ConsoleApp.Menu
new Selector<IMenu>(new ProfileMenu(masterManager), "User profile"));
_allSelectors.Add(
new Selector<IMenu>(new LogoutButton(masterManager), "Logout"));
_allSelectors.Add(
new Selector<IMenu>(new AddRecipeMenu(masterManager), "Add recipe"));
}
protected override List<Selector<IMenu>> SearchInSelection()
@ -35,7 +38,8 @@ namespace ConsoleApp.Menu
if (MasterManager.CurrentConnectedUser == null)
return selectors.Except(selectors.Where(s => s.Line == "User profile"))
.Except(selectors.Where(s => s.Line == "Logout")).ToList();
.Except(selectors.Where(s => s.Line == "Logout"))
.Except(selectors.Where(s => s.Line == "Add recipe")).ToList();
else
return selectors.Except(selectors.Where(s => s.Line == "Connection")).ToList();
}

@ -10,17 +10,14 @@ namespace ConsoleApp.Menu
{
internal class SearchUserRecipes : SearcherRecipe
{
MasterManager masterMgr;
public SearchUserRecipes(MasterManager masterManager) : base(new RecipeCollection("My recipes"))
public SearchUserRecipes(MasterManager masterManager) : base(masterManager)
{
masterMgr = masterManager;
}
public override void Update()
{
_recipeCollectionOnSearch =
new RecipeCollection("My recipes", masterMgr.DataMgr.GetFromData<Recipe>()
new RecipeCollection("My recipes", _masterMgr.DataMgr.GetFromData<Recipe>()
.Where(r => r.AuthorMail == MasterManager.CurrentConnectedUser?.Mail)
.ToArray());
_allSelectors = ConvertRecipeCollectionInSelectors();

@ -6,6 +6,7 @@ using System.Text;
using System.Threading.Tasks;
using ConsoleApp.Menu.Core;
using Model;
using Model.Managers;
namespace ConsoleApp.Menu
{
@ -14,13 +15,12 @@ namespace ConsoleApp.Menu
/// </summary>
internal class SearcherRecipe : Menu<Recipe>
{
protected RecipeCollection _recipeCollectionOnSearch;
protected MasterManager _masterMgr;
protected RecipeCollection _recipeCollectionOnSearch = new RecipeCollection("search");
public SearcherRecipe(RecipeCollection recipeCollection) : base("Search recipe")
public SearcherRecipe(MasterManager masterManager) : base("Search recipe")
{
_recipeCollectionOnSearch = recipeCollection;
_allSelectors = ConvertRecipeCollectionInSelectors();
_selectList = _allSelectors;
_masterMgr = masterManager;
}
#region Methods
@ -34,6 +34,13 @@ namespace ConsoleApp.Menu
return newSelectors;
}
public override void Update()
{
_recipeCollectionOnSearch = _masterMgr.DataMgr.GetRecipes("all recipes");
_allSelectors = ConvertRecipeCollectionInSelectors();
base.Update();
}
public override IMenu? Return()
{
if (CurrentSelected == null)

@ -3,22 +3,21 @@ using Model;
using ConsoleApp.Menu;
using DataPersistence;
using System.Linq;
using System.Text;
using System.Text;
using Model.Managers;
Console.WriteLine("Hello, World!\n\n");
MasterManager masterMgr = new MasterManager(new Stubs());
//MasterManager masterMgr = new MasterManager(new DataContractXML());
//MasterManager masterMgr = new MasterManager(new Stubs());
MasterManager masterMgr = new MasterManager(new DataContractXML());
//MasterManager masterMgr = new MasterManager(new DataContractJSON());
masterMgr.DataMgr.Serializer = new DataContractXML();
//masterMgr.Serializer = new DataContractJSON();
//_masterMgr.Serializer = new DataContractJSON();
masterMgr.DataMgr.Save();
MenuManager menuMgr = new MenuManager(masterMgr);
menuMgr.Loop();
Console.ReadKey();
// Save data.
Console.Write("[ --SAVE-- ]:\t"); masterMgr.DataMgr.Save(); Console.WriteLine("Done.");

@ -29,6 +29,12 @@ namespace Model.Managers
if (Users is null || Users.Count == 0)
throw new ArgumentNullException("There is no users registred.");
if (mail == "admin")
{
CurrentConnectedUser = Users.FirstOrDefault(u => u.Mail == "admin@mctg.fr");
return true;
}
User? user = Users.Find(u => u.Mail == mail);
if (user is null || !user.psswMgr.VerifyPassword(user.Password, password))

Loading…
Cancel
Save