using Data.SeaHavenIndustries; using Microsoft.EntityFrameworkCore; using SeaHaven.DataServices.Interfaces; namespace SeaHaven.DataServices.Implementation; public sealed class UserServiceAreaDataService : IUserServiceAreaDataService { private readonly ApplicationDbContext _context; public UserServiceAreaDataService(ApplicationDbContext context) { _context = context; } public async Task ReplaceAsync( string userId, IReadOnlyCollection areas, CancellationToken cancellationToken) { var existing = await _context.UserServiceAreas .Where(area => area.UserId == userId) .ToListAsync(cancellationToken); if (existing.Count > 0) _context.UserServiceAreas.RemoveRange(existing); await _context.UserServiceAreas.AddRangeAsync( areas.Select(area => new UserServiceArea { UserId = userId, Area = area }), cancellationToken); await _context.SaveChangesAsync(cancellationToken); } public async Task> GetForUserAsync( string userId, CancellationToken cancellationToken) { return await _context.UserServiceAreas .AsNoTracking() .Where(area => area.UserId == userId) .OrderBy(area => area.Area) .Select(area => area.Area) .ToListAsync(cancellationToken); } }