From 25b59888668d881c39606e28f4fb3d1125b737a2 Mon Sep 17 00:00:00 2001 From: Jason Asonye Date: Sun, 10 Nov 2024 02:23:43 -0500 Subject: [PATCH 1/2] wmr - Reworked GetResidentRA API route --- Gordon360/Controllers/HousingController.cs | 4 ++-- Gordon360/Services/HousingService.cs | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Gordon360/Controllers/HousingController.cs b/Gordon360/Controllers/HousingController.cs index 8b3d7ce01..ce7bc06d7 100644 --- a/Gordon360/Controllers/HousingController.cs +++ b/Gordon360/Controllers/HousingController.cs @@ -372,8 +372,8 @@ public async Task DeleteAssignment(int rangeId) /// The ID of the hall. /// The resident's room number. /// Returns the RA's ID if found, otherwise null. - [HttpGet("GetResidentRA")] - public async Task> GetResidentRA([FromQuery] string hallId, [FromQuery] string roomNumber) + [HttpGet("GetResidentRA/{hallId}/{roomNumber}")] + public async Task> GetResidentRA(string hallId, string roomNumber) { try { diff --git a/Gordon360/Services/HousingService.cs b/Gordon360/Services/HousingService.cs index ba6f162b8..54612868c 100644 --- a/Gordon360/Services/HousingService.cs +++ b/Gordon360/Services/HousingService.cs @@ -711,7 +711,18 @@ public async Task GetResidentRAAsync(string hallId, string roomNumber) throw new InvalidOperationException("No RA assigned to this room range."); } - return assignedRA; + // Use RA_ID to query RA_Assigned_Ranges_View for the RA’s name + var assignedRAName = await context.RA_Assigned_Ranges_View + .Where(raView => raView.RA_ID == assignedRA) + .Select(raView => raView.Fname + " " + raView.Lname) + .FirstOrDefaultAsync(); + + if (assignedRAName == null) + { + throw new InvalidOperationException("No RA name found for the RA ID."); + } + + return assignedRAName; } /// From de7f72b9ee96a9e7eb0fb99f93f73944da3109ad Mon Sep 17 00:00:00 2001 From: Jason Asonye Date: Tue, 12 Nov 2024 02:08:25 -0500 Subject: [PATCH 2/2] wmr - Fixed API call to return a RA_StudentsViewModel object --- Gordon360/Controllers/HousingController.cs | 8 ++--- Gordon360/Services/HousingService.cs | 38 ++++++++++++++-------- Gordon360/Services/ServiceInterfaces.cs | 2 +- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/Gordon360/Controllers/HousingController.cs b/Gordon360/Controllers/HousingController.cs index ce7bc06d7..1aca9af94 100644 --- a/Gordon360/Controllers/HousingController.cs +++ b/Gordon360/Controllers/HousingController.cs @@ -371,14 +371,14 @@ public async Task DeleteAssignment(int rangeId) /// /// The ID of the hall. /// The resident's room number. - /// Returns the RA's ID if found, otherwise null. + /// Returns the RA's details if found, otherwise null. [HttpGet("GetResidentRA/{hallId}/{roomNumber}")] - public async Task> GetResidentRA(string hallId, string roomNumber) + public async Task GetResidentRA([FromRoute] string hallId, [FromRoute] string roomNumber) { try { - var raId = await housingService.GetResidentRAAsync(hallId, roomNumber); - return Ok(raId); + var raInfo = await housingService.GetResidentRAAsync(hallId, roomNumber); + return Ok(raInfo); } catch (InvalidOperationException ex) { diff --git a/Gordon360/Services/HousingService.cs b/Gordon360/Services/HousingService.cs index 54612868c..0d8bfa2df 100644 --- a/Gordon360/Services/HousingService.cs +++ b/Gordon360/Services/HousingService.cs @@ -686,13 +686,13 @@ public async Task DeleteAssignmentAsync(int rangeId) /// /// The ID of the hall. /// The resident's room number. - /// Returns the RA's ID if found, otherwise null. - public async Task GetResidentRAAsync(string hallId, string roomNumber) + /// Returns the RA's details if found, otherwise throws an exception. + public async Task GetResidentRAAsync(string hallId, string roomNumber) { // Query the room range within the specified hall that contains the room number var roomRange = await context.Hall_Assignment_Ranges - .FirstOrDefaultAsync(r => r.Hall_ID == hallId - && string.Compare(r.Room_Start, roomNumber) <= 0 + .FirstOrDefaultAsync(r => r.Hall_ID == hallId + && string.Compare(r.Room_Start, roomNumber) <= 0 && string.Compare(r.Room_End, roomNumber) >= 0); if (roomRange == null) @@ -701,28 +701,38 @@ public async Task GetResidentRAAsync(string hallId, string roomNumber) } // Find the RA assigned to that room range - var assignedRA = await context.RA_Assigned_Ranges + var assignedRAID = await context.RA_Assigned_Ranges .Where(ra => ra.Range_ID == roomRange.Range_ID) .Select(ra => ra.Ra_ID) .FirstOrDefaultAsync(); - if (assignedRA == null) + if (assignedRAID == null) { throw new InvalidOperationException("No RA assigned to this room range."); } - // Use RA_ID to query RA_Assigned_Ranges_View for the RA’s name - var assignedRAName = await context.RA_Assigned_Ranges_View - .Where(raView => raView.RA_ID == assignedRA) - .Select(raView => raView.Fname + " " + raView.Lname) + // Get the full details of the RA assigned to the room range + var assignedRA = await context.RA_Students + .Where(ra => ra.ID == assignedRAID) + .Select(ra => new RA_StudentsViewModel + { + FirstName = ra.FirstName, + LastName = ra.LastName, + Dorm = ra.Dorm, + BLDG_Code = ra.BLDG_Code, + RoomNumber = ra.RoomNumber, + Email = ra.Email, + PhoneNumber = ra.PhoneNumber, + ID = ra.ID + }) .FirstOrDefaultAsync(); - if (assignedRAName == null) + if (assignedRA == null) { - throw new InvalidOperationException("No RA name found for the RA ID."); + throw new InvalidOperationException("RA details could not be retrieved."); } - - return assignedRAName; + + return assignedRA; } /// diff --git a/Gordon360/Services/ServiceInterfaces.cs b/Gordon360/Services/ServiceInterfaces.cs index f9050ec6b..d6afe97f3 100644 --- a/Gordon360/Services/ServiceInterfaces.cs +++ b/Gordon360/Services/ServiceInterfaces.cs @@ -230,7 +230,7 @@ public interface IHousingService Task DeleteRoomRangeAsync(int rangeId); Task AssignRaToRoomRangeAsync(int rangeId, string raId); Task DeleteAssignmentAsync(int rangeId); - Task GetResidentRAAsync(string hallId, string roomNumber); + Task GetResidentRAAsync(string hallId, string roomNumber); Task> GetAllRoomRangesAsync(); Task> GetAllRAsAsync(); Task> GetRangeAssignmentsAsync();