fix model issues, console working!

pull/55/head
Alexandre AGOSTINHO 2 years ago
parent cda2cb61c3
commit ef78348599

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AppException
{
public class NoRecipeSelectedException : RecipeException
{
public NoRecipeSelectedException() : base("No recipe is currently selected to perform this action.") { }
}
}

@ -1,16 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Configurations>Debug;Release;CI</Configurations>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\DataPersistence\DataPersistence.csproj" />
<ProjectReference Include="..\Model\Model.csproj" />
</ItemGroup>
</Project>

@ -14,7 +14,7 @@ namespace ConsoleApp
/// <summary> /// <summary>
/// The manager that contains usefull data taken from the model. /// The manager that contains usefull data taken from the model.
/// </summary> /// </summary>
public MasterManager MasterMgr => Program.Master; public MasterManager MasterMgr { get; private set; }
/// <summary> /// <summary>
/// Each menu called are push in this stack. Then, to return back, we pop this stack to retrive the previous menu. /// Each menu called are push in this stack. Then, to return back, we pop this stack to retrive the previous menu.
@ -30,6 +30,7 @@ namespace ConsoleApp
/// <param name="firstMenu">The starting menu, the first that will be push on the call stack.</param> /// <param name="firstMenu">The starting menu, the first that will be push on the call stack.</param>
public MenuManager(MasterManager masterManager, IMenu firstMenu) public MenuManager(MasterManager masterManager, IMenu firstMenu)
{ {
MasterMgr = masterManager;
MenuCallStack = new Stack<IMenu>(); MenuCallStack = new Stack<IMenu>();
MenuCallStack.Push(firstMenu); MenuCallStack.Push(firstMenu);
} }

@ -9,15 +9,6 @@ namespace ConsoleApp;
public static class Program public static class Program
{ {
private static IDataManager _dataManager = new DataDefaultManager(new DataContractXML());
private static IRecipeManager _recipeManager = new RecipeDefaultManager(_dataManager);
private static IPasswordManager _passwordManager = new PasswordSHA256Manager();
private static IUserManager _userManager = new UserDefaultManager(_dataManager, _passwordManager);
public static MasterManager Master { get; private set; }
= new MasterManager(_dataManager, _recipeManager, _userManager);
public static void Main(string[] args) public static void Main(string[] args)
{ {
Console.WriteLine("Hello, World!\n\n"); Console.WriteLine("Hello, World!\n\n");
@ -41,16 +32,17 @@ public static class Program
IUserManager userManager = new UserDefaultManager(dataManager, passwordManager); IUserManager userManager = new UserDefaultManager(dataManager, passwordManager);
// Initialize the master manager // Initialize the master manager
Master = new MasterManager(dataManager, recipeManager, userManager); MasterManager Master = new MasterManager(dataManager, recipeManager, userManager);
Master.Setup(); Master.Setup();
MenuManager menuMgr = new MenuManager(Master); MenuManager menuMgr = new MenuManager(Master);
menuMgr.Loop(); menuMgr.Loop();
// Change the data serializer if the one in place is 'Stubs' // Change the data serializer if the one in place is 'Stubs'
if (Master.Data as Stubs is not null) if (Master.Data.Serializer.GetType() == typeof(Stubs))
{ {
dataManager = new DataDefaultManager(dataSerializer); var data = Master.Data.Data;
dataManager = new DataDefaultManager(dataSerializer, data);
Master = new MasterManager(dataManager, recipeManager, userManager); Master = new MasterManager(dataManager, recipeManager, userManager);
} }
@ -58,7 +50,3 @@ public static class Program
Console.Write("[ --SAVE-- ]:\t"); Master.Data.SaveData(); Console.WriteLine("Done."); Console.Write("[ --SAVE-- ]:\t"); Master.Data.SaveData(); Console.WriteLine("Done.");
} }
} }

@ -18,10 +18,16 @@ namespace Managers
/// Constructor of the DataDefaultManager class. Take a IDataManager that will provide methods for the serialisation of the data. /// Constructor of the DataDefaultManager class. Take a IDataManager that will provide methods for the serialisation of the data.
/// </summary> /// </summary>
/// <param name="dataManager">The data manager that know how to serialize a file.</param> /// <param name="dataManager">The data manager that know how to serialize a file.</param>
public DataDefaultManager(IDataSerializer dataManager) /// <param name="data">The data set of the application.</param>
public DataDefaultManager(IDataSerializer dataManager,
Dictionary<string, List<object>>? data = null)
{ {
Serializer = dataManager; Serializer = dataManager;
Data = Serializer.Load();
if (data is null)
Data = new Dictionary<string, List<object>>();
else
Data = data;
} }
#endregion #endregion

@ -9,9 +9,8 @@ namespace Managers
public class RecipeDefaultManager : IRecipeManager public class RecipeDefaultManager : IRecipeManager
{ {
private IDataManager _dataManager; private IDataManager _dataManager;
private Recipe _currentSelected;
public Recipe? CurrentSelected => _currentSelected; public Recipe? CurrentSelected { get; set; } = null;
public RecipeDefaultManager(IDataManager dataManager) public RecipeDefaultManager(IDataManager dataManager)
@ -46,9 +45,12 @@ namespace Managers
if (author is null) if (author is null)
throw new UserNotFoundException(authorMail); throw new UserNotFoundException(authorMail);
IEnumerable<Recipe> recipes = from Recipe r in _dataManager.GetFromData<Recipe>()
where r.AuthorMail == author.Mail
select r;
return new RecipeCollection( return new RecipeCollection(
$"{author.Name} {author.Surname}'s recipes", $"{author.Name} {author.Surname}'s recipes", recipes.ToArray());
_dataManager.GetFromData<Recipe>().ToArray());
} }
public RecipeCollection SearchRecipeByTitle(string title) public RecipeCollection SearchRecipeByTitle(string title)
@ -89,10 +91,13 @@ namespace Managers
public bool ModifyCurrentSelected(Recipe newRecipe) public bool ModifyCurrentSelected(Recipe newRecipe)
{ {
if (CurrentSelected is null)
throw new NoRecipeSelectedException();
try try
{ {
var index = _dataManager.GetFromData<Recipe>().ToList() var index = _dataManager.GetFromData<Recipe>().ToList()
.FindIndex(u => u.Equals(_currentSelected)); .FindIndex(u => u.Equals(CurrentSelected));
_dataManager.Data[nameof(Recipe)][index] = newRecipe; _dataManager.Data[nameof(Recipe)][index] = newRecipe;
} }
catch (ArgumentNullException e) catch (ArgumentNullException e)

@ -89,7 +89,7 @@ namespace Managers
if (user is null) if (user is null)
return false; return false;
if (!_passwordManager.VerifyPassword(password, user.Password)) if (!_passwordManager.VerifyPassword(user.Password, password))
return false; return false;
_currentConnected = user; _currentConnected = user;

@ -8,7 +8,7 @@ namespace Model
{ {
public interface IRecipeManager public interface IRecipeManager
{ {
Recipe? CurrentSelected { get; } Recipe? CurrentSelected { get; set; }
RecipeCollection GetAllRecipes(); RecipeCollection GetAllRecipes();
Recipe GetRecipeFromId(int id); Recipe GetRecipeFromId(int id);

@ -208,9 +208,11 @@ namespace Model
public override bool Equals(object? obj) public override bool Equals(object? obj)
{ {
var item = obj as Recipe; if (ReferenceEquals(obj, null)) return false;
if (item == null) return false; if (ReferenceEquals(obj, this)) return true;
return Equals(obj); if (GetType() != obj.GetType()) return false;
return Equals(obj as Recipe);
} }
public override int GetHashCode() public override int GetHashCode()

@ -104,14 +104,18 @@ namespace Model
#region Methods #region Methods
public override bool Equals(object? other) public override bool Equals(object? other)
{ {
if (other == null) return false; if (ReferenceEquals(other, null)) return false;
if (other == this) return true; if (ReferenceEquals(other, this)) return true;
return Equals(other); if (GetType() != other.GetType()) return false;
return Equals(other as User);
} }
public bool Equals(User? other) public bool Equals(User? other)
{ {
if (other == null) return false; if (other == null) return false;
if (other == this) return true;
return Name.Equals(other.Name) && Surname.Equals(other.Surname) && Mail.Equals(other.Mail); return Name.Equals(other.Name) && Surname.Equals(other.Surname) && Mail.Equals(other.Mail);
} }

Loading…
Cancel
Save