You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
162 lines
5.4 KiB
162 lines
5.4 KiB
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
|
|
{
|
|
/// <summary>
|
|
/// The Main manager of the model.
|
|
/// </summary>
|
|
public class MasterManager : INotifyPropertyChanged
|
|
{
|
|
#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.
|
|
/// </summary>
|
|
public RecipeCollection Recipes { get; private set; }
|
|
|
|
/// <summary>
|
|
/// The collection of all users loaded.
|
|
/// </summary>
|
|
public List<User> Users { get; private set; }
|
|
|
|
/// <summary>
|
|
/// The data manager for load, save, export and import data.
|
|
/// </summary>
|
|
public DataManager DataMgr { 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)
|
|
{
|
|
DataMgr = new DataManager(dataManager);
|
|
CurrentConnectedUser = null;
|
|
Recipes = DataMgr.GetRecipes("all recipes");
|
|
RecipesInSearch = DataMgr.GetRecipes("search on");
|
|
Users = DataMgr.GetUsers();
|
|
}
|
|
#endregion
|
|
|
|
#region Methods
|
|
/// <summary>
|
|
/// Log in an user. Test if the log in information are correct then connect the user.
|
|
/// </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)
|
|
{
|
|
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;
|
|
}
|
|
|
|
/// <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
|
|
}
|