|
|
|
@ -1,161 +1,57 @@
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Model.Managers
|
|
|
|
|
|
|
|
|
|
namespace Model
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The Main manager of the model.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class MasterManager : INotifyPropertyChanged
|
|
|
|
|
public class MasterManager
|
|
|
|
|
{
|
|
|
|
|
#region Attributes & Properties
|
|
|
|
|
public event PropertyChangedEventHandler? PropertyChanged;
|
|
|
|
|
|
|
|
|
|
private RecipeCollection _recipesInSearch = new RecipeCollection("");
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The currently connected user. 'null' if no user is connected.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public User? CurrentConnectedUser { get; private set; }
|
|
|
|
|
|
|
|
|
|
public RecipeCollection RecipesInSearch
|
|
|
|
|
{
|
|
|
|
|
get => _recipesInSearch;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_recipesInSearch = value;
|
|
|
|
|
OnPropertyChange();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The collection of all recipes loaded.
|
|
|
|
|
/// Manage the data of the application.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public RecipeCollection Recipes { get; private set; }
|
|
|
|
|
public IDataManager Data { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The collection of all users loaded.
|
|
|
|
|
/// Manage the recipes of the application.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public List<User> Users { get; private set; }
|
|
|
|
|
public IRecipeManager Recipe { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The data manager for load, save, export and import data.
|
|
|
|
|
/// Manage the users of the application.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public DataManager DataMgr { get; private set; }
|
|
|
|
|
public IUserManager User { get; private set; }
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Constructors
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructor of the MasterManager.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dataManager">The serializer for the data.</param>
|
|
|
|
|
public MasterManager(IDataSerializer dataManager)
|
|
|
|
|
/// <param name="dataManager">The data manager.</param>
|
|
|
|
|
/// <param name="recipeManager">The recipes manager.</param>
|
|
|
|
|
/// <param name="userManager">The users manager.</param>
|
|
|
|
|
public MasterManager(IDataManager dataManager,
|
|
|
|
|
IRecipeManager recipeManager,
|
|
|
|
|
IUserManager userManager)
|
|
|
|
|
{
|
|
|
|
|
DataMgr = new DataManager(dataManager);
|
|
|
|
|
CurrentConnectedUser = null;
|
|
|
|
|
Recipes = DataMgr.GetRecipes("all recipes");
|
|
|
|
|
RecipesInSearch = DataMgr.GetRecipes("search on");
|
|
|
|
|
Users = DataMgr.GetUsers();
|
|
|
|
|
Data = dataManager;
|
|
|
|
|
Recipe = recipeManager;
|
|
|
|
|
User = userManager;
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Methods
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Log in an user. Test if the log in information are correct then connect the user.
|
|
|
|
|
/// Setup all the necessary parameters before start.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="mail">The user's mail</param>
|
|
|
|
|
/// <param name="password">The user's password.</param>
|
|
|
|
|
/// <returns>True if the user was correctly connected, false if there is something wrong.</returns>
|
|
|
|
|
/// <exception cref="ArgumentNullException"></exception>
|
|
|
|
|
public bool Login(string mail, string password)
|
|
|
|
|
public void Setup()
|
|
|
|
|
{
|
|
|
|
|
if (Users is null || Users.Count == 0)
|
|
|
|
|
throw new ArgumentNullException("There is no users registred.");
|
|
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
|
if (mail == "admin")
|
|
|
|
|
{
|
|
|
|
|
CurrentConnectedUser = Users.FirstOrDefault(u => u.Mail == "admin@mctg.fr");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
User? user = Users.Find(u => u.Mail == mail);
|
|
|
|
|
|
|
|
|
|
if (user is null || !user.psswMgr.VerifyPassword(user.Password, password))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
CurrentConnectedUser = user;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Log out an user.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>True if the user is correctly diconnected, false otherwise.</returns>
|
|
|
|
|
public bool Logout()
|
|
|
|
|
{
|
|
|
|
|
if (CurrentConnectedUser is null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
CurrentConnectedUser = null;
|
|
|
|
|
return true;
|
|
|
|
|
Data.LoadData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Register an user.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="newuser">The new user to add in the database.</param>
|
|
|
|
|
/// <returns>False if there is a problem with the registerement, true otherwise.</returns>
|
|
|
|
|
public bool Register(User newuser)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
User user = newuser;
|
|
|
|
|
DataMgr.Data[nameof(User)].Add(user);
|
|
|
|
|
Users = DataMgr.GetUsers();
|
|
|
|
|
}
|
|
|
|
|
catch (ArgumentException e)
|
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine(e.Message);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Add a recipe to the database.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="recipe">The recipe to add.</param>
|
|
|
|
|
public void AddRecipe(Recipe recipe)
|
|
|
|
|
{
|
|
|
|
|
DataMgr.Data[nameof(Recipe)].Add(recipe);
|
|
|
|
|
Recipes = DataMgr.GetRecipes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get the current connected user's personal recipes.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The current connected user's personal recipes.</returns>
|
|
|
|
|
public RecipeCollection GetCurrentUserRecipes()
|
|
|
|
|
=> new RecipeCollection("User recipes",
|
|
|
|
|
DataMgr.GetRecipes().ToList().FindAll(r => r.AuthorMail == CurrentConnectedUser?.Mail).ToArray());
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Notify property change handler.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="propertyName">the name of the property that change.</param>
|
|
|
|
|
public void OnPropertyChange([CallerMemberName] string propertyName = "")
|
|
|
|
|
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|