public IEnumerable <AttendedEventViewModel> GetEventsForStudentByTerm(string user_name, string term) { var studentExists = _unitOfWork.AccountRepository.Where(x => x.AD_Username.Trim() == user_name.Trim()).Count() > 0; if (!studentExists) { throw new ResourceNotFoundException() { ExceptionMessage = "The Account was not found." }; } // Declare the variables used var idParam = new SqlParameter("@STU_USERNAME", user_name.Trim()); // Run the stored query and return an iterable list of objects var result = RawSqlQuery <ChapelEventViewModel> .query("EVENTS_BY_STUDENT_ID @STU_USERNAME", idParam); // Confirm that result is not empty if (result == null) { throw new ResourceNotFoundException() { ExceptionMessage = "The student was not found" }; } // This is the only real difference between the last method and this one // Filter out the events that are part of the specified term, based on the attribute specified, then sort by Date result = result.Where(x => x.CHTermCD.Trim().Equals(term)).OrderByDescending(x => x.CHDate); // A list to hold each combined event until we finish List <AttendedEventViewModel> list = new List <AttendedEventViewModel>(); // Create an empty event view model, to use just in case EventViewModel whoops = null; // Create an empty list of events, to use just in case IEnumerable <EventViewModel> events = null; // Get a list of every attended event, to send over to 25Live string joined = string.Join("+", result.Select(x => x.LiveID)); // Attempt to return all events attended by the student from 25Live // We use the cached data events = GetAllEvents(Data.AllEvents); // Loop through each event a student has attended and pull it's corresponding details from 25Live foreach (var c in result) { try { // Find the event with the same ID as the attended event EventViewModel l = events.ToList().Find(x => x.Event_ID == c.LiveID); whoops = l; } catch { // Ignore issue and continue to iterate } AttendedEventViewModel combine = new AttendedEventViewModel(whoops, c); // Add to the list we made earlier list.Add(combine); } // Declare an empty AttendedEventViewModel to return in the case of a problem IEnumerable <AttendedEventViewModel> vm = null; // In the database, the time and date are stored as separate datetime objects, here we combine them into one foreach (var v in list) { try { v.CHDate = v.CHDate.Value.Add(v.CHTime.Value.TimeOfDay); } catch (InvalidOperationException e) { // time value is null -- don't worry bout it System.Diagnostics.Debug.WriteLine(e.Message); } } // Attempt to convert the list to a ViewModel we can return try { vm = list.AsEnumerable <AttendedEventViewModel>(); } catch (Exception c) { // Do Nothing -- Let the Front End handle a return containing 0 Events System.Diagnostics.Debug.WriteLine(c.Message); } return(vm); }
public IEnumerable <AttendedEventViewModel> GetAllForStudent(string user_name) { // Confirm that student exists var studentExists = _unitOfWork.AccountRepository.Where(x => x.AD_Username.Trim() == user_name.Trim()).Count() > 0; if (!studentExists) { throw new ResourceNotFoundException() { ExceptionMessage = "The Account was not found." }; } // Declare the variables used var idParam = new SqlParameter("@STU_USERNAME", user_name.Trim()); // Run the query, which returns an iterable json list var result = RawSqlQuery <ChapelEventViewModel> .query("EVENTS_BY_STUDENT_ID @STU_USERNAME", idParam); if (result == null) { throw new ResourceNotFoundException() { ExceptionMessage = "No events attended yet!" }; } // A list to hold each combined event until we finish List <AttendedEventViewModel> list = new List <AttendedEventViewModel>(); // Create an empty event view model, to use just in case EventViewModel whoops = null; // Create an empty list of events, to use just in case IEnumerable <EventViewModel> events = null; // Get a list of every attended event, to send over to 25Live string joined = string.Join("+", result.Select(x => x.LiveID)); // Attempt to return all events attended by the student from 25Live events = GetAllEvents(Data.AllEvents); // Loop through each event a student has attended and pull it's corresponding details from 25Live foreach (var c in result) { try { // Find the event with the same ID as the attended event EventViewModel l = events.ToList().Find(x => x.Event_ID == c.LiveID); whoops = l; } catch { // Ignore issue and continue to iterate } // Bring the two together into an AttendedEventViewModel AttendedEventViewModel combine = new AttendedEventViewModel(whoops, c); // Add to the list we made earlier list.Add(combine); } // Declare an empty AttendedEventViewModel to return in the case of a problem IEnumerable <AttendedEventViewModel> vm = null; // In the database, the time and date are stored as separate datetime objects, here we combine them into one foreach (var v in list) { try { v.CHDate = v.CHDate.Value.Add(v.CHTime.Value.TimeOfDay); } catch (InvalidOperationException e) { System.Diagnostics.Debug.WriteLine(e.Message); // time value is null -- don't worry bout it } } // Attempt to convert the list to a ViewModel we can return try { vm = list.AsEnumerable <AttendedEventViewModel>(); } catch (Exception c) { // Note - 04/10/2020: Front end handles this, but we need to do something with the exception variable to suppress a warning // Do Nothing -- Let the Front End handle a return containing 0 Events System.Diagnostics.Debug.WriteLine(c.Message); } return(vm); }