done UserDefaultManager + better structure on exception proj

pull/55/head
Alexandre AGOSTINHO 2 years ago
parent 17e8b0f648
commit 58855731da

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

@ -1,8 +1,7 @@
 
namespace AppException namespace AppException
{ {
[Serializable] public class RecipeNotFoundException : RecipeException
public class RecipeNotFoundException : Exception
{ {
public RecipeNotFoundException() : base("Recipe not found.") { } public RecipeNotFoundException() : base("Recipe not found.") { }
public RecipeNotFoundException(int id) : base($"Recipe id: '{id}'not found.") { } public RecipeNotFoundException(int id) : base($"Recipe id: '{id}'not found.") { }

@ -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.") { }
}
}

@ -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.") { }
}
}

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

@ -1,7 +1,6 @@
namespace AppException namespace AppException
{ {
[Serializable] public class UserNotFoundException : UserException
public class UserNotFoundException : Exception
{ {
public UserNotFoundException() : base("User not found.") { } public UserNotFoundException() : base("User not found.") { }
public UserNotFoundException(string userMail) : base($"User with mail: '{userMail}' not found.") { } public UserNotFoundException(string userMail) : base($"User with mail: '{userMail}' not found.") { }

@ -1,5 +1,6 @@
using AppException; using AppException;
using Model; using Model;
using System.Diagnostics;
using System.Reflection; using System.Reflection;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
@ -83,19 +84,16 @@ namespace Managers
public bool ModifyRecipeInData(Recipe oldRecipe, Recipe newRecipe) public bool ModifyRecipeInData(Recipe oldRecipe, Recipe newRecipe)
{ {
if (oldRecipe.Equals(newRecipe)) try
return false;
Recipe? recipe = _dataManager.GetFromData<Recipe>()
.ToList()
.Find(r => r.Equals(oldRecipe));
if (recipe is null)
throw new RecipeNotFoundException();
foreach (var property in typeof(Recipe).GetProperties()
.Where(prop => prop.CanWrite))
{ {
property.SetValue(oldRecipe, recipe); var index = _dataManager.GetFromData<Recipe>().ToList()
.FindIndex(u => u.Equals(oldRecipe));
_dataManager.Data[nameof(Recipe)][index] = newRecipe;
}
catch (ArgumentNullException e)
{
Debug.WriteLine("Recipe to modify not found.");
return false;
} }
return true; return true;

@ -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<User> GetAllUsers()
{
return _dataManager.GetFromData<User>();
}
public User GetUserFromMail(string mail)
{
User? user = _dataManager.GetFromData<User>().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<User>().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<User>().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;
}
}
}

@ -8,11 +8,13 @@ namespace Model
{ {
public interface IUserManager public interface IUserManager
{ {
User? CurrentConnectedUser { get; }
ICollection<User> GetAllUsers(); ICollection<User> GetAllUsers();
User GetUserFromMail(string mail); User GetUserFromMail(string mail);
bool AddUserToData(User user); bool AddUserToData(User user);
bool ModifyUserInData(User user); bool ModifyUserInData(User oldUser, User newUser);
bool LogIn(string mail, string password); bool LogIn(string mail, string password);
bool LogOut(); void LogOut();
} }
} }

Loading…
Cancel
Save