add exceptions and RecipeManager's progress to GetRecipesByPriorityOrder

pull/55/head
Alexandre AGOSTINHO 2 years ago
parent e3bf8f503a
commit bcfa543f28

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -0,0 +1,10 @@

namespace AppException
{
[Serializable]
public class RecipeNotFoundException : Exception
{
public RecipeNotFoundException() : base("Recipe not found.") { }
public RecipeNotFoundException(int id) : base($"Recipe id: '{id}'not found.") { }
}
}

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

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -0,0 +1,80 @@
using AppException;
using Model;
namespace Managers
{
public class RecipeDefaultManager : IRecipeManager
{
private IDataManager _dataManager;
public RecipeDefaultManager(IDataManager dataManager)
{
_dataManager = dataManager;
}
public bool AddRecipeToData(Recipe recipe)
{
var recipeList = _dataManager.Data[nameof(Recipe)];
if (recipeList.Exists(r => r.Equals(recipe)))
return false;
_dataManager.Data[nameof(Recipe)].Add(recipe);
return true;
}
public RecipeCollection GetAllRecipes()
{
return new RecipeCollection(
"All recipes",
_dataManager.GetFromData<Recipe>().ToArray());
}
public RecipeCollection GetRecipeByAuthor(string authorMail)
{
User? author = _dataManager.GetFromData<User>()
.ToList()
.Find(u => u.Mail == authorMail);
if (author is null)
throw new UserNotFoundException(authorMail);
return new RecipeCollection(
$"{author.Name} {author.Surname}'s recipes",
_dataManager.GetFromData<Recipe>().ToArray());
}
public RecipeCollection GetRecipeByTitle(string title)
{
IEnumerable<Recipe> recipes = from Recipe recipe in _dataManager.GetFromData<Recipe>()
where recipe.Title.Contains(title)
select recipe;
return new RecipeCollection(
$"Search for '{title}'", recipes.ToArray());
}
public Recipe GetRecipeFromId(int id)
{
Recipe? recipe = _dataManager.GetFromData<Recipe>()
.ToList()
.Find(r => r.Id == id);
if (recipe is null)
throw new RecipeNotFoundException();
return recipe;
}
public RecipeCollection GetRecipesByPriorityOrder(Priority priority)
{
IEnumerable<Recipe> recipes = from Recipe recipe in _dataManager.GetFromData<Recipe>()
where recipe.Title.Contains(title)
select recipe;
return new RecipeCollection(
$"Suggestions", recipes.ToArray());
}
public bool ModifyRecipeInData(Recipe recipe)
{
throw new NotImplementedException();
}
}
}

@ -1,17 +1,23 @@
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Model.Managers namespace Model
{ {
public interface IDataManager public interface IDataManager
{ {
IDataSerializer Serializer { get; }
Dictionary<string, List<object>> Data { get; }
void LoadData(); void LoadData();
void ReloadData(); void ReloadData();
void SaveData(); void SaveData();
void Import<T>(string pathOfTheFile) where T : class; void Import<T>(string pathOfTheFile) where T : class;
void Export<T>(T obj, string pathToExport) where T : class; void Export<T>(T obj, string pathToExport) where T : class;
ICollection<T> GetFromData<T>();
} }
} }

@ -4,7 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Model.Managers namespace Model
{ {
public interface IRecipeManager public interface IRecipeManager
{ {

@ -4,7 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Model.Managers namespace Model
{ {
public interface IUserManager public interface IUserManager
{ {

Loading…
Cancel
Save