Refactor CustomerPlanService to use FacilityRepository, remove PlaceService, and introduce RfidTagService
- Updated CustomerPlanService to replace references to PlaceRepo with FacilityRepository for managing customer plans and associated facilities. - Removed PlaceService and its related methods, consolidating functionality into FacilityService. - Added RfidTagService for managing RFID tags, including methods for creating, updating, and setting statuses of tags. - Updated TenantDbContext to include new DbSet for RfidTags and CustomerPlanFacilities, and adjusted model configurations accordingly. - Enhanced FacilityService to support new booking modes and validation for facility creation and updates.
This commit is contained in:
+119
-1
@@ -41,9 +41,11 @@ namespace Fitness_core_BE.Repos
|
||||
?? throw new KeyNotFoundException("Facility not found");
|
||||
|
||||
existing.Name = facility.Name;
|
||||
existing.FacilityType = facility.FacilityType;
|
||||
existing.BookingMode = facility.BookingMode;
|
||||
existing.Description = facility.Description;
|
||||
existing.DefaultCapacity = facility.DefaultCapacity;
|
||||
existing.NumberOfUnits = facility.NumberOfUnits;
|
||||
existing.PricePerHour = facility.PricePerHour;
|
||||
existing.IsActive = facility.IsActive;
|
||||
|
||||
return existing;
|
||||
@@ -98,5 +100,121 @@ namespace Fitness_core_BE.Repos
|
||||
session.IsCancelled = true;
|
||||
return session;
|
||||
}
|
||||
|
||||
// --- Device binding (absorbed from PlaceRepo) ---
|
||||
|
||||
public Task<AccessDevice?> GetDeviceByIdForClientAsync(Guid deviceId, Guid clientId)
|
||||
=> _context.AccessDevices
|
||||
.FirstOrDefaultAsync(d => d.DeviceId == deviceId && d.FKClientId == clientId);
|
||||
|
||||
public async Task<Dictionary<Guid, string>> GetFacilityNamesByIdsAsync(List<Guid> facilityIds)
|
||||
{
|
||||
return await _context.Facilities
|
||||
.Where(f => facilityIds.Contains(f.FacilityId))
|
||||
.ToDictionaryAsync(f => f.FacilityId, f => f.Name);
|
||||
}
|
||||
|
||||
public async Task<Dictionary<Guid, AccessDevice>> GetDevicesByFacilityIdsAsync(List<Guid> facilityIds)
|
||||
{
|
||||
var devices = await _context.AccessDevices
|
||||
.Where(d => d.FKFacilityId != null && facilityIds.Contains(d.FKFacilityId.Value))
|
||||
.ToListAsync();
|
||||
|
||||
return devices.ToDictionary(d => d.FKFacilityId!.Value, d => d);
|
||||
}
|
||||
|
||||
public Task<bool> IsDeviceAssignedToAnotherFacilityAsync(Guid deviceId, Guid facilityId)
|
||||
=> _context.AccessDevices
|
||||
.AnyAsync(d => d.DeviceId == deviceId && d.FKFacilityId != null && d.FKFacilityId != facilityId);
|
||||
|
||||
public async Task AssignDeviceAsync(Guid facilityId, Guid deviceId)
|
||||
{
|
||||
// A device can only sit in one facility at a time -- clear anywhere else it's
|
||||
// currently assigned before pointing it at this facility.
|
||||
var previouslyAssigned = await _context.AccessDevices
|
||||
.Where(d => d.FKFacilityId == facilityId && d.DeviceId != deviceId)
|
||||
.ToListAsync();
|
||||
foreach (var device in previouslyAssigned)
|
||||
device.FKFacilityId = null;
|
||||
|
||||
var target = await _context.AccessDevices.FindAsync(deviceId)
|
||||
?? throw new KeyNotFoundException("Device not found");
|
||||
target.FKFacilityId = facilityId;
|
||||
}
|
||||
|
||||
public async Task UnassignDeviceAsync(Guid facilityId)
|
||||
{
|
||||
var devices = await _context.AccessDevices
|
||||
.Where(d => d.FKFacilityId == facilityId)
|
||||
.ToListAsync();
|
||||
foreach (var device in devices)
|
||||
device.FKFacilityId = null;
|
||||
}
|
||||
|
||||
// --- CustomerPlan access-scoping (absorbed from PlaceRepo) ---
|
||||
|
||||
public async Task SetPlanFacilitiesAsync(Guid customerPlanId, List<Guid> facilityIds)
|
||||
{
|
||||
var existing = await _context.CustomerPlanFacilities
|
||||
.Where(pf => pf.FKCustomerPlanId == customerPlanId)
|
||||
.ToListAsync();
|
||||
_context.CustomerPlanFacilities.RemoveRange(existing);
|
||||
|
||||
foreach (var facilityId in facilityIds.Distinct())
|
||||
{
|
||||
_context.CustomerPlanFacilities.Add(new CustomerPlanFacility
|
||||
{
|
||||
CustomerPlanFacilityId = Guid.NewGuid(),
|
||||
FKCustomerPlanId = customerPlanId,
|
||||
FKFacilityId = facilityId
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public Task<List<Guid>> GetFacilityIdsForPlanAsync(Guid customerPlanId)
|
||||
=> _context.CustomerPlanFacilities
|
||||
.Where(pf => pf.FKCustomerPlanId == customerPlanId)
|
||||
.Select(pf => pf.FKFacilityId)
|
||||
.ToListAsync();
|
||||
|
||||
public async Task<Dictionary<Guid, List<Guid>>> GetFacilityIdsByPlanIdsAsync(List<Guid> customerPlanIds)
|
||||
{
|
||||
var rows = await _context.CustomerPlanFacilities
|
||||
.Where(pf => customerPlanIds.Contains(pf.FKCustomerPlanId))
|
||||
.ToListAsync();
|
||||
|
||||
return rows
|
||||
.GroupBy(pf => pf.FKCustomerPlanId)
|
||||
.ToDictionary(g => g.Key, g => g.Select(pf => pf.FKFacilityId).ToList());
|
||||
}
|
||||
|
||||
// --- AdHoc booking support (new) ---
|
||||
|
||||
public Task<int> GetOverlappingAdhocBookingCountAsync(Guid facilityId, DateTime startTime, DateTime endTime, Guid? excludeBookingId = null)
|
||||
{
|
||||
var query = _context.Bookings.Where(b =>
|
||||
b.FKFacilityId == facilityId &&
|
||||
b.FKFacilitySessionId == null && // AdHoc bookings only
|
||||
b.Status != "Cancelled" && b.Status != "NoShow" && b.Status != "Completed" &&
|
||||
b.StartTime < endTime && b.EndTime > startTime);
|
||||
|
||||
if (excludeBookingId.HasValue)
|
||||
query = query.Where(b => b.BookingId != excludeBookingId.Value);
|
||||
|
||||
return query.CountAsync();
|
||||
}
|
||||
|
||||
public async Task LockFacilityForUpdateAsync(Guid facilityId)
|
||||
{
|
||||
// FromSqlInterpolated (not ExecuteSqlInterpolatedAsync, which is for non-query DML)
|
||||
// is the correct EF Core idiom for a locking SELECT -- it runs through the same ADO
|
||||
// connection/transaction as the caller's already-open _uow, so the row lock is held
|
||||
// until that transaction commits/rolls back. AsNoTracking avoids clashing with any
|
||||
// already-tracked Facility instance in this same context/request.
|
||||
await _context.Facilities
|
||||
.FromSqlInterpolated($"SELECT * FROM Facilities WHERE FacilityId = {facilityId} FOR UPDATE")
|
||||
.AsNoTracking()
|
||||
.ToListAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user