update MasterManager

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

@ -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<Recipe>().ToList()
.FindIndex(u => u.Equals(oldRecipe));
.FindIndex(u => u.Equals(_currentSelected));
_dataManager.Data[nameof(Recipe)][index] = newRecipe;
}
catch (ArgumentNullException e)

@ -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<User>()
_currentConnected = _dataManager.GetFromData<User>()
.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<User>().ToList()
.FindIndex(u => u.Equals(oldUser));
.FindIndex(u => u.Equals(_currentConnected));
_dataManager.Data[nameof(User)][index] = newUser;
}
catch (ArgumentNullException e)

@ -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> priority);
bool AddRecipeToData(Recipe recipe);
bool ModifyRecipeInData(Recipe oldRecipe, Recipe newRecipe);
bool ModifyCurrentSelected(Recipe newRecipe);
}
}

@ -8,14 +8,14 @@ namespace Model
{
public interface IUserManager
{
User? CurrentConnectedUser { get; }
User? CurrentConnected { get; }
IPasswordManager PasswordManager { get; }
ICollection<User> 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();
}

@ -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
}

Loading…
Cancel
Save