Files
Sharepoint-Toolbox/SharepointToolbox/Services/ProfileService.cs
Dev 9e850b07f2 feat(11-04): add UpdateProfileAsync to ProfileService and ImportLogoFromBytesAsync to BrandingService
- ProfileService.UpdateProfileAsync: replaces profile by name and persists the change
- IBrandingService: add ImportLogoFromBytesAsync to interface contract
- BrandingService.ImportLogoFromBytesAsync: validates magic bytes, compresses if > 512KB, returns LogoData
- BrandingService.ImportLogoAsync: refactored to delegate to ImportLogoFromBytesAsync
- ProfileServiceTests: 2 new tests (UpdateProfileAsync happy path + KeyNotFoundException)
- BrandingServiceTests: 2 new tests (ImportLogoFromBytesAsync valid PNG + invalid bytes)
- Tests.csproj: suppress NU1701 for pre-existing LiveCharts2/OpenTK transitive warnings
2026-04-08 14:34:11 +02:00

64 lines
2.2 KiB
C#

using SharepointToolbox.Core.Models;
using SharepointToolbox.Infrastructure.Persistence;
namespace SharepointToolbox.Services;
public class ProfileService
{
private readonly ProfileRepository _repository;
public ProfileService(ProfileRepository repository)
{
_repository = repository;
}
public Task<IReadOnlyList<TenantProfile>> GetProfilesAsync()
=> _repository.LoadAsync();
public async Task AddProfileAsync(TenantProfile profile)
{
if (string.IsNullOrWhiteSpace(profile.Name))
throw new ArgumentException("Profile name must not be empty.", nameof(profile));
if (string.IsNullOrWhiteSpace(profile.TenantUrl) ||
!Uri.TryCreate(profile.TenantUrl, UriKind.Absolute, out _))
throw new ArgumentException("TenantUrl must be a valid absolute URL.", nameof(profile));
if (string.IsNullOrWhiteSpace(profile.ClientId))
throw new ArgumentException("ClientId must not be empty.", nameof(profile));
var existing = (await _repository.LoadAsync()).ToList();
existing.Add(profile);
await _repository.SaveAsync(existing);
}
public async Task RenameProfileAsync(string existingName, string newName)
{
var profiles = (await _repository.LoadAsync()).ToList();
var target = profiles.FirstOrDefault(p => p.Name == existingName)
?? throw new KeyNotFoundException($"Profile '{existingName}' not found.");
target.Name = newName;
await _repository.SaveAsync(profiles);
}
public async Task DeleteProfileAsync(string name)
{
var profiles = (await _repository.LoadAsync()).ToList();
var target = profiles.FirstOrDefault(p => p.Name == name)
?? throw new KeyNotFoundException($"Profile '{name}' not found.");
profiles.Remove(target);
await _repository.SaveAsync(profiles);
}
public async Task UpdateProfileAsync(TenantProfile profile)
{
var profiles = (await _repository.LoadAsync()).ToList();
var idx = profiles.FindIndex(p => p.Name == profile.Name);
if (idx < 0) throw new KeyNotFoundException($"Profile '{profile.Name}' not found.");
profiles[idx] = profile;
await _repository.SaveAsync(profiles);
}
}