Files
mmorales.photo/back/services/bussines/PhotoService/PhotoService.cs
2025-08-24 14:18:20 +02:00

83 lines
2.8 KiB
C#

using back.DataModels;
using back.DTO;
using back.persistance.blob;
using back.persistance.data.repositories.Abstracts;
namespace back.services.bussines.PhotoService;
public class PhotoService(
IPhotoRepository photoRepository,
IUserRepository userRepository,
IBlobStorageService blobStorageService
) : IPhotoService
{
public async Task<Photo?> Create(PhotoFormModel form)
{
ArgumentNullException.ThrowIfNull(form);
if (form.Image == null || form.Image.Length == 0)
throw new ArgumentException("No image uploaded.", nameof(form));
//if (string.IsNullOrEmpty(form.UserId) || await userRepository.IsContentManager(form.UserId))
// throw new ArgumentException("Invalid user ID or user is not a content manager.", nameof(form.UserId));
throw new NotImplementedException();
}
public async Task Delete(string id, string userId = User.SystemUserId)
{
//if (string.IsNullOrEmpty(userId) || await userRepository.IsContentManager(userId))
// throw new ArgumentException("Invalid user ID or user is not a content manager.", nameof(userId));
photoRepository.Delete(id);
}
public async Task<Photo?> Get(string id, string userId = User.SystemUserId)
{
Photo? photo = await photoRepository.GetById(id);
return photo;
//return photo?.CanBeSeenBy(userId) ?? false
// ? photo
// : null;
}
public async Task<(string? mediaType, byte[]? fileBytes)> GetBytes(string id, string res = "")
{
var photo = await photoRepository.GetById(id);
if (photo == null)
return (null, null);
string filePath = res.ToLower() switch
{
"high" => photo.HighResUrl,
"mid" => photo.MidResUrl,
"low" or _ => photo.LowResUrl
};
string? mediaType = res.ToLower() switch
{
"high" => $"image/{photo.Extension}",
"mid" or "low" or _ => "image/webp",
};
return (
mediaType,
await blobStorageService.GetBytes(filePath) ?? throw new FileNotFoundException("File not found.", filePath)
);
}
public async Task<(int totalItems, IEnumerable<Photo>? pageData)> GetPage(int page, int pageSize)
{
return (
totalItems: await photoRepository.GetTotalItems(),
pageData: photoRepository.GetPage(page, pageSize)
);
}
public async Task<Photo?> Update(Photo photo, string userId = "00000000-0000-0000-0000-000000000001")
{
//if (string.IsNullOrEmpty(userId) || await userRepository.IsContentManager(userId))
// throw new ArgumentException("Invalid user ID or user is not a content manager.", nameof(userId));
return await photoRepository.Update(photo);
}
}