add repository logic for mediafile

This commit is contained in:
franc
2025-04-08 15:05:49 +02:00
parent d4bcf7980b
commit 31fbafe806
2 changed files with 70 additions and 33 deletions
@@ -2,12 +2,10 @@
{
public interface IMediaFileRepository
{
Task ReplaceInstructionImageAsync(Guid instructionId, IFormFile? newPhoto);
Task ReplaceInstructionImageAsync(Guid instructionId, Guid mediafileId, IFormFile? newPhoto);
Task UploadInstructionImageAsync(Guid instructionId, IFormFile? photo);
Task ReplaceRecipeImageAsync(Guid recipeId, IFormFile? newPhoto);
Task UploadRecipeImageAsync(Guid recipeId, IFormFile? photo);
Task UploadRecipeMediaAsync(Guid recipeId, IFormFile mediaFile);
}
}
@@ -18,7 +18,7 @@
_recipeRepository = recipeRepository;
}
public async Task ReplaceInstructionImageAsync(Guid instructionId, IFormFile? newPhoto)
public async Task ReplaceInstructionImageAsync(Guid instructionId, Guid mediaFileIdToReplace, IFormFile? newPhoto)
{
if (newPhoto is null)
{
@@ -26,24 +26,32 @@
}
var instruction = await _instructionRepository.GetInstructionAsync(instructionId);
_context.RemoveRange(instruction.MediaFiles);
await _context.SaveChangesAsync();
await UploadInstructionImageAsync(instructionId, newPhoto);
}
public async Task ReplaceRecipeImageAsync(Guid recipeId, IFormFile? newPhoto)
{
if (newPhoto is null)
var mediaToReplace = instruction.MediaFiles.FirstOrDefault(m => m.Id == mediaFileIdToReplace);
if (mediaToReplace == null)
{
throw new ArgumentNullException(nameof(newPhoto));
throw new InvalidOperationException("The specified media file does not exist.");
}
var recipe = await _recipeRepository.GetRecipeAsync(recipeId);
_context.RemoveRange(recipe.MediaFiles);
_context.MediaFiles.Remove(mediaToReplace);
await _context.SaveChangesAsync();
await UploadInstructionImageAsync(recipeId, newPhoto);
using (var memoryStream = new MemoryStream())
{
await newPhoto.CopyToAsync(memoryStream);
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();
}
}
public async Task UploadInstructionImageAsync(Guid instructionId, IFormFile? photo)
@@ -72,30 +80,61 @@
}
}
public async Task UploadRecipeImageAsync(Guid recipeId, IFormFile? photo)
public async Task UploadRecipeMediaAsync(Guid recipeId, IFormFile mediaFile)
{
if (photo is null)
if (mediaFile == null)
{
throw new ArgumentNullException(nameof(photo));
throw new ArgumentNullException(nameof(mediaFile));
}
var recipe = await _recipeRepository.GetRecipeAsync(recipeId);
using (var memoryStream = new MemoryStream())
if (recipe == null)
{
await photo.CopyToAsync(memoryStream);
var recipeImage = new MediaFile
{
FileName = photo.FileName,
MimeType = photo.ContentType,
Data = memoryStream.ToArray(),
Recipe = recipe,
};
_context.Add(recipeImage);
await _context.SaveChangesAsync();
throw new InvalidOperationException("The specified recipe does not exist.");
}
var isImage = mediaFile.ContentType.StartsWith("image/");
var isVideo = mediaFile.ContentType.StartsWith("video/");
if (!isImage && !isVideo)
{
throw new InvalidOperationException("Only image or video files are allowed.");
}
if (isImage)
{
var existingImage = recipe.MediaFiles?.FirstOrDefault(m => m.MimeType?.StartsWith("image/") == true);
if (existingImage != null)
{
_context.MediaFiles.Remove(existingImage);
await _context.SaveChangesAsync();
}
}
else if (isVideo)
{
var existingVideo = recipe.MediaFiles?.FirstOrDefault(m => m.MimeType?.StartsWith("video/") == true);
if (existingVideo != null)
{
_context.MediaFiles.Remove(existingVideo);
await _context.SaveChangesAsync();
}
}
using var memoryStream = new MemoryStream();
await mediaFile.CopyToAsync(memoryStream);
var newMedia = new MediaFile
{
Id = Guid.NewGuid(),
FileName = mediaFile.FileName,
MimeType = mediaFile.ContentType,
Data = memoryStream.ToArray(),
Recipe = recipe,
};
_context.MediaFiles.Add(newMedia);
await _context.SaveChangesAsync();
}
}
}