/// <summary> /// Search for the allocation at a certain time for a certain pod. /// </summary> /// <param name="time">The time to search for in seconds since UNIX epoch.</param> /// <param name="pod">The pod to search for.</param> /// <returns>The allocation at the time for the pod if it exists, null otherwise.</returns> /// <exception cref="T:System.ArgumentException"> /// Thrown when the <see cref="T:HospitalAllocation.Data.Allocation.StaffGroups.TeamType"/> isn't a pod. /// </exception> public Pod GetPastPod(long time, Data.Allocation.StaffGroups.TeamType pod) { using (var context = new AllocationContext(_dbOptions)) { var allocations = context.TeamAllocations .Include(t => t.Positions) .ThenInclude(p => p.StaffPosition) .Where(p => p.Type == ModelConverter.TeamTypeToModel(pod)) .Where(p => time >= p.Time); var allocation = allocations.FirstOrDefault(p => p.Time == allocations.Max(q => q.Time)); if (allocation == null) { return(null); } else { foreach (var pos in allocation.Positions.Where(p => p.StaffPosition is KnownStaffPosition)) { context.Entry(pos.StaffPosition as KnownStaffPosition) .Reference(sp => sp.StaffMember) .Load(); } return(ModelConverter.PodFromModel(allocation)); } } }
/// <summary> /// Converts a <see cref="T:HospitalAllocation.Data.Allocation.StaffGroups.TeamType"/> /// to a <see cref="T:HospitalAllocation.Providers.Allocation.Database.Model.TeamType"/>. /// </summary> /// <param name="type">The type to convert.</param> /// <returns>The converted type.</returns> public static Model.TeamType TeamTypeToModel(Data.Allocation.StaffGroups.TeamType type) { switch (type) { case Data.Allocation.StaffGroups.TeamType.A: return(Model.TeamType.A); case Data.Allocation.StaffGroups.TeamType.B: return(Model.TeamType.B); case Data.Allocation.StaffGroups.TeamType.C: return(Model.TeamType.C); case Data.Allocation.StaffGroups.TeamType.D: return(Model.TeamType.D); case Data.Allocation.StaffGroups.TeamType.Senior: return(Model.TeamType.Senior); default: throw new ArgumentException(); } }
/// <summary> /// Commit changes made to the interface to the database. /// </summary> /// <param name="team">The team to commit.</param> public void Commit(Data.Allocation.StaffGroups.TeamType team) { using (var context = new AllocationContext(_dbOptions)) { var currentTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); var modelConverter = ModelConverter.FromDb(_dbOptions); var oldAllocations = context.TeamAllocations .Include(a => a.Positions) .Where(a => a.Type == ModelConverter.TeamTypeToModel(team)) .Where(a => a.Time == currentTime); // Determine the team to insert Pod pod; switch (team) { case Data.Allocation.StaffGroups.TeamType.A: pod = PodA.AsPod; break; case Data.Allocation.StaffGroups.TeamType.B: pod = PodB.AsPod; break; case Data.Allocation.StaffGroups.TeamType.C: pod = PodC.AsPod; break; case Data.Allocation.StaffGroups.TeamType.D: pod = PodD.AsPod; break; case Data.Allocation.StaffGroups.TeamType.Senior: pod = null; break; default: throw new ArgumentException("Unknown team type"); } // Insert a pod team if (pod != null) { InsertAllocation(context, currentTime, oldAllocations, modelConverter.PodToModel(pod)); // Check and update double for the staff var multipleAllocations = new Dictionary <int, int>(); multipleAllocations = pod .MultipleAllocation() .GroupBy(d => d.Key) .ToDictionary(d => d.Key, d => d.First().Value); foreach (int staffId in multipleAllocations.Keys) { StaffMember staffMember = context.StaffMembers. SingleOrDefault(staff => staff.StaffMemberId == staffId); staffMember.LastDouble = currentTime; } } // Insert a senior team else { InsertAllocation(context, currentTime, oldAllocations, modelConverter.SeniorTeamToModel(SeniorTeam.AsSeniorTeam)); } context.SaveChanges(); } }