Skip to content

Commit

Permalink
Implement better GetSchedules endpoint, including course dates
Browse files Browse the repository at this point in the history
  • Loading branch information
EjPlatzer committed Oct 17, 2023
1 parent 8e7928b commit ea256bb
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 218 deletions.
99 changes: 6 additions & 93 deletions Gordon360/Controllers/ScheduleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -15,112 +16,24 @@ namespace Gordon360.Controllers
[Route("api/[controller]")]
public class ScheduleController : ControllerBase
{
private readonly CCTContext _context;

private readonly IScheduleService _scheduleService;

public ScheduleController(CCTContext context)
public ScheduleController(IScheduleService scheduleService)
{
_context = context;
_scheduleService = new ScheduleService(context);
_scheduleService = scheduleService;
}

/// <summary>
/// Gets all schedule objects for a user
/// </summary>
/// <returns>A IEnumerable of schedule objects</returns>
[HttpGet]
[Route("")]
public ActionResult<ScheduleViewModel> Get()
{
var authenticatedUserUsername = AuthUtils.GetUsername(User);
var groups = AuthUtils.GetGroups(User);

if (groups.Contains(AuthGroup.Student))
{
var result = _scheduleService.GetScheduleStudentAsync(authenticatedUserUsername);
if (result == null)
{
return NotFound();
}
return Ok(result);
}

else if (groups.Contains(AuthGroup.FacStaff))
{
var result = _scheduleService.GetScheduleFacultyAsync(authenticatedUserUsername);
if (result == null)
{
return NotFound();
}
return Ok(result);
}
else
{
return NotFound();
}
}

/// <summary>
/// Gets all schedule objects for a user
/// </summary>
/// <returns>A IEnumerable of schedule objects</returns>
[HttpGet]
[Route("{username}")]
public async Task<ActionResult<JArray>> GetAsync(string username, [FromQuery] string? sessionID)
{
//probably needs privacy stuff like ProfilesController and service
var viewerGroups = AuthUtils.GetGroups(User);


var authenticatedUserUsername = AuthUtils.GetUsername(User);

var groups = AuthUtils.GetGroups(username);

IEnumerable<ScheduleViewModel>? scheduleResult = null;

if (groups.Contains(AuthGroup.Student))
{
if (authenticatedUserUsername == username)
{
scheduleResult = await _scheduleService.GetScheduleStudentAsync(username, sessionID);

}
else if (viewerGroups.Contains(AuthGroup.Police) || viewerGroups.Contains(AuthGroup.SiteAdmin))
{
scheduleResult = await _scheduleService.GetScheduleStudentAsync(username, sessionID);

}
else if (viewerGroups.Contains(AuthGroup.Advisors))
{
scheduleResult = await _scheduleService.GetScheduleStudentAsync(username, sessionID);
}
}
else if (groups.Contains(AuthGroup.FacStaff))
{
scheduleResult = await _scheduleService.GetScheduleFacultyAsync(username, sessionID);
}
else
{
return NotFound();
}

JArray result = JArray.FromObject(scheduleResult);

foreach (JObject elem in result)
{
elem.Property("ID_NUM").Remove();
}

return Ok(result);
}
public async Task<ActionResult<IEnumerable<ScheduleViewModel>>> GetSchedulesAsync(string? username)
=> Ok(await _scheduleService.GetSchedulesAsync(username ?? AuthUtils.GetUsername(User)));

/// <summary>
/// Gets all session objects for a user
/// </summary>
/// <returns>A IEnumerable of session objects as well as the schedules</returns>
[HttpGet]
[Route("{username}/allcourses")]
[Obsolete("Use the basic get route instead")]
public async Task<ActionResult<CoursesBySessionViewModel>> GetAllCourses(string username)
{
IEnumerable<CoursesBySessionViewModel> result = await _scheduleService.GetAllCoursesAsync(username);
Expand Down
31 changes: 5 additions & 26 deletions Gordon360/Documentation/Gordon360.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gordon360/Gordon360.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.DirectoryServices.AccountManagement" Version="6.0.0" />
</ItemGroup>
<PropertyGroup>
Expand Down
72 changes: 72 additions & 0 deletions Gordon360/Models/ViewModels/ScheduleCourseViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using Gordon360.Models.CCT;
using System;
using System.Collections.Generic;

namespace Gordon360.Models.ViewModels;

public record ScheduleCourseViewModel
{
public string Code { get; set; }
public string Title { get; set; }
public string Role { get; set; }
public string? Location { get; set; }
public List<char> MeetingDays { get; set; }
public TimeOnly? BeginTime { get; set; }
public TimeOnly? EndTime { get; set; }
public DateOnly? BeginDate { get; set; }
public DateOnly? EndDate { get; set; }
public string YearTermCode { get; set; }


public static implicit operator ScheduleCourseViewModel(UserCourses course)
{
List<char> meetingDays = new();
if (course.MONDAY_CDE == "M")
{
meetingDays.Add('M');
}
if (course.TUESDAY_CDE == "T")
{
meetingDays.Add('T');
}
if (course.WEDNESDAY_CDE == "W")
{
meetingDays.Add('W');
}
if (course.THURSDAY_CDE == "R")
{
meetingDays.Add('R');
}
if (course.FRIDAY_CDE == "F")
{
meetingDays.Add('F');
}
if (course.SATURDAY_CDE == "S")
{
meetingDays.Add('S');
}
if (course.MONDAY_CDE == "U")
{
meetingDays.Add('U');
}

return new ScheduleCourseViewModel()
{
Code = course.CRS_CDE.Trim(),
Title = course.CRS_TITLE.Trim(),
Role = course.Role,
Location = (course.BLDG_CDE, course.ROOM_CDE) switch
{
(string building_code, string room_code) => $"{building_code} {room_code}",
(string building_code, null) => building_code,
_ => null
},
MeetingDays = meetingDays,
BeginTime = course.BEGIN_TIME is TimeSpan BeginTime ? TimeOnly.FromTimeSpan(BeginTime) : null,
EndTime = course.END_TIME is TimeSpan EndTime ? TimeOnly.FromTimeSpan(EndTime) : null,
BeginDate = course.BEGIN_DATE is DateTime BeginDate ? DateOnly.FromDateTime(BeginDate) : null,
EndDate = course.END_DATE is DateTime EndDate ? DateOnly.FromDateTime(EndDate) : null,
YearTermCode = course.YR_CDE + course.TRM_CDE,
};
}
}
29 changes: 3 additions & 26 deletions Gordon360/Models/ViewModels/ScheduleViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,6 @@
using System;
using System.Collections.Generic;

namespace Gordon360.Models.ViewModels
{
public class ScheduleViewModel
{
public int ID_NUM { get; set; }
public string CRS_CDE { get; set; }
public string CRS_TITLE { get; set; }
namespace Gordon360.Models.ViewModels;

public string BLDG_CDE { get; set; }

public string ROOM_CDE { get; set; }

public string MONDAY_CDE { get; set; }
public string TUESDAY_CDE { get; set; }
public string WEDNESDAY_CDE { get; set; }
public string THURSDAY_CDE { get; set; }
public string FRIDAY_CDE { get; set; }

public TimeSpan? BEGIN_TIME { get; set; }

public TimeSpan? END_TIME { get; set; }


}


}
public record ScheduleViewModel(SessionViewModel Session, IEnumerable<ScheduleCourseViewModel> Courses);
1 change: 1 addition & 0 deletions Gordon360/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
builder.Services.AddScoped<IEmailService, EmailService>();
builder.Services.AddScoped<INewsService, NewsService>();
builder.Services.AddScoped<ISessionService, SessionService>();
builder.Services.AddScoped<IScheduleService, ScheduleService>();
builder.Services.AddScoped<ServerUtils, ServerUtils>();
builder.Services.AddHostedService<EventCacheRefreshService>();
builder.Services.AddScoped<RecIM.IActivityService, RecIM.ActivityService>();
Expand Down
Loading

0 comments on commit ea256bb

Please sign in to comment.