update MasterManager

pull/55/head
Alexandre AGOSTINHO 2 years ago
parent ca02846b35
commit 2a9271bd29

@ -9,12 +9,17 @@ namespace Managers
public class RecipeDefaultManager : IRecipeManager public class RecipeDefaultManager : IRecipeManager
{ {
private IDataManager _dataManager; private IDataManager _dataManager;
private Recipe _currentSelected;
public Recipe? CurrentSelected => _currentSelected;
public RecipeDefaultManager(IDataManager dataManager) public RecipeDefaultManager(IDataManager dataManager)
{ {
_dataManager = dataManager; _dataManager = dataManager;
} }
public bool AddRecipeToData(Recipe recipe) public bool AddRecipeToData(Recipe recipe)
{ {
var recipeList = _dataManager.Data[nameof(Recipe)]; var recipeList = _dataManager.Data[nameof(Recipe)];
@ -82,12 +87,12 @@ namespace Managers
$"Suggestions", recipes.ToArray()); $"Suggestions", recipes.ToArray());
} }
public bool ModifyRecipeInData(Recipe oldRecipe, Recipe newRecipe) public bool ModifyCurrentSelected(Recipe newRecipe)
{ {
try try
{ {
var index = _dataManager.GetFromData<Recipe>().ToList() var index = _dataManager.GetFromData<Recipe>().ToList()
.FindIndex(u => u.Equals(oldRecipe)); .FindIndex(u => u.Equals(_currentSelected));
_dataManager.Data[nameof(Recipe)][index] = newRecipe; _dataManager.Data[nameof(Recipe)][index] = newRecipe;
} }
catch (ArgumentNullException e) catch (ArgumentNullException e)

@ -15,7 +15,7 @@ namespace Managers
{ {
private IDataManager _dataManager; private IDataManager _dataManager;
private IPasswordManager _passwordManager; private IPasswordManager _passwordManager;
private User? _currentConnectedUser = null; private User? _currentConnected = null;
public UserDefaultManager(IDataManager dataManager, IPasswordManager passwordManager) public UserDefaultManager(IDataManager dataManager, IPasswordManager passwordManager)
@ -25,7 +25,7 @@ namespace Managers
} }
public User? CurrentConnectedUser => _currentConnectedUser; public User? CurrentConnected => _currentConnected;
public IPasswordManager PasswordManager => _passwordManager; public IPasswordManager PasswordManager => _passwordManager;
@ -72,13 +72,13 @@ namespace Managers
public bool LogIn(string mail, string password) public bool LogIn(string mail, string password)
{ {
if (CurrentConnectedUser is not null) if (CurrentConnected is not null)
throw new UserAlreadyConnectedException(); throw new UserAlreadyConnectedException();
#if DEBUG #if DEBUG
if (mail == "admin") if (mail == "admin")
{ {
_currentConnectedUser = _dataManager.GetFromData<User>() _currentConnected = _dataManager.GetFromData<User>()
.FirstOrDefault(u => u.Mail == "admin@mctg.fr"); .FirstOrDefault(u => u.Mail == "admin@mctg.fr");
return true; return true;
} }
@ -92,24 +92,24 @@ namespace Managers
if (!_passwordManager.VerifyPassword(password, user.Password)) if (!_passwordManager.VerifyPassword(password, user.Password))
return false; return false;
_currentConnectedUser = user; _currentConnected = user;
return true; return true;
} }
public void LogOut() public void LogOut()
{ {
if (CurrentConnectedUser is null) if (CurrentConnected is null)
throw new NoUserConnectedException(); throw new NoUserConnectedException();
_currentConnectedUser = null; _currentConnected = null;
} }
public bool ModifyUserInData(User oldUser, User newUser) public bool ModifyCurrentConnected(User newUser)
{ {
try try
{ {
var index = _dataManager.GetFromData<User>().ToList() var index = _dataManager.GetFromData<User>().ToList()
.FindIndex(u => u.Equals(oldUser)); .FindIndex(u => u.Equals(_currentConnected));
_dataManager.Data[nameof(User)][index] = newUser; _dataManager.Data[nameof(User)][index] = newUser;
} }
catch (ArgumentNullException e) catch (ArgumentNullException e)

@ -8,12 +8,14 @@ namespace Model
{ {
public interface IRecipeManager public interface IRecipeManager
{ {
Recipe? CurrentSelected { get; }
RecipeCollection GetAllRecipes(); RecipeCollection GetAllRecipes();
Recipe GetRecipeFromId(int id); Recipe GetRecipeFromId(int id);
RecipeCollection SearchRecipeByTitle(string title); RecipeCollection SearchRecipeByTitle(string title);
RecipeCollection SearchRecipeByAuthor(string authorMail); RecipeCollection SearchRecipeByAuthor(string authorMail);
RecipeCollection GetRecipesByPriorityOrder(IEnumerable<Priority> priority); RecipeCollection GetRecipesByPriorityOrder(IEnumerable<Priority> priority);
bool AddRecipeToData(Recipe recipe); bool AddRecipeToData(Recipe recipe);
bool ModifyRecipeInData(Recipe oldRecipe, Recipe newRecipe); bool ModifyCurrentSelected(Recipe newRecipe);
} }
} }

@ -8,14 +8,14 @@ namespace Model
{ {
public interface IUserManager public interface IUserManager
{ {
User? CurrentConnectedUser { get; } User? CurrentConnected { get; }
IPasswordManager PasswordManager { get; } IPasswordManager PasswordManager { get; }
ICollection<User> GetAllUsers(); ICollection<User> GetAllUsers();
User GetUserFromMail(string mail); User GetUserFromMail(string mail);
User CreateUser(string? name, string? surname, string mail, string password, string? profilePict); User CreateUser(string? name, string? surname, string mail, string password, string? profilePict);
bool AddUserToData(User user); bool AddUserToData(User user);
bool ModifyUserInData(User oldUser, User newUser); bool ModifyCurrentConnected(User newUser);
bool LogIn(string mail, string password); bool LogIn(string mail, string password);
void LogOut(); void LogOut();
} }

@ -1,161 +1,57 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; 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> /// <summary>
/// The Main manager of the model. /// The Main manager of the model.
/// </summary> /// </summary>
public class MasterManager : INotifyPropertyChanged public class MasterManager
{ {
#region Attributes & Properties #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> /// <summary>
/// The collection of all recipes loaded. /// Manage the data of the application.
/// </summary> /// </summary>
public RecipeCollection Recipes { get; private set; } public IDataManager Data { get; private set; }
/// <summary> /// <summary>
/// The collection of all users loaded. /// Manage the recipes of the application.
/// </summary> /// </summary>
public List<User> Users { get; private set; } public IRecipeManager Recipe { get; private set; }
/// <summary> /// <summary>
/// The data manager for load, save, export and import data. /// Manage the users of the application.
/// </summary> /// </summary>
public DataManager DataMgr { get; private set; } public IUserManager User { get; private set; }
#endregion #endregion
#region Constructors #region Constructors
/// <summary> /// <summary>
/// Constructor of the MasterManager. /// Constructor of the MasterManager.
/// </summary> /// </summary>
/// <param name="dataManager">The serializer for the data.</param> /// <param name="dataManager">The data manager.</param>
public MasterManager(IDataSerializer dataManager) /// <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); Data = dataManager;
CurrentConnectedUser = null; Recipe = recipeManager;
Recipes = DataMgr.GetRecipes("all recipes"); User = userManager;
RecipesInSearch = DataMgr.GetRecipes("search on");
Users = DataMgr.GetUsers();
} }
#endregion #endregion
#region Methods #region Methods
/// <summary> /// <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> /// </summary>
/// <param name="mail">The user's mail</param> public void Setup()
/// <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) Data.LoadData();
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;
} }
#endregion
/// <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
} }

Loading…
Cancel
Save