From 58855731da003dfa9f2b84df4ca939048746ea1f Mon Sep 17 00:00:00 2001 From: Alexandre Agostinho Date: Wed, 31 May 2023 21:49:18 +0200 Subject: [PATCH] done UserDefaultManager + better structure on exception proj --- .../RecipeExceptions/RecipeException.cs | 15 +++ .../RecipeNotFoundException.cs | 19 ++-- .../NoUserConnectedException.cs | 13 +++ .../UserAlreadyConnectedException.cs | 13 +++ .../Exception/UserExceptions/UserException.cs | 14 +++ .../UserNotFoundException.cs | 17 ++-- MCTG/Managers/RecipeDefaultManager.cs | 22 ++--- MCTG/Managers/UserDefaultManager.cs | 94 +++++++++++++++++++ MCTG/Model/Managers/IUserManager.cs | 6 +- 9 files changed, 180 insertions(+), 33 deletions(-) create mode 100644 MCTG/Exception/RecipeExceptions/RecipeException.cs rename MCTG/Exception/{ => RecipeExceptions}/RecipeNotFoundException.cs (71%) create mode 100644 MCTG/Exception/UserExceptions/NoUserConnectedException.cs create mode 100644 MCTG/Exception/UserExceptions/UserAlreadyConnectedException.cs create mode 100644 MCTG/Exception/UserExceptions/UserException.cs rename MCTG/Exception/{ => UserExceptions}/UserNotFoundException.cs (73%) create mode 100644 MCTG/Managers/UserDefaultManager.cs diff --git a/MCTG/Exception/RecipeExceptions/RecipeException.cs b/MCTG/Exception/RecipeExceptions/RecipeException.cs new file mode 100644 index 0000000..aba9d0b --- /dev/null +++ b/MCTG/Exception/RecipeExceptions/RecipeException.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection.Emit; +using System.Text; +using System.Threading.Tasks; + +namespace AppException +{ + public class RecipeException : Exception + { + public RecipeException() : base("Something went wrong with a recipe or a collection of recipe.") { } + public RecipeException(string message) : base(message) { } + } +} diff --git a/MCTG/Exception/RecipeNotFoundException.cs b/MCTG/Exception/RecipeExceptions/RecipeNotFoundException.cs similarity index 71% rename from MCTG/Exception/RecipeNotFoundException.cs rename to MCTG/Exception/RecipeExceptions/RecipeNotFoundException.cs index 6fe37c6..a6c3ee2 100644 --- a/MCTG/Exception/RecipeNotFoundException.cs +++ b/MCTG/Exception/RecipeExceptions/RecipeNotFoundException.cs @@ -1,10 +1,9 @@ - -namespace AppException -{ - [Serializable] - public class RecipeNotFoundException : Exception - { - public RecipeNotFoundException() : base("Recipe not found.") { } - public RecipeNotFoundException(int id) : base($"Recipe id: '{id}'not found.") { } - } -} + +namespace AppException +{ + public class RecipeNotFoundException : RecipeException + { + public RecipeNotFoundException() : base("Recipe not found.") { } + public RecipeNotFoundException(int id) : base($"Recipe id: '{id}'not found.") { } + } +} diff --git a/MCTG/Exception/UserExceptions/NoUserConnectedException.cs b/MCTG/Exception/UserExceptions/NoUserConnectedException.cs new file mode 100644 index 0000000..e0659fa --- /dev/null +++ b/MCTG/Exception/UserExceptions/NoUserConnectedException.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AppException +{ + public class NoUserConnectedException : UserException + { + public NoUserConnectedException() : base("No user is currently connected.") { } + } +} diff --git a/MCTG/Exception/UserExceptions/UserAlreadyConnectedException.cs b/MCTG/Exception/UserExceptions/UserAlreadyConnectedException.cs new file mode 100644 index 0000000..15b0ad6 --- /dev/null +++ b/MCTG/Exception/UserExceptions/UserAlreadyConnectedException.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AppException +{ + public class UserAlreadyConnectedException : UserException + { + public UserAlreadyConnectedException() : base("An user is already connected.") { } + } +} diff --git a/MCTG/Exception/UserExceptions/UserException.cs b/MCTG/Exception/UserExceptions/UserException.cs new file mode 100644 index 0000000..957e97a --- /dev/null +++ b/MCTG/Exception/UserExceptions/UserException.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AppException +{ + public class UserException : Exception + { + public UserException() : base("Somthing went wrong with an User.") { } + public UserException(string message) : base(message) { } + } +} diff --git a/MCTG/Exception/UserNotFoundException.cs b/MCTG/Exception/UserExceptions/UserNotFoundException.cs similarity index 73% rename from MCTG/Exception/UserNotFoundException.cs rename to MCTG/Exception/UserExceptions/UserNotFoundException.cs index 602482d..c858fac 100644 --- a/MCTG/Exception/UserNotFoundException.cs +++ b/MCTG/Exception/UserExceptions/UserNotFoundException.cs @@ -1,9 +1,8 @@ -namespace AppException -{ - [Serializable] - public class UserNotFoundException : Exception - { - public UserNotFoundException() : base("User not found.") { } - public UserNotFoundException(string userMail) : base($"User with mail: '{userMail}' not found.") { } - } -} +namespace AppException +{ + public class UserNotFoundException : UserException + { + public UserNotFoundException() : base("User not found.") { } + public UserNotFoundException(string userMail) : base($"User with mail: '{userMail}' not found.") { } + } +} diff --git a/MCTG/Managers/RecipeDefaultManager.cs b/MCTG/Managers/RecipeDefaultManager.cs index 825c376..7f4560c 100644 --- a/MCTG/Managers/RecipeDefaultManager.cs +++ b/MCTG/Managers/RecipeDefaultManager.cs @@ -1,5 +1,6 @@ using AppException; using Model; +using System.Diagnostics; using System.Reflection; using System.Runtime.CompilerServices; @@ -83,19 +84,16 @@ namespace Managers public bool ModifyRecipeInData(Recipe oldRecipe, Recipe newRecipe) { - if (oldRecipe.Equals(newRecipe)) - return false; - - Recipe? recipe = _dataManager.GetFromData() - .ToList() - .Find(r => r.Equals(oldRecipe)); - if (recipe is null) - throw new RecipeNotFoundException(); - - foreach (var property in typeof(Recipe).GetProperties() - .Where(prop => prop.CanWrite)) + try + { + var index = _dataManager.GetFromData().ToList() + .FindIndex(u => u.Equals(oldRecipe)); + _dataManager.Data[nameof(Recipe)][index] = newRecipe; + } + catch (ArgumentNullException e) { - property.SetValue(oldRecipe, recipe); + Debug.WriteLine("Recipe to modify not found."); + return false; } return true; diff --git a/MCTG/Managers/UserDefaultManager.cs b/MCTG/Managers/UserDefaultManager.cs new file mode 100644 index 0000000..cba0c0c --- /dev/null +++ b/MCTG/Managers/UserDefaultManager.cs @@ -0,0 +1,94 @@ +using AppException; +using Model; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Managers +{ + public class UserDefaultManager : IUserManager + { + private IDataManager _dataManager; + private IPasswordManager _passwordManager; + private User? _currentConnectedUser = null; + + public UserDefaultManager(IDataManager dataManager, IPasswordManager passwordManager) + { + _dataManager = dataManager; + _passwordManager = passwordManager; + } + + public User? CurrentConnectedUser => _currentConnectedUser; + + public bool AddUserToData(User user) + { + var userList = _dataManager.Data[nameof(User)]; + + if (userList.Exists(r => r.Equals(user))) + return false; + + _dataManager.Data[nameof(User)].Add(user); + return true; + } + + public ICollection GetAllUsers() + { + return _dataManager.GetFromData(); + } + + public User GetUserFromMail(string mail) + { + User? user = _dataManager.GetFromData().ToList() + .Find(u => u.Mail == mail); + if (user is null) + throw new UserNotFoundException(); + + return user; + } + + public bool LogIn(string mail, string password) + { + if (CurrentConnectedUser is not null) + throw new UserAlreadyConnectedException(); + + User? user = _dataManager.GetFromData().ToList() + .Find(u => u.Mail == mail); + if (user is null) + throw new UserNotFoundException(); + + if (!_passwordManager.VerifyPassword(password, user.Password)) + return false; + + _currentConnectedUser = user; + return true; + } + + public void LogOut() + { + if (CurrentConnectedUser is null) + throw new NoUserConnectedException(); + + _currentConnectedUser = null; + } + + public bool ModifyUserInData(User oldUser, User newUser) + { + try + { + var index = _dataManager.GetFromData().ToList() + .FindIndex(u => u.Equals(oldUser)); + _dataManager.Data[nameof(User)][index] = newUser; + } + catch (ArgumentNullException e) + { + Debug.WriteLine("User to modify not found."); + return false; + } + + return true; + } + } +} diff --git a/MCTG/Model/Managers/IUserManager.cs b/MCTG/Model/Managers/IUserManager.cs index bf5f04f..bc0ed3c 100644 --- a/MCTG/Model/Managers/IUserManager.cs +++ b/MCTG/Model/Managers/IUserManager.cs @@ -8,11 +8,13 @@ namespace Model { public interface IUserManager { + User? CurrentConnectedUser { get; } + ICollection GetAllUsers(); User GetUserFromMail(string mail); bool AddUserToData(User user); - bool ModifyUserInData(User user); + bool ModifyUserInData(User oldUser, User newUser); bool LogIn(string mail, string password); - bool LogOut(); + void LogOut(); } }