From 2a9271bd29ad3e24129a033ba158c98bb65a6e18 Mon Sep 17 00:00:00 2001 From: Alexandre Agostinho Date: Thu, 1 Jun 2023 00:21:48 +0200 Subject: [PATCH] update MasterManager --- MCTG/Managers/RecipeDefaultManager.cs | 9 +- MCTG/Managers/UserDefaultManager.cs | 18 ++-- MCTG/Model/Managers/IRecipeManager.cs | 4 +- MCTG/Model/Managers/IUserManager.cs | 4 +- MCTG/Model/Managers/MasterManager.cs | 148 ++++---------------------- 5 files changed, 43 insertions(+), 140 deletions(-) diff --git a/MCTG/Managers/RecipeDefaultManager.cs b/MCTG/Managers/RecipeDefaultManager.cs index b5a7f1c..a1117dc 100644 --- a/MCTG/Managers/RecipeDefaultManager.cs +++ b/MCTG/Managers/RecipeDefaultManager.cs @@ -9,12 +9,17 @@ namespace Managers public class RecipeDefaultManager : IRecipeManager { private IDataManager _dataManager; + private Recipe _currentSelected; + + public Recipe? CurrentSelected => _currentSelected; + public RecipeDefaultManager(IDataManager dataManager) { _dataManager = dataManager; } + public bool AddRecipeToData(Recipe recipe) { var recipeList = _dataManager.Data[nameof(Recipe)]; @@ -82,12 +87,12 @@ namespace Managers $"Suggestions", recipes.ToArray()); } - public bool ModifyRecipeInData(Recipe oldRecipe, Recipe newRecipe) + public bool ModifyCurrentSelected(Recipe newRecipe) { try { var index = _dataManager.GetFromData().ToList() - .FindIndex(u => u.Equals(oldRecipe)); + .FindIndex(u => u.Equals(_currentSelected)); _dataManager.Data[nameof(Recipe)][index] = newRecipe; } catch (ArgumentNullException e) diff --git a/MCTG/Managers/UserDefaultManager.cs b/MCTG/Managers/UserDefaultManager.cs index 4d9e6c6..0afd8b4 100644 --- a/MCTG/Managers/UserDefaultManager.cs +++ b/MCTG/Managers/UserDefaultManager.cs @@ -15,7 +15,7 @@ namespace Managers { private IDataManager _dataManager; private IPasswordManager _passwordManager; - private User? _currentConnectedUser = null; + private User? _currentConnected = null; public UserDefaultManager(IDataManager dataManager, IPasswordManager passwordManager) @@ -25,7 +25,7 @@ namespace Managers } - public User? CurrentConnectedUser => _currentConnectedUser; + public User? CurrentConnected => _currentConnected; public IPasswordManager PasswordManager => _passwordManager; @@ -72,13 +72,13 @@ namespace Managers public bool LogIn(string mail, string password) { - if (CurrentConnectedUser is not null) + if (CurrentConnected is not null) throw new UserAlreadyConnectedException(); #if DEBUG if (mail == "admin") { - _currentConnectedUser = _dataManager.GetFromData() + _currentConnected = _dataManager.GetFromData() .FirstOrDefault(u => u.Mail == "admin@mctg.fr"); return true; } @@ -92,24 +92,24 @@ namespace Managers if (!_passwordManager.VerifyPassword(password, user.Password)) return false; - _currentConnectedUser = user; + _currentConnected = user; return true; } public void LogOut() { - if (CurrentConnectedUser is null) + if (CurrentConnected is null) throw new NoUserConnectedException(); - _currentConnectedUser = null; + _currentConnected = null; } - public bool ModifyUserInData(User oldUser, User newUser) + public bool ModifyCurrentConnected(User newUser) { try { var index = _dataManager.GetFromData().ToList() - .FindIndex(u => u.Equals(oldUser)); + .FindIndex(u => u.Equals(_currentConnected)); _dataManager.Data[nameof(User)][index] = newUser; } catch (ArgumentNullException e) diff --git a/MCTG/Model/Managers/IRecipeManager.cs b/MCTG/Model/Managers/IRecipeManager.cs index d24a916..2692854 100644 --- a/MCTG/Model/Managers/IRecipeManager.cs +++ b/MCTG/Model/Managers/IRecipeManager.cs @@ -8,12 +8,14 @@ namespace Model { public interface IRecipeManager { + Recipe? CurrentSelected { get; } + RecipeCollection GetAllRecipes(); Recipe GetRecipeFromId(int id); RecipeCollection SearchRecipeByTitle(string title); RecipeCollection SearchRecipeByAuthor(string authorMail); RecipeCollection GetRecipesByPriorityOrder(IEnumerable priority); bool AddRecipeToData(Recipe recipe); - bool ModifyRecipeInData(Recipe oldRecipe, Recipe newRecipe); + bool ModifyCurrentSelected(Recipe newRecipe); } } diff --git a/MCTG/Model/Managers/IUserManager.cs b/MCTG/Model/Managers/IUserManager.cs index 6260a64..5babaf4 100644 --- a/MCTG/Model/Managers/IUserManager.cs +++ b/MCTG/Model/Managers/IUserManager.cs @@ -8,14 +8,14 @@ namespace Model { public interface IUserManager { - User? CurrentConnectedUser { get; } + User? CurrentConnected { get; } IPasswordManager PasswordManager { get; } ICollection GetAllUsers(); User GetUserFromMail(string mail); User CreateUser(string? name, string? surname, string mail, string password, string? profilePict); bool AddUserToData(User user); - bool ModifyUserInData(User oldUser, User newUser); + bool ModifyCurrentConnected(User newUser); bool LogIn(string mail, string password); void LogOut(); } diff --git a/MCTG/Model/Managers/MasterManager.cs b/MCTG/Model/Managers/MasterManager.cs index 76e3402..551a6a1 100644 --- a/MCTG/Model/Managers/MasterManager.cs +++ b/MCTG/Model/Managers/MasterManager.cs @@ -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 { /// /// The Main manager of the model. /// - public class MasterManager : INotifyPropertyChanged + public class MasterManager { #region Attributes & Properties - public event PropertyChangedEventHandler? PropertyChanged; - - private RecipeCollection _recipesInSearch = new RecipeCollection(""); - - /// - /// The currently connected user. 'null' if no user is connected. - /// - public User? CurrentConnectedUser { get; private set; } - - public RecipeCollection RecipesInSearch - { - get => _recipesInSearch; - set - { - _recipesInSearch = value; - OnPropertyChange(); - } - } - /// - /// The collection of all recipes loaded. + /// Manage the data of the application. /// - public RecipeCollection Recipes { get; private set; } + public IDataManager Data { get; private set; } /// - /// The collection of all users loaded. + /// Manage the recipes of the application. /// - public List Users { get; private set; } + public IRecipeManager Recipe { get; private set; } /// - /// The data manager for load, save, export and import data. + /// Manage the users of the application. /// - public DataManager DataMgr { get; private set; } + public IUserManager User { get; private set; } #endregion #region Constructors /// /// Constructor of the MasterManager. /// - /// The serializer for the data. - public MasterManager(IDataSerializer dataManager) + /// The data manager. + /// The recipes manager. + /// The users manager. + 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 /// - /// Log in an user. Test if the log in information are correct then connect the user. + /// Setup all the necessary parameters before start. /// - /// The user's mail - /// The user's password. - /// True if the user was correctly connected, false if there is something wrong. - /// - 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; - } - - /// - /// Log out an user. - /// - /// True if the user is correctly diconnected, false otherwise. - public bool Logout() - { - if (CurrentConnectedUser is null) - return false; - - CurrentConnectedUser = null; - return true; + Data.LoadData(); } - - /// - /// Register an user. - /// - /// The new user to add in the database. - /// False if there is a problem with the registerement, true otherwise. - 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; - } - - /// - /// Add a recipe to the database. - /// - /// The recipe to add. - public void AddRecipe(Recipe recipe) - { - DataMgr.Data[nameof(Recipe)].Add(recipe); - Recipes = DataMgr.GetRecipes(); - } - - /// - /// Get the current connected user's personal recipes. - /// - /// The current connected user's personal recipes. - public RecipeCollection GetCurrentUserRecipes() - => new RecipeCollection("User recipes", - DataMgr.GetRecipes().ToList().FindAll(r => r.AuthorMail == CurrentConnectedUser?.Mail).ToArray()); - - /// - /// Notify property change handler. - /// - /// the name of the property that change. - public void OnPropertyChange([CallerMemberName] string propertyName = "") - => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + #endregion } - #endregion }