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:
2026-09-29 13:02:52 +05:30
parent bffc9b1686
commit 7d8a8478ff
39 changed files with 1596 additions and 678 deletions
+49 -3
View File
@@ -24,23 +24,31 @@ namespace Fitness_core_BE.Repos
public Task<Booking?> GetBookingById(Guid bookingId)
=> _context.Bookings
.Include(b => b.FacilitySession).ThenInclude(s => s.Facility)
.Include(b => b.FacilitySession)
.Include(b => b.Facility)
.Include(b => b.RfidTag)
.FirstOrDefaultAsync(b => b.BookingId == bookingId);
public Task<Booking?> GetActiveBookingByOtp(string otpCode)
=> _context.Bookings
.Include(b => b.FacilitySession).ThenInclude(s => s.Facility)
.Include(b => b.FacilitySession)
.Include(b => b.Facility)
.Include(b => b.RfidTag)
.FirstOrDefaultAsync(b => b.OtpCode == otpCode && b.Status == "Confirmed");
public Task<List<Booking>> GetBookingsForSession(Guid facilitySessionId)
=> _context.Bookings
.Include(b => b.Facility)
.Include(b => b.RfidTag)
.Where(b => b.FKFacilitySessionId == facilitySessionId)
.OrderBy(b => b.BookedAt)
.ToListAsync();
public Task<List<Booking>> GetBookingsForCustomer(Guid customerId)
=> _context.Bookings
.Include(b => b.FacilitySession).ThenInclude(s => s.Facility)
.Include(b => b.FacilitySession)
.Include(b => b.Facility)
.Include(b => b.RfidTag)
.Where(b => b.FKCustomerId == customerId)
.OrderByDescending(b => b.BookedAt)
.ToListAsync();
@@ -50,5 +58,43 @@ namespace Fitness_core_BE.Repos
b.FKFacilitySessionId == facilitySessionId &&
b.FKCustomerId == customerId &&
b.Status != "Cancelled");
public async Task<List<Booking>> GetBookings(Guid clientId, Guid? facilityId, Guid? facilitySessionId, DateTime? fromUtc, DateTime? toUtc, string? status)
{
var query = _context.Bookings
.Include(b => b.FacilitySession)
.Include(b => b.Facility)
.Include(b => b.RfidTag)
.Where(b => b.FKClientId == clientId);
if (facilityId.HasValue) query = query.Where(b => b.FKFacilityId == facilityId.Value);
if (facilitySessionId.HasValue) query = query.Where(b => b.FKFacilitySessionId == facilitySessionId.Value);
if (fromUtc.HasValue) query = query.Where(b => b.EndTime >= fromUtc.Value);
if (toUtc.HasValue) query = query.Where(b => b.StartTime <= toUtc.Value);
if (!string.IsNullOrEmpty(status)) query = query.Where(b => b.Status == status);
return await query.OrderByDescending(b => b.StartTime).ToListAsync();
}
public Task<int> MarkOverdueConfirmedAsNoShowAsync(Guid clientId, DateTime now)
=> _context.Bookings
.Where(b => b.FKClientId == clientId && b.Status == "Confirmed" && b.EndTime < now)
.ExecuteUpdateAsync(s => s.SetProperty(b => b.Status, "NoShow"));
public Task<List<Booking>> GetOverdueCheckedInBookingsAsync(Guid clientId, DateTime now)
=> _context.Bookings
.Include(b => b.RfidTag)
.Where(b => b.FKClientId == clientId && b.Status == "CheckedIn" && b.EndTime < now)
.ToListAsync();
public async Task<bool> TryClaimRfidTagAsync(Guid tagId, DateTime now)
{
var rows = await _context.RfidTags
.Where(t => t.RfidTagId == tagId && t.Status == "Available")
.ExecuteUpdateAsync(s => s
.SetProperty(t => t.Status, "InUse")
.SetProperty(t => t.UpdatedAt, now));
return rows > 0;
}
}
}
+119 -1
View File
@@ -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();
}
}
}
-143
View File
@@ -1,143 +0,0 @@
using Fitness_core_BE.Interfaces;
using Fitness_core_BE.Models;
using Microsoft.EntityFrameworkCore;
namespace Fitness_core_BE.Repos
{
public class PlaceRepo : IPlaceRepo
{
private readonly TenantDbContext _context;
public PlaceRepo(TenantDbContext context)
{
_context = context;
}
public async Task<List<Place>> GetAllPlaces(Guid clientId, bool includeInactive = false)
{
var query = _context.Places.Where(p => p.FKClientId == clientId);
if (!includeInactive)
query = query.Where(p => p.IsActive);
return await query.OrderBy(p => p.Name).ToListAsync();
}
public Task<Place?> GetPlaceById(Guid placeId)
=> _context.Places.FirstOrDefaultAsync(p => p.PlaceId == placeId);
public async Task<Place> CreatePlace(Place place)
{
place.PlaceId = Guid.NewGuid();
place.CreatedAt = DateTime.UtcNow;
place.IsActive = true;
_context.Places.Add(place);
return place;
}
public async Task<Place> UpdatePlace(Place place)
{
var existing = await _context.Places.FindAsync(place.PlaceId)
?? throw new KeyNotFoundException("Place not found");
existing.Name = place.Name;
existing.Description = place.Description;
existing.IsActive = place.IsActive;
return existing;
}
public async Task<Place?> SetPlaceActive(Guid placeId, Guid clientId, bool isActive)
{
var place = await _context.Places
.FirstOrDefaultAsync(p => p.PlaceId == placeId && p.FKClientId == clientId);
if (place == null) return null;
place.IsActive = isActive;
return place;
}
public Task<AccessDevice?> GetDeviceByIdForClientAsync(Guid deviceId, Guid clientId)
=> _context.AccessDevices
.FirstOrDefaultAsync(d => d.DeviceId == deviceId && d.FKClientId == clientId);
public async Task<Dictionary<Guid, string>> GetPlaceNamesByIdsAsync(List<Guid> placeIds)
{
return await _context.Places
.Where(p => placeIds.Contains(p.PlaceId))
.ToDictionaryAsync(p => p.PlaceId, p => p.Name);
}
public async Task<Dictionary<Guid, AccessDevice>> GetDevicesByPlaceIdsAsync(List<Guid> placeIds)
{
var devices = await _context.AccessDevices
.Where(d => d.FKPlaceId != null && placeIds.Contains(d.FKPlaceId.Value))
.ToListAsync();
return devices.ToDictionary(d => d.FKPlaceId!.Value, d => d);
}
public Task<bool> IsDeviceAssignedToAnotherPlaceAsync(Guid deviceId, Guid placeId)
=> _context.AccessDevices
.AnyAsync(d => d.DeviceId == deviceId && d.FKPlaceId != null && d.FKPlaceId != placeId);
public async Task AssignDeviceAsync(Guid placeId, Guid deviceId)
{
// A device can only sit in one place at a time -- clear anywhere else it's
// currently assigned before pointing it at this place.
var previouslyAssigned = await _context.AccessDevices
.Where(d => d.FKPlaceId == placeId && d.DeviceId != deviceId)
.ToListAsync();
foreach (var device in previouslyAssigned)
device.FKPlaceId = null;
var target = await _context.AccessDevices.FindAsync(deviceId)
?? throw new KeyNotFoundException("Device not found");
target.FKPlaceId = placeId;
}
public async Task UnassignDeviceAsync(Guid placeId)
{
var devices = await _context.AccessDevices
.Where(d => d.FKPlaceId == placeId)
.ToListAsync();
foreach (var device in devices)
device.FKPlaceId = null;
}
public async Task SetPlanPlacesAsync(Guid customerPlanId, List<Guid> placeIds)
{
var existing = await _context.CustomerPlanPlaces
.Where(pp => pp.FKCustomerPlanId == customerPlanId)
.ToListAsync();
_context.CustomerPlanPlaces.RemoveRange(existing);
foreach (var placeId in placeIds.Distinct())
{
_context.CustomerPlanPlaces.Add(new CustomerPlanPlace
{
CustomerPlanPlaceId = Guid.NewGuid(),
FKCustomerPlanId = customerPlanId,
FKPlaceId = placeId
});
}
}
public Task<List<Guid>> GetPlaceIdsForPlanAsync(Guid customerPlanId)
=> _context.CustomerPlanPlaces
.Where(pp => pp.FKCustomerPlanId == customerPlanId)
.Select(pp => pp.FKPlaceId)
.ToListAsync();
public async Task<Dictionary<Guid, List<Guid>>> GetPlaceIdsByPlanIdsAsync(List<Guid> customerPlanIds)
{
var rows = await _context.CustomerPlanPlaces
.Where(pp => customerPlanIds.Contains(pp.FKCustomerPlanId))
.ToListAsync();
return rows
.GroupBy(pp => pp.FKCustomerPlanId)
.ToDictionary(g => g.Key, g => g.Select(pp => pp.FKPlaceId).ToList());
}
}
}
+67
View File
@@ -0,0 +1,67 @@
using Fitness_core_BE.Interfaces;
using Fitness_core_BE.Models;
using Microsoft.EntityFrameworkCore;
namespace Fitness_core_BE.Repos
{
public class RfidTagRepository(TenantDbContext context) : IRfidTagRepository
{
private readonly TenantDbContext _context = context;
public async Task<List<RfidTag>> GetAllTags(Guid clientId, string? status = null)
{
var query = _context.RfidTags.Where(t => t.FKClientId == clientId);
if (!string.IsNullOrEmpty(status))
query = query.Where(t => t.Status == status);
return await query.OrderBy(t => t.TagCode).ToListAsync();
}
public Task<RfidTag?> GetTagById(Guid tagId, Guid clientId)
=> _context.RfidTags.FirstOrDefaultAsync(t => t.RfidTagId == tagId && t.FKClientId == clientId);
public Task<bool> TagCodeExistsAsync(Guid clientId, string tagCode, Guid? excludeTagId = null)
{
var query = _context.RfidTags.Where(t => t.FKClientId == clientId && t.TagCode == tagCode);
if (excludeTagId.HasValue)
query = query.Where(t => t.RfidTagId != excludeTagId.Value);
return query.AnyAsync();
}
public async Task<RfidTag> CreateTag(RfidTag tag)
{
tag.RfidTagId = Guid.NewGuid();
tag.Status = "Available";
tag.CreatedAt = DateTime.UtcNow;
tag.UpdatedAt = DateTime.UtcNow;
_context.RfidTags.Add(tag);
return tag;
}
public async Task<RfidTag> UpdateTag(RfidTag tag)
{
var existing = await _context.RfidTags.FindAsync(tag.RfidTagId)
?? throw new KeyNotFoundException("RFID tag not found");
existing.TagCode = tag.TagCode;
existing.Label = tag.Label;
existing.UpdatedAt = DateTime.UtcNow;
return existing;
}
public async Task<RfidTag?> SetStatusAsync(Guid tagId, Guid clientId, string status)
{
var tag = await _context.RfidTags.FirstOrDefaultAsync(t => t.RfidTagId == tagId && t.FKClientId == clientId);
if (tag == null) return null;
if (tag.Status == "InUse")
throw new ArgumentException("This tag is currently assigned to a booking -- it can only be freed via that booking's Complete action.");
tag.Status = status;
tag.UpdatedAt = DateTime.UtcNow;
return tag;
}
}
}