Skip to content

Commit

Permalink
Merge pull request #1092 from gordon-cs/wmr-fetch-RA-info
Browse files Browse the repository at this point in the history
wmr - Reworked GetResidentRA API route
RossClark01 authored Nov 12, 2024
2 parents 2fd99f4 + de7f72b commit d9bf4c1
Showing 3 changed files with 33 additions and 12 deletions.
10 changes: 5 additions & 5 deletions Gordon360/Controllers/HousingController.cs
Original file line number Diff line number Diff line change
@@ -371,14 +371,14 @@ public async Task<IActionResult> DeleteAssignment(int rangeId)
/// </summary>
/// <param name="hallId">The ID of the hall.</param>
/// <param name="roomNumber">The resident's room number.</param>
/// <returns>Returns the RA's ID if found, otherwise null.</returns>
[HttpGet("GetResidentRA")]
public async Task<ActionResult<string>> GetResidentRA([FromQuery] string hallId, [FromQuery] string roomNumber)
/// <returns>Returns the RA's details if found, otherwise null. </returns>
[HttpGet("GetResidentRA/{hallId}/{roomNumber}")]
public async Task<IActionResult> 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)
{
33 changes: 27 additions & 6 deletions Gordon360/Services/HousingService.cs
Original file line number Diff line number Diff line change
@@ -686,13 +686,13 @@ public async Task<bool> DeleteAssignmentAsync(int rangeId)
/// </summary>
/// <param name="hallId">The ID of the hall.</param>
/// <param name="roomNumber">The resident's room number.</param>
/// <returns>Returns the RA's ID if found, otherwise null.</returns>
public async Task<string> GetResidentRAAsync(string hallId, string roomNumber)
/// <returns>Returns the RA's details if found, otherwise throws an exception.</returns>
public async Task<RA_StudentsViewModel> 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,16 +701,37 @@ public async Task<string> 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.");
}

// 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 (assignedRA == null)
{
throw new InvalidOperationException("RA details could not be retrieved.");
}

return assignedRA;
}

2 changes: 1 addition & 1 deletion Gordon360/Services/ServiceInterfaces.cs
Original file line number Diff line number Diff line change
@@ -230,7 +230,7 @@ public interface IHousingService
Task<bool> DeleteRoomRangeAsync(int rangeId);
Task<RA_Assigned_Ranges> AssignRaToRoomRangeAsync(int rangeId, string raId);
Task<bool> DeleteAssignmentAsync(int rangeId);
Task<string> GetResidentRAAsync(string hallId, string roomNumber);
Task<RA_StudentsViewModel> GetResidentRAAsync(string hallId, string roomNumber);
Task<List<HallAssignmentRangeViewModel>> GetAllRoomRangesAsync();
Task<List<RA_StudentsViewModel>> GetAllRAsAsync();
Task<List<RA_Assigned_RangesViewModel>> GetRangeAssignmentsAsync();

0 comments on commit d9bf4c1

Please sign in to comment.