Refactoring some Code and clean Code

This commit is contained in:
franc
2025-04-09 15:37:44 +02:00
parent d248ccb1db
commit 127a60b621
19 changed files with 132 additions and 114 deletions
@@ -41,5 +41,12 @@
return category.Recipes;
}
public async Task<IEnumerable<Category>> GetAllCategoriesWithRecipesAsync()
{
return await _context.Categories
.Include(c => c.Recipes)
.ToListAsync();
}
}
}
@@ -13,5 +13,7 @@
Task<IEnumerable<Category>> GetAllCategoriesAsync();
Task<IEnumerable<Recipe>> GetRecipesByCategoryAsync(Guid categoryId);
Task<IEnumerable<Category>> GetAllCategoriesWithRecipesAsync();
}
}
@@ -4,7 +4,7 @@
using Francesco.Recipes.World.Models.BackendModels.Recipe;
using Microsoft.EntityFrameworkCore;
public class FavoritRepository : IFavoritRepository
public class FavoritRepository : IFavoriteRepository
{
private readonly FrancescosRecipesWorldDbContext _context;
@@ -2,7 +2,7 @@
{
using Francesco.Recipes.World.Models.BackendModels.Recipe;
public interface IFavoritRepository
public interface IFavoriteRepository
{
Task<IEnumerable<Recipe>> GetFavoriteRecipesAsync();
@@ -37,7 +37,6 @@ namespace Francesco.Recipes.World.Repositories.Ingredient
existingRecipeIngredient.Ingredient = recipeIngredient.Ingredient;
existingRecipeIngredient.Unit = recipeIngredient.Unit;
_context.RecipeIngredients.Update(existingRecipeIngredient);
await _context.SaveChangesAsync();
}
@@ -77,7 +76,6 @@ namespace Francesco.Recipes.World.Repositories.Ingredient
existingIngredient.Name = ingredient.Name;
_context.Ingredients.Update(existingIngredient);
await _context.SaveChangesAsync();
}
@@ -1,7 +1,6 @@
namespace Francesco.Recipes.World.Repositories.Instruction
{
using Francesco.Recipes.World.Models.BackendModels.Instruction;
using Francesco.Recipes.World.Models.BackendModels.Recipe;
public interface IInstructionRepository
{
@@ -11,6 +10,6 @@
Task<List<Instruction>> GetInstructionsByRecipeIdAsync(Guid recipeId);
Task RemoveInstructionFromRecipeAsync(Recipe recipe, Guid instructionId);
Task RemoveInstructionFromRecipeAsync(Guid recipeId, Guid instructionId);
}
}
@@ -2,7 +2,6 @@
{
using Francesco.Recipes.World.Data;
using Francesco.Recipes.World.Models.BackendModels.Instruction;
using Francesco.Recipes.World.Models.BackendModels.Recipe;
using Francesco.Recipes.World.Repositories.Recipe;
using Microsoft.EntityFrameworkCore;
@@ -25,19 +24,19 @@
public async Task<Instruction> CreateInstructionToRecipeAsync(Guid recipeId, string description, int number)
{
var recipe = await _recipeRepository.GetRecipeAsync(recipeId);
var recipe = await _recipeRepository.GetRecipeAsync(recipeId);
if (string.IsNullOrWhiteSpace(description))
if (string.IsNullOrWhiteSpace(description))
{
throw new ArgumentException("Description cannot be empty", nameof(description));
}
if (number <= 0)
if (number <= 0)
{
throw new ArgumentOutOfRangeException(nameof(number), "Number must be greater than 0.");
}
var newInstruction = new Instruction
var newInstruction = new Instruction
{
Id = Guid.NewGuid(),
Description = description,
@@ -45,24 +44,28 @@
Recipe = recipe,
};
_context.Instructions.Add(newInstruction);
await _context.SaveChangesAsync();
_context.Instructions.Add(newInstruction);
await _context.SaveChangesAsync();
return newInstruction;
return newInstruction;
}
public async Task RemoveInstructionFromRecipeAsync(Recipe recipe, Guid instructionId)
public async Task RemoveInstructionFromRecipeAsync(Guid recipeId, Guid instructionId)
{
if (recipe == null)
var recipe = await _recipeRepository.GetRecipeAsync(recipeId);
if (recipe.Instructions == null || !recipe.Instructions.Any())
{
throw new ArgumentNullException(nameof(recipe));
await _context.Entry(recipe)
.Collection(r => r.Instructions)
.LoadAsync();
}
var instructionToRemove = recipe.Instructions.FirstOrDefault(i => i.Id == instructionId);
var instructionToRemove = recipe.Instructions?.FirstOrDefault(i => i.Id == instructionId);
if (instructionToRemove != null)
{
recipe.Instructions.Remove(instructionToRemove);
recipe.Instructions?.Remove(instructionToRemove);
await _context.SaveChangesAsync();
}
}
@@ -2,7 +2,7 @@
{
public interface IMediaFileRepository
{
Task ReplaceInstructionImageAsync(Guid instructionId, Guid mediafileId, IFormFile? newPhoto);
Task ReplaceInstructionImageAsync(Guid instructionId, Guid mediaFileIdToReplace, string fileName, string mimeType, byte[] newMediaData);
Task UploadInstructionImageAsync(Guid instructionId, IFormFile? photo);
@@ -2,6 +2,7 @@
{
using Francesco.Recipes.World.Data;
using Francesco.Recipes.World.Models.BackendModels.MediaFile;
using Francesco.Recipes.World.Models.BackendModels.Recipe;
using Francesco.Recipes.World.Repositories.Instruction;
using Francesco.Recipes.World.Repositories.Recipe;
@@ -18,13 +19,8 @@
_recipeRepository = recipeRepository;
}
public async Task ReplaceInstructionImageAsync(Guid instructionId, Guid mediaFileIdToReplace, IFormFile? newPhoto)
public async Task ReplaceInstructionImageAsync(Guid instructionId, Guid mediaFileIdToReplace, string fileName, string mimeType, byte[] newMediaData)
{
if (newPhoto is null)
{
throw new ArgumentNullException(nameof(newPhoto));
}
var instruction = await _instructionRepository.GetInstructionAsync(instructionId);
var mediaToReplace = instruction.MediaFiles.FirstOrDefault(m => m.Id == mediaFileIdToReplace);
@@ -36,22 +32,17 @@
_context.MediaFiles.Remove(mediaToReplace);
await _context.SaveChangesAsync();
using (var memoryStream = new MemoryStream())
var newMedia = new MediaFile
{
await newPhoto.CopyToAsync(memoryStream);
Id = Guid.NewGuid(),
FileName = fileName,
MimeType = mimeType,
Data = newMediaData,
Instruction = instruction,
};
var newMedia = new MediaFile
{
Id = Guid.NewGuid(),
FileName = newPhoto.FileName,
MimeType = newPhoto.ContentType,
Data = memoryStream.ToArray(),
Instruction = instruction,
};
_context.MediaFiles.Add(newMedia);
await _context.SaveChangesAsync();
}
_context.MediaFiles.Add(newMedia);
await _context.SaveChangesAsync();
}
public async Task UploadInstructionImageAsync(Guid instructionId, IFormFile? photo)
@@ -89,11 +80,6 @@
var recipe = await _recipeRepository.GetRecipeAsync(recipeId);
if (recipe == null)
{
throw new InvalidOperationException("The specified recipe does not exist.");
}
var isImage = mediaFile.ContentType.StartsWith("image/");
var isVideo = mediaFile.ContentType.StartsWith("video/");
@@ -104,21 +90,11 @@
if (isImage)
{
var existingImage = recipe.MediaFiles?.FirstOrDefault(m => m.MimeType?.StartsWith("image/") == true);
if (existingImage != null)
{
_context.MediaFiles.Remove(existingImage);
await _context.SaveChangesAsync();
}
await RemoveExistingMediaAsync(recipe, "image/");
}
else if (isVideo)
{
var existingVideo = recipe.MediaFiles?.FirstOrDefault(m => m.MimeType?.StartsWith("video/") == true);
if (existingVideo != null)
{
_context.MediaFiles.Remove(existingVideo);
await _context.SaveChangesAsync();
}
await RemoveExistingMediaAsync(recipe, "video/");
}
using var memoryStream = new MemoryStream();
@@ -136,5 +112,15 @@
_context.MediaFiles.Add(newMedia);
await _context.SaveChangesAsync();
}
private async Task RemoveExistingMediaAsync(Recipe recipe, string mediaTypePrefix)
{
var existingMedia = recipe.MediaFiles?.FirstOrDefault(m => m.MimeType?.StartsWith(mediaTypePrefix) == true);
if (existingMedia != null)
{
_context.MediaFiles.Remove(existingMedia);
await _context.SaveChangesAsync();
}
}
}
}
@@ -13,7 +13,7 @@
Task RemoveIngredientFromRecipeAsync(Guid recipeId, Guid ingredientId);
Task<IEnumerable<Recipe>> GetRecipesByNameOrIngredientAsync(string name, string ingredient);
Task<IEnumerable<Recipe>> GetRecipesByNameAndIngredientAsync(string name, string ingredient);
Task<IReadOnlyCollection<Recipe>> GetRecipesByDifficultyAsync(Difficulty? difficulty);
@@ -37,6 +37,7 @@
.Include(r => r.RecipeIngredients)
.ThenInclude(ri => ri.Unit)
.Include(r => r.MediaFiles)
.Include(r => r.Instructions)
.FirstOrDefaultAsync(r => r.Id == recipeId);
}
@@ -53,7 +54,7 @@
var ingredients = await _ingredientRepository.GetIngredientsByNameAsync(ingredientName);
Ingredient ingredient;
if (ingredients == null || !ingredients.Any())
if (!ingredients.Any())
{
ingredient = new Ingredient
{
@@ -131,7 +132,6 @@
public async Task RemoveIngredientFromRecipeAsync(Guid recipeId, Guid ingredientId)
{
var recipe = await GetRecipeAsync(recipeId);
var recipeIngredient = await _context.RecipeIngredients
.FirstOrDefaultAsync(ri => ri.Recipe.Id == recipeId && ri.Ingredient.Id == ingredientId);
@@ -144,7 +144,7 @@
await _context.SaveChangesAsync();
}
public async Task<IEnumerable<Recipe>> GetRecipesByNameOrIngredientAsync(string name, string ingredient)
public async Task<IEnumerable<Recipe>> GetRecipesByNameAndIngredientAsync(string name, string ingredient)
{
var query = _context.Recipes
.Include(r => r.RecipeIngredients)
@@ -114,7 +114,6 @@
public async Task<IEnumerable<RecipeIngredientShoppingList>> GetIngredientsOfRecipeInListAsync(Guid shoppingListRecipeId)
{
return await _context.RecipeIngredientsShoppingLists
.AsNoTracking()
.Include(i => i.RecipeIngredient)
.ThenInclude(ri => ri.Ingredient)
.Include(i => i.RecipeIngredient.Unit)
@@ -168,7 +167,6 @@
public async Task<Recipe?> GetRecipeByNameAndImageAsync(string recipeName, string imageFileName)
{
return await _context.Recipes
.AsNoTracking()
.Where(r => r.Name == recipeName && r.MediaFiles.Any(mf => mf.FileName == imageFileName))
.Include(r => r.MediaFiles.Where(mf => mf.FileName == imageFileName))
.FirstOrDefaultAsync();