Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a Private/Public Filter #141

Merged
merged 1 commit into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace DigitalFamilyCookbook.Data.Interfaces;

public interface IRecipeRepository
{
IEnumerable<Recipe> GetAll();
IEnumerable<Recipe> GetAll(bool includePrivate = true);

Recipe GetById(int recipeId);

Expand All @@ -14,19 +14,19 @@ public interface IRecipeRepository

Task DeleteRecipeImage(int recipeId);

IEnumerable<Recipe> GetRecipesForUser(string userAccountId);
IEnumerable<Recipe> GetRecipesForUser(string userAccountId, bool includePrivate);

(IEnumerable<Recipe> recipes, int totalRecipes) GetRecipesForUserPaginated(string userAcountId, int currentPage = 1, int recipesPerPage = 10);
(IEnumerable<Recipe> recipes, int totalRecipes) GetRecipesForUserPaginated(string userAcountId, bool includePrivate, int currentPage = 1, int recipesPerPage = 10);

IEnumerable<Recipe> GetRecipesForCategory(int categoryId);
IEnumerable<Recipe> GetRecipesForCategory(int categoryId, bool isLoggedIn);

(IEnumerable<Recipe> recipes, int totalRecipes) GetRecipesForCategoryPaginated(int categoryId, int currentPage = 1, int recipesPerPage = 10);
(IEnumerable<Recipe> recipes, int totalRecipes) GetRecipesForCategoryPaginated(int categoryId, bool includePrivate, int currentPage = 1, int recipesPerPage = 10);

IEnumerable<Recipe> GetRecipesForMeat(int meatId);
IEnumerable<Recipe> GetRecipesForMeat(int meatId, bool includePrivate);

(IEnumerable<Recipe> recipes, int totalRecipes) GetRecipesForMeatPaginated(int meatId, int currentPage = 1, int recipesPerPage = 10);
(IEnumerable<Recipe> recipes, int totalRecipes) GetRecipesForMeatPaginated(int meatId, bool includePrivate, int currentPage = 1, int recipesPerPage = 10);

(IEnumerable<Recipe> recipes, int totalRecipes) GetAllRecipesPaginated(int currentPage = 1, int recipesPerPage = 10);
(IEnumerable<Recipe> recipes, int totalRecipes) GetAllRecipesPaginated(bool includePrivate, int currentPage = 1, int recipesPerPage = 10);

Task MarkRecipeAsFavorite(string userAccountId, int recipeId);

Expand All @@ -36,11 +36,11 @@ public interface IRecipeRepository

(IEnumerable<Recipe> recipes, int totalRecipes) GetFavoriteRecipesForUserPaginated(string userAccountId, int currentPage = 1, int recipesPerPage = 10);

IEnumerable<Recipe> GetRecentRecipes(int count);
IEnumerable<Recipe> GetRecentRecipes(int count, bool includePrivate);

IEnumerable<Recipe> GetMostFavoritedRecipes(int count);
IEnumerable<Recipe> GetMostFavoritedRecipes(int count, bool includePrivate);

(IEnumerable<Recipe> recipes, int totalRecipes) SearchRecipesPaginated(string keywords, int currentPage = 1, int recipesPerPage = 10);
(IEnumerable<Recipe> recipes, int totalRecipes) SearchRecipesPaginated(string keywords, bool includePrivate, int currentPage = 1, int recipesPerPage = 10);

IEnumerable<Recipe> QuickSearchRecipes(string keywords, int count = 10);
IEnumerable<Recipe> QuickSearchRecipes(string keywords, bool includePrivate, int count = 10);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ public RecipeRepository(ApplicationDbContext db,
_stepRepository = stepRepository;
}

public IEnumerable<Recipe> GetAll()
public IEnumerable<Recipe> GetAll(bool includePrivate = true)
{
return _db.Recipes
.Where(r => r.IsPublic || includePrivate)
.Include(r => r.UserAccount)
.Select(r => Recipe.FromDto(r));
}
Expand Down Expand Up @@ -215,18 +216,18 @@ public async Task DeleteRecipeImage(int recipeId)
await _db.SaveChangesAsync();
}

public IEnumerable<Recipe> GetRecipesForUser(string userAccountId)
public IEnumerable<Recipe> GetRecipesForUser(string userAccountId, bool includePrivate)
{
return _db.Recipes
.Where(r => r.UserAccountId == userAccountId)
.Where(r => r.UserAccountId == userAccountId && (includePrivate || r.IsPublic))
.Include(r => r.UserAccount)
.Select(r => Recipe.FromDto(r));
}

public (IEnumerable<Recipe> recipes, int totalRecipes) GetRecipesForUserPaginated(string userAccountId, int currentPage = 1, int recipesPerPage = 10)
public (IEnumerable<Recipe> recipes, int totalRecipes) GetRecipesForUserPaginated(string userAccountId, bool includePrivate, int currentPage = 1, int recipesPerPage = 10)
{
var data = _db.Recipes
.Where(r => r.UserAccountId == userAccountId)
.Where(r => r.UserAccountId == userAccountId && (includePrivate || r.IsPublic))
.OrderBy(r => r.Name)
.Skip(currentPage == 1 ? 0 : (currentPage - 1) * recipesPerPage)
.Include(r => r.UserAccount)
Expand All @@ -247,18 +248,18 @@ public IEnumerable<Recipe> GetRecipesForUser(string userAccountId)
return (recipes, recipeCount);
}

public IEnumerable<Recipe> GetRecipesForCategory(int categoryId)
public IEnumerable<Recipe> GetRecipesForCategory(int categoryId, bool includePrivate)
{
return _db.Recipes
.Include(r => r.RecipeCategories)
.Where(r => r.RecipeCategories.Select(rc => rc.CategoryId).Contains(categoryId))
.Where(r => r.RecipeCategories.Select(rc => rc.CategoryId).Contains(categoryId) && (includePrivate || r.IsPublic))
.Select(r => Recipe.FromDto(r));
}

public (IEnumerable<Recipe> recipes, int totalRecipes) GetRecipesForCategoryPaginated(int categoryId, int currentPage = 1, int recipesPerPage = 10)
public (IEnumerable<Recipe> recipes, int totalRecipes) GetRecipesForCategoryPaginated(int categoryId, bool includePrivate, int currentPage = 1, int recipesPerPage = 10)
{
var data = _db.Recipes
.Where(r => r.RecipeCategories.Select(rc => rc.CategoryId).Contains(categoryId))
.Where(r => r.RecipeCategories.Select(rc => rc.CategoryId).Contains(categoryId) && (includePrivate || r.IsPublic))
.OrderBy(r => r.Name)
.Skip(currentPage == 1 ? 0 : (currentPage - 1) * recipesPerPage)
.Include(r => r.RecipeCategories)
Expand All @@ -281,18 +282,18 @@ public IEnumerable<Recipe> GetRecipesForCategory(int categoryId)
return (recipes, recipeCount);
}

public IEnumerable<Recipe> GetRecipesForMeat(int meatId)
public IEnumerable<Recipe> GetRecipesForMeat(int meatId, bool includePrivate)
{
return _db.Recipes
.Include(r => r.RecipeMeats)
.Where(r => r.RecipeMeats.Select(rm => rm.MeatId).Contains(meatId))
.Where(r => r.RecipeMeats.Select(rm => rm.MeatId).Contains(meatId) && (includePrivate || r.IsPublic))
.Select(r => Recipe.FromDto(r));
}

public (IEnumerable<Recipe> recipes, int totalRecipes) GetRecipesForMeatPaginated(int meatId, int currentPage = 1, int recipesPerPage = 10)
public (IEnumerable<Recipe> recipes, int totalRecipes) GetRecipesForMeatPaginated(int meatId, bool includePrivate, int currentPage = 1, int recipesPerPage = 10)
{
var data = _db.Recipes
.Where(r => r.RecipeMeats.Select(rc => rc.MeatId).Contains(meatId))
.Where(r => r.RecipeMeats.Select(rc => rc.MeatId).Contains(meatId) && (includePrivate || r.IsPublic))
.OrderBy(r => r.Name)
.Skip(currentPage == 1 ? 0 : (currentPage - 1) * recipesPerPage)
.Include(r => r.RecipeMeats)
Expand All @@ -315,11 +316,12 @@ public IEnumerable<Recipe> GetRecipesForMeat(int meatId)
return (recipes, recipeCount);
}

public (IEnumerable<Recipe> recipes, int totalRecipes) GetAllRecipesPaginated(int currentPage = 1, int recipesPerPage = 10)
public (IEnumerable<Recipe> recipes, int totalRecipes) GetAllRecipesPaginated(bool includePrivate, int currentPage = 1, int recipesPerPage = 10)
{
var allRecipes = _db.Recipes.Include(r => r.UserAccount);

var data = allRecipes
.Where(r => includePrivate || r.IsPublic)
.OrderBy(r => r.Name)
.Skip(currentPage == 1 ? 0 : (currentPage - 1) * recipesPerPage)
.Include(r => r.UserAccount)
Expand Down Expand Up @@ -403,9 +405,10 @@ public bool IsRecipeFavoriteForUser(string userAccountId, int recipeId)
return (recipes, recipeCount);
}

public IEnumerable<Recipe> GetRecentRecipes(int count)
public IEnumerable<Recipe> GetRecentRecipes(int count, bool includePrivate)
{
var data = _db.Recipes
.Where(r => includePrivate || r.IsPublic)
.OrderByDescending(r => r.DateCreated)
.Include(r => r.UserAccount)
.Include(r => r.RecipeFavorites)
Expand All @@ -425,9 +428,11 @@ public IEnumerable<Recipe> GetRecentRecipes(int count)
return recipes;
}

public IEnumerable<Recipe> GetMostFavoritedRecipes(int count)
public IEnumerable<Recipe> GetMostFavoritedRecipes(int count, bool includePrivate)
{
var favoriteRecipes = _db.RecipeFavorites
.Include(rf => rf.Recipe)
.Where(rf => rf.Recipe.IsPublic || includePrivate)
.ToList()
.GroupBy(rf => rf.RecipeId)
.Select(r => new RecipeGrouping() { RecipeId = r.Key, RecipeCount = r.Count() })
Expand Down Expand Up @@ -468,10 +473,10 @@ public IEnumerable<Recipe> GetMostFavoritedRecipes(int count)
return recipesOutput;
}

public (IEnumerable<Recipe> recipes, int totalRecipes) SearchRecipesPaginated(string keywords, int currentPage = 1, int recipesPerPage = 10)
public (IEnumerable<Recipe> recipes, int totalRecipes) SearchRecipesPaginated(string keywords, bool includePrivate, int currentPage = 1, int recipesPerPage = 10)
{
var data = _db.Recipes
.Where(r => r.Name.ToLower().Contains(keywords.ToLower()) || (r.Description ?? "").ToLower().Contains(keywords.ToLower()))
.Where(r => (includePrivate || r.IsPublic) && (r.Name.ToLower().Contains(keywords.ToLower()) || (r.Description ?? "").ToLower().Contains(keywords.ToLower())))
.OrderBy(r => r.Name)
.Skip(currentPage == 1 ? 0 : (currentPage - 1) * recipesPerPage)
.Include(r => r.UserAccount)
Expand All @@ -494,10 +499,10 @@ public IEnumerable<Recipe> GetMostFavoritedRecipes(int count)
return (recipes, recipeCount);
}

public IEnumerable<Recipe> QuickSearchRecipes(string keywords, int count = 10)
public IEnumerable<Recipe> QuickSearchRecipes(string keywords, bool includePrivate, int count = 10)
{
var recipes = _db.Recipes
.Where(r => r.Name.ToLower().Contains(keywords.ToLower()) || (r.Description ?? "").ToLower().Contains(keywords.ToLower()))
.Where(r => (includePrivate || r.IsPublic) && (r.Name.ToLower().Contains(keywords.ToLower()) || (r.Description ?? "").ToLower().Contains(keywords.ToLower())))
.OrderBy(r => r.Name)
.Take(count);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ public static UserAccountApiModel CurrentUser(this HttpContext context, bool thr
return UserAccountApiModel.None();
}

public static bool IsUserLoggedIn(this HttpContext context)
{
if (context.Items.ContainsKey("User"))
{
if (context.Items["User"] is not null)
{
var user = context.Items["User"] as UserAccountApiModel;

return user is not null;
}
}

return false;
}

public static string? GetAccessToken(this HttpContext context)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,28 @@ public class Handler : IRequestHandler<Query, OperationResult<RecipeListPageResu
{
private readonly IRecipeRepository _recipeRepository;
private readonly IFileService _fileService;
private readonly IHttpContextAccessor _httpContextAccessor;

public Handler(
IRecipeRepository recipeRepository,
IFileService fileService
IFileService fileService,
IHttpContextAccessor httpContextAccessor
)
{
_recipeRepository = recipeRepository;
_fileService = fileService;
_httpContextAccessor = httpContextAccessor;
}

public async Task<OperationResult<RecipeListPageResults>> Handle(Query request, CancellationToken cancellationToken)
{
try
{
var (data, totalRecipes) = await Task.FromResult(_recipeRepository.GetAllRecipesPaginated(request.PageNumber, request.RecipesPerPage));
var includePrivateRecipes = _httpContextAccessor.HttpContext?.IsUserLoggedIn() ?? false;
var (data, totalRecipes) = await Task.FromResult(_recipeRepository.GetAllRecipesPaginated(includePrivateRecipes, request.PageNumber, request.RecipesPerPage));

var recipes = data
.Select(r => RecipeApiModel.FromDomainModel(r))
.Select(RecipeApiModel.FromDomainModel)
.OrderBy(r => r.Name)
.ToList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,27 @@ public class Handler : IRequestHandler<Query, IReadOnlyCollection<RecipeApiModel
{
private readonly IRecipeRepository _recipeRepository;
private readonly IFileService _fileService;
private readonly IHttpContextAccessor _httpContextAccessor;

public Handler(
IRecipeRepository recipeRepository,
IFileService fileService
IFileService fileService,
IHttpContextAccessor httpContextAccessor
)
{
_recipeRepository = recipeRepository;
_fileService = fileService;
_httpContextAccessor = httpContextAccessor;
}

public async Task<IReadOnlyCollection<RecipeApiModel>> Handle(Query request, CancellationToken cancellationToken)
{
var data = await Task.FromResult(_recipeRepository.GetMostFavoritedRecipes(request.Count));
var includePrivateRecipes = _httpContextAccessor.HttpContext?.IsUserLoggedIn() ?? false;

var data = await Task.FromResult(_recipeRepository.GetMostFavoritedRecipes(request.Count, includePrivateRecipes));

var recipes = data
.Select(r => RecipeApiModel.FromDomainModel(r))
.Select(RecipeApiModel.FromDomainModel)
.ToList();

if (!request.IncludeImages)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ public class Handler : IRequestHandler<Query, IReadOnlyCollection<RecipeApiModel
{
private readonly IRecipeRepository _recipeRepository;
private readonly IFileService _fileService;
private readonly IHttpContextAccessor _httpContextAccessor;

public Handler(IRecipeRepository recipeRepository, IFileService fileService)
public Handler(IRecipeRepository recipeRepository, IFileService fileService, IHttpContextAccessor httpContextAccessor)
{
_recipeRepository = recipeRepository;
_fileService = fileService;
_httpContextAccessor = httpContextAccessor;
}

public async Task<IReadOnlyCollection<RecipeApiModel>> Handle(Query request, CancellationToken cancellationToken)
{
var data = await Task.FromResult(_recipeRepository.GetRecentRecipes(request.Count));
var includePrivateRecipes = _httpContextAccessor.HttpContext?.IsUserLoggedIn() ?? false;
var data = await Task.FromResult(_recipeRepository.GetRecentRecipes(request.Count, includePrivateRecipes));

var recipes = data
.Select(r => RecipeApiModel.FromDomainModel(r))
.Select(RecipeApiModel.FromDomainModel)
.OrderBy(r => r.Name)
.ToList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public async Task<OperationResult<RecipeApiModel>> Handle(Query request, Cancell
recipe.Categories = _categoryRepostory.GetForRecipe(recipe.RecipeId);
recipe.Meats = _meatRepository.GetForRecipe(recipe.RecipeId);

var user = _httpContextAccessor.HttpContext?.CurrentUser();
var user = _httpContextAccessor.HttpContext?.CurrentUser(false);

if (user is not null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,32 @@ public class Handler : IRequestHandler<Query, OperationResult<RecipeListPageResu
private readonly IRecipeRepository _recipeRepository;
private readonly ICategoryRepository _categoryRepository;
private readonly IFileService _fileService;
private readonly IHttpContextAccessor _httpContextAccessor;

public Handler(
IRecipeRepository recipeRepository,
ICategoryRepository categoryRepository,
IFileService fileService
IFileService fileService,
IHttpContextAccessor httpContextAccessor
)
{
_recipeRepository = recipeRepository;
_categoryRepository = categoryRepository;
_fileService = fileService;
_httpContextAccessor = httpContextAccessor;
}

public async Task<OperationResult<RecipeListPageResults>> Handle(Query request, CancellationToken cancellationToken)
{
try
{
var includePrivateRecipes = _httpContextAccessor.HttpContext?.IsUserLoggedIn() ?? false;
var category = _categoryRepository.Get(request.CategoryId);

var (data, totalRecipes) = await Task.FromResult(_recipeRepository.GetRecipesForCategoryPaginated(request.CategoryId, request.PageNumber, request.RecipesPerPage));
var (data, totalRecipes) = await Task.FromResult(_recipeRepository.GetRecipesForCategoryPaginated(request.CategoryId, includePrivateRecipes, request.PageNumber, request.RecipesPerPage));

var recipes = data
.Select(r => RecipeApiModel.FromDomainModel(r))
.Select(RecipeApiModel.FromDomainModel)
.OrderBy(r => r.Name)
.ToList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,30 @@ public class Handler : IRequestHandler<Query, OperationResult<RecipeListPageResu
private readonly IRecipeRepository _recipeRepository;
private readonly IMeatRepository _meatRepository;
private readonly IFileService _fileService;
private readonly IHttpContextAccessor _httpContextAccessor;

public Handler(IRecipeRepository recipeRepository,
IMeatRepository meatRepository,
IFileService fileService)
IFileService fileService,
IHttpContextAccessor httpContextAccessor)
{
_recipeRepository = recipeRepository;
_meatRepository = meatRepository;
_fileService = fileService;
_httpContextAccessor = httpContextAccessor;
}

public async Task<OperationResult<RecipeListPageResults>> Handle(Query request, CancellationToken cancellationToken)
{
try
{
var includePrivateRecipes = _httpContextAccessor.HttpContext?.IsUserLoggedIn() ?? false;
var meat = _meatRepository.Get(request.MeatId);

var (data, totalRecipes) = await Task.FromResult(_recipeRepository.GetRecipesForMeatPaginated(request.MeatId, request.PageNumber, request.RecipesPerPage));
var (data, totalRecipes) = await Task.FromResult(_recipeRepository.GetRecipesForMeatPaginated(request.MeatId, includePrivateRecipes, request.PageNumber, request.RecipesPerPage));

var recipes = data
.Select(r => RecipeApiModel.FromDomainModel(r))
.Select(RecipeApiModel.FromDomainModel)
.OrderBy(r => r.Name)
.ToList();

Expand Down
Loading