| | | 1 | | using Anichron.Core.Data; |
| | | 2 | | using Anichron.Core.Data.Repository; |
| | | 3 | | using Anichron.Core.Domain; |
| | | 4 | | using Microsoft.EntityFrameworkCore; |
| | | 5 | | |
| | | 6 | | namespace Anichron.API.Services; |
| | | 7 | | |
| | | 8 | | public interface IAdminStorageConfigService |
| | | 9 | | { |
| | | 10 | | Task<List<UserStorageConfig>?> GetByUserIdAsync(Guid userId, CancellationToken ct); |
| | | 11 | | Task<ServiceResult<UserStorageConfig, IAddStorageConfigError>> AddAsync(Guid userId, string rootPath, CancellationTo |
| | | 12 | | Task<bool> DeleteAsync(Guid userId, Guid configId, CancellationToken ct); |
| | | 13 | | } |
| | | 14 | | |
| | 23 | 15 | | public sealed class AdminStorageConfigService( |
| | 23 | 16 | | IUserRepository users, |
| | 23 | 17 | | IUserStorageConfigRepository storageConfigs, |
| | 23 | 18 | | IGuidFactory guidFactory, |
| | 23 | 19 | | IUnitOfWork unitOfWork) : IAdminStorageConfigService |
| | | 20 | | { |
| | | 21 | | public async Task<List<UserStorageConfig>?> GetByUserIdAsync(Guid userId, CancellationToken ct) |
| | 2 | 22 | | { |
| | 2 | 23 | | var user = await users.FindByIdAsync(userId, ct); |
| | 2 | 24 | | if (user is null) |
| | 1 | 25 | | return null; |
| | | 26 | | |
| | 1 | 27 | | return await storageConfigs.GetByUserIdAsync(userId, ct); |
| | 2 | 28 | | } |
| | | 29 | | |
| | | 30 | | public async Task<ServiceResult<UserStorageConfig, IAddStorageConfigError>> AddAsync(Guid userId, string rootPath, C |
| | 18 | 31 | | { |
| | 18 | 32 | | ArgumentNullException.ThrowIfNull(rootPath); |
| | 17 | 33 | | var canonicalPath = StoragePath.Canonicalize(rootPath); |
| | 17 | 34 | | if (canonicalPath is null || !StoragePath.IsInsideOriginals(canonicalPath)) |
| | 7 | 35 | | return new PathInvalid(); |
| | | 36 | | |
| | 10 | 37 | | var user = await users.FindByIdAsync(userId, ct); |
| | 10 | 38 | | if (user is null) |
| | 1 | 39 | | return new UserNotFound(); |
| | | 40 | | |
| | 9 | 41 | | var existingRootPaths = await storageConfigs.GetAllRootPathsAsync(ct); |
| | 9 | 42 | | if (existingRootPaths.Contains(canonicalPath, StringComparer.Ordinal)) |
| | 2 | 43 | | return new PathAlreadyAssigned(); |
| | 7 | 44 | | if (StoragePath.FindOverlap(existingRootPaths, canonicalPath) is not null) |
| | 2 | 45 | | return new PathOverlaps(); |
| | | 46 | | |
| | 5 | 47 | | var config = new UserStorageConfig |
| | 5 | 48 | | { |
| | 5 | 49 | | Id = guidFactory.NewGuid(), |
| | 5 | 50 | | UserId = userId, |
| | 5 | 51 | | RootPath = canonicalPath, |
| | 5 | 52 | | IsActive = true, |
| | 5 | 53 | | }; |
| | | 54 | | |
| | 5 | 55 | | storageConfigs.Add(config); |
| | | 56 | | try |
| | 5 | 57 | | { |
| | 5 | 58 | | await unitOfWork.SaveChangesAsync(ct); |
| | 3 | 59 | | } |
| | 2 | 60 | | catch (DbUpdateException ex) when (ex.IsUniqueViolation(StorageConfigIndexNames.RootPathUnique)) |
| | 1 | 61 | | { |
| | 1 | 62 | | return new PathAlreadyAssigned(); |
| | | 63 | | } |
| | | 64 | | |
| | 3 | 65 | | return config; |
| | 16 | 66 | | } |
| | | 67 | | |
| | | 68 | | public async Task<bool> DeleteAsync(Guid userId, Guid configId, CancellationToken ct) |
| | 3 | 69 | | { |
| | 3 | 70 | | var config = await storageConfigs.FindByIdAsync(configId, ct); |
| | 3 | 71 | | if (config is null || config.UserId != userId) |
| | 2 | 72 | | return false; |
| | | 73 | | |
| | 1 | 74 | | storageConfigs.Remove(config); |
| | 1 | 75 | | await unitOfWork.SaveChangesAsync(ct); |
| | 1 | 76 | | return true; |
| | 3 | 77 | | } |
| | | 78 | | } |