Created
March 20, 2017 15:12
-
-
Save Kikimora/6aa04539af550367a812c84d687407fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Returns filtered list of events. | |
/// </summary> | |
/// <remarks>Returns filtered list of events according to filter and other parameters\n</remarks> | |
/// <param name="filter">A string to filter events by. Currently is filtering only event names using StartsWith criteria</param> | |
/// <param name="eventType">Type of the events to return (local/global). If omitted, return all events</param> | |
/// <param name="pageNumber">Zero-based page number</param> | |
/// <param name="recordsPerPage">Records per page</param> | |
/// <param name="sortField">Field to sort by</param> | |
/// <param name="sortOrder">Sorting order (asc/desc)</param> | |
/// <response code="200">An array of events wrapped in success response object</response> | |
/// <response code="500">Unexpected error</response> | |
[HttpGet] | |
[SwaggerOperation("GetEventsList")] | |
[SwaggerResponse(200, type: typeof(SuccessResponseWithArrayAndCount<MeetingEventDTO>))] | |
public IActionResult GetEventsList( | |
[FromQuery(Name = "filter")] string filter, | |
[FromQuery(Name = "event_type")] string eventType, | |
[FromQuery(Name = "page")] int pageNumber, | |
[FromQuery(Name = "per_page")] int recordsPerPage = 10, | |
[FromQuery(Name = "sort_field")] string sortField = "start_date", | |
[FromQuery(Name = "sort_order")] string sortOrder = "asc" | |
) | |
{ | |
if (string.IsNullOrWhiteSpace(sortOrder)) | |
sortOrder = ""; | |
if (!UserUid.HasValue) | |
return UnauthorizedWithCodeAndBody(401, ErrorResponse.GenerateInvalidUser("user_uid")); | |
if (UserSubscription == null) | |
return UnauthorizedWithCodeAndBody(402, ErrorResponse.GenerateSubscriptionExpired(null)); | |
if (UserSubscription.ExpiresAt < DateTime.Now) | |
return UnauthorizedWithCodeAndBody(402, ErrorResponse.GenerateSubscriptionExpired(UserSubscription.ExpiresAt)); | |
if (!string.IsNullOrWhiteSpace(sortField) && !SortDictionary.ContainsKey(sortField)) | |
return BadRequest(ErrorResponse.GenerateInvalidParameter(typeof(MeetingEvent), "", "Invalid sort field: " + sortField, "sort_field")); | |
if (filter == null) | |
filter = string.Empty; | |
Logger.LogInformation($@"Trying to get all events with names starting with '{filter}'"); | |
using (var db = GetDatabaseService()) | |
{ | |
var eventsWithCategoriesAndOptions = db.EventsTable.Include(eventObj => eventObj.AttendeeCategories).ThenInclude(categoryObj => categoryObj.Options); | |
var filteredMeetingEventsCount = ConstructRecordsListQuery(db.EventsTable, eventType, filter, 0, 0, null, true).Count(); | |
var eventsListQuery = ConstructRecordsListQuery(eventsWithCategoriesAndOptions, eventType, filter, pageNumber, recordsPerPage, SortDictionary[sortField], sortOrder.ToLower() != "desc"); | |
var meetingEventDtos = eventsListQuery.Select(MeetingEventDTO.From).ToList(); | |
var responseObj = new SuccessResponseWithArrayAndCount<MeetingEventDTO>() | |
{ | |
Data = meetingEventDtos, | |
TotalFilteredRecords = filteredMeetingEventsCount, | |
}; | |
return Ok(responseObj); | |
} | |
} | |
protected static IQueryable<MeetingEvent> ConstructRecordsListQuery( | |
IQueryable<MeetingEvent> dbTable, | |
string eventType, | |
string filter, | |
int pageNumber, | |
int pageSize, | |
string sortPropertyName, | |
bool isAscending | |
) | |
{ | |
IQueryable<MeetingEvent> queryMeetingEvents = dbTable; | |
if (!string.IsNullOrWhiteSpace(eventType)) | |
{ | |
queryMeetingEvents = queryMeetingEvents.Where(rec => rec.Type == eventType); | |
} | |
if (!string.IsNullOrWhiteSpace(filter)) | |
{ | |
queryMeetingEvents = queryMeetingEvents.Where(rec => rec.Name.StartsWith(filter)); | |
} | |
if (sortPropertyName != null) | |
{ | |
queryMeetingEvents = queryMeetingEvents.OrderBy(sortPropertyName, isAscending); | |
} | |
if (pageNumber > 0 && pageSize > 0) | |
queryMeetingEvents = queryMeetingEvents.Skip(pageNumber * pageSize); | |
if (pageSize > 0) | |
queryMeetingEvents = queryMeetingEvents.Take(pageSize); | |
return queryMeetingEvents; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment