add repository logic for mediafile
This commit is contained in:
@@ -2,12 +2,10 @@
|
|||||||
{
|
{
|
||||||
public interface IMediaFileRepository
|
public interface IMediaFileRepository
|
||||||
{
|
{
|
||||||
Task ReplaceInstructionImageAsync(Guid instructionId, IFormFile? newPhoto);
|
Task ReplaceInstructionImageAsync(Guid instructionId, Guid mediafileId, IFormFile? newPhoto);
|
||||||
|
|
||||||
Task UploadInstructionImageAsync(Guid instructionId, IFormFile? photo);
|
Task UploadInstructionImageAsync(Guid instructionId, IFormFile? photo);
|
||||||
|
|
||||||
Task ReplaceRecipeImageAsync(Guid recipeId, IFormFile? newPhoto);
|
Task UploadRecipeMediaAsync(Guid recipeId, IFormFile mediaFile);
|
||||||
|
|
||||||
Task UploadRecipeImageAsync(Guid recipeId, IFormFile? photo);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@
|
|||||||
_recipeRepository = recipeRepository;
|
_recipeRepository = recipeRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task ReplaceInstructionImageAsync(Guid instructionId, IFormFile? newPhoto)
|
public async Task ReplaceInstructionImageAsync(Guid instructionId, Guid mediaFileIdToReplace, IFormFile? newPhoto)
|
||||||
{
|
{
|
||||||
if (newPhoto is null)
|
if (newPhoto is null)
|
||||||
{
|
{
|
||||||
@@ -26,24 +26,32 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
var instruction = await _instructionRepository.GetInstructionAsync(instructionId);
|
var instruction = await _instructionRepository.GetInstructionAsync(instructionId);
|
||||||
_context.RemoveRange(instruction.MediaFiles);
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
|
|
||||||
await UploadInstructionImageAsync(instructionId, newPhoto);
|
var mediaToReplace = instruction.MediaFiles.FirstOrDefault(m => m.Id == mediaFileIdToReplace);
|
||||||
}
|
if (mediaToReplace == null)
|
||||||
|
|
||||||
public async Task ReplaceRecipeImageAsync(Guid recipeId, IFormFile? newPhoto)
|
|
||||||
{
|
|
||||||
if (newPhoto is null)
|
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException(nameof(newPhoto));
|
throw new InvalidOperationException("The specified media file does not exist.");
|
||||||
}
|
}
|
||||||
|
|
||||||
var recipe = await _recipeRepository.GetRecipeAsync(recipeId);
|
_context.MediaFiles.Remove(mediaToReplace);
|
||||||
_context.RemoveRange(recipe.MediaFiles);
|
|
||||||
await _context.SaveChangesAsync();
|
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)
|
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);
|
var recipe = await _recipeRepository.GetRecipeAsync(recipeId);
|
||||||
|
|
||||||
using (var memoryStream = new MemoryStream())
|
if (recipe == null)
|
||||||
{
|
{
|
||||||
await photo.CopyToAsync(memoryStream);
|
throw new InvalidOperationException("The specified recipe does not exist.");
|
||||||
|
|
||||||
var recipeImage = new MediaFile
|
|
||||||
{
|
|
||||||
FileName = photo.FileName,
|
|
||||||
MimeType = photo.ContentType,
|
|
||||||
Data = memoryStream.ToArray(),
|
|
||||||
Recipe = recipe,
|
|
||||||
};
|
|
||||||
|
|
||||||
_context.Add(recipeImage);
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user