transactions
This commit is contained in:
16
back/services/bussines/PhotoService/IPhotoService.cs
Normal file
16
back/services/bussines/PhotoService/IPhotoService.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using back.DataModels;
|
||||
using back.DTO;
|
||||
using DependencyInjector.Abstractions.ClassTypes;
|
||||
using DependencyInjector.Lifetimes;
|
||||
|
||||
namespace back.services.bussines.PhotoService;
|
||||
|
||||
public interface IPhotoService: IScoped
|
||||
{
|
||||
Task<Photo?> Create(PhotoFormModel form);
|
||||
Task Delete(string id, string userId = "00000000-0000-0000-0000-000000000001");
|
||||
Task<Photo?> Get(string id, string userId = "00000000-0000-0000-0000-000000000001");
|
||||
Task<(string? mediaType, byte[]? fileBytes)> GetBytes(string id, string res = "");
|
||||
Task<(int totalItems, IEnumerable<Photo>? pageData)> GetPage(int page, int pageSize);
|
||||
Task<Photo?> Update(Photo photo, string userId = "00000000-0000-0000-0000-000000000001");
|
||||
}
|
82
back/services/bussines/PhotoService/PhotoService.cs
Normal file
82
back/services/bussines/PhotoService/PhotoService.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user