| | | 1 | | using Anichron.Core.Data; |
| | | 2 | | using Anichron.Core.Data.Repository; |
| | | 3 | | using Anichron.Core.Domain; |
| | | 4 | | |
| | | 5 | | namespace Anichron.API.Services; |
| | | 6 | | |
| | | 7 | | public interface IAdminStorageConfigService |
| | | 8 | | { |
| | | 9 | | Task<AuthResult<List<UserStorageConfig>>> GetByUserIdAsync(Guid userId, CancellationToken ct); |
| | | 10 | | Task<AuthResult<UserStorageConfig>> AddAsync(Guid userId, string rootPath, CancellationToken ct); |
| | | 11 | | Task<AuthResult> DeleteAsync(Guid userId, Guid configId, CancellationToken ct); |
| | | 12 | | } |
| | | 13 | | |
| | 10 | 14 | | public sealed class AdminStorageConfigService( |
| | 10 | 15 | | IUserRepository users, |
| | 10 | 16 | | IUserStorageConfigRepository storageConfigs, |
| | 10 | 17 | | IGuidFactory guidFactory, |
| | 10 | 18 | | IUnitOfWork unitOfWork) : IAdminStorageConfigService |
| | | 19 | | { |
| | | 20 | | public async Task<AuthResult<List<UserStorageConfig>>> GetByUserIdAsync(Guid userId, CancellationToken ct) |
| | 2 | 21 | | { |
| | 2 | 22 | | var user = await users.FindByIdAsync(userId, ct); |
| | 2 | 23 | | if (user is null) |
| | 1 | 24 | | return AuthResult.Fail<List<UserStorageConfig>>(AuthError.UserNotFound); |
| | | 25 | | |
| | 1 | 26 | | var configs = await storageConfigs.GetByUserIdAsync(userId, ct); |
| | 1 | 27 | | return AuthResult.Ok(configs); |
| | 2 | 28 | | } |
| | | 29 | | |
| | | 30 | | public async Task<AuthResult<UserStorageConfig>> AddAsync(Guid userId, string rootPath, CancellationToken ct) |
| | 5 | 31 | | { |
| | 5 | 32 | | ArgumentNullException.ThrowIfNull(rootPath); |
| | 5 | 33 | | if (string.IsNullOrWhiteSpace(rootPath)) |
| | 2 | 34 | | return AuthResult.Fail<UserStorageConfig>(AuthError.PathInvalid); |
| | | 35 | | |
| | 3 | 36 | | var user = await users.FindByIdAsync(userId, ct); |
| | 3 | 37 | | if (user is null) |
| | 1 | 38 | | return AuthResult.Fail<UserStorageConfig>(AuthError.UserNotFound); |
| | | 39 | | |
| | 2 | 40 | | var existing = await storageConfigs.FindByRootPathAsync(rootPath, ct); |
| | 2 | 41 | | if (existing is not null) |
| | 1 | 42 | | return AuthResult.Fail<UserStorageConfig>(AuthError.PathAlreadyAssigned); |
| | | 43 | | |
| | 1 | 44 | | var config = new UserStorageConfig |
| | 1 | 45 | | { |
| | 1 | 46 | | Id = guidFactory.NewGuid(), |
| | 1 | 47 | | UserId = userId, |
| | 1 | 48 | | RootPath = rootPath, |
| | 1 | 49 | | IsActive = true, |
| | 1 | 50 | | }; |
| | | 51 | | |
| | 1 | 52 | | storageConfigs.Add(config); |
| | 1 | 53 | | await unitOfWork.SaveChangesAsync(ct); |
| | 1 | 54 | | return AuthResult.Ok(config); |
| | 5 | 55 | | } |
| | | 56 | | |
| | | 57 | | public async Task<AuthResult> DeleteAsync(Guid userId, Guid configId, CancellationToken ct) |
| | 3 | 58 | | { |
| | 3 | 59 | | var config = await storageConfigs.FindByIdAsync(configId, ct); |
| | 3 | 60 | | if (config is null || config.UserId != userId) |
| | 2 | 61 | | return AuthResult.Fail(AuthError.StorageConfigNotFound); |
| | | 62 | | |
| | 1 | 63 | | storageConfigs.Remove(config); |
| | 1 | 64 | | await unitOfWork.SaveChangesAsync(ct); |
| | 1 | 65 | | return AuthResult.Ok(); |
| | 3 | 66 | | } |
| | | 67 | | } |