Skip to content

Commit

Permalink
[RSN-57] - created paging, filtering and sorting for events
Browse files Browse the repository at this point in the history
  • Loading branch information
rogacky11 committed Jun 23, 2024
1 parent ad1d209 commit 17afb81
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 2 deletions.
38 changes: 37 additions & 1 deletion Server/ReasnAPI/ReasnAPI/Controllers/EventsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,5 +299,41 @@ public IActionResult DeleteEventsTag([FromRoute] int tagId)
{
tagService.DeleteTag(tagId);
return NoContent();
}
}
[HttpGet]
[Route("api/EventsPaging")]
public async Task<ActionResult<PagedResponse<EventDto>>> GetPagedEvents(

Check warning on line 305 in Server/ReasnAPI/ReasnAPI/Controllers/EventsController.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (ubuntu-latest)

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 305 in Server/ReasnAPI/ReasnAPI/Controllers/EventsController.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (macos-latest)

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 305 in Server/ReasnAPI/ReasnAPI/Controllers/EventsController.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (windows-latest)

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
[FromQuery] string? filterName,
[FromQuery] EventStatus? filterStatus,
[FromQuery] List<string>? filterTags,
[FromQuery] DateTime? filterStartAt,
[FromQuery] DateTime? filterEndAt,
[FromQuery] int offset = 0,
[FromQuery] int limit = 10,
[FromQuery] SortBy sortBy = SortBy.StartAt,
[FromQuery] SortOrder sortOrder = SortOrder.Ascending)
{
try
{
var request = new PagedRequest
{
FilterName = filterName,

Check warning on line 320 in Server/ReasnAPI/ReasnAPI/Controllers/EventsController.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (ubuntu-latest)

Possible null reference assignment.

Check warning on line 320 in Server/ReasnAPI/ReasnAPI/Controllers/EventsController.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (macos-latest)

Possible null reference assignment.

Check warning on line 320 in Server/ReasnAPI/ReasnAPI/Controllers/EventsController.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (windows-latest)

Possible null reference assignment.
FilterStatus = filterStatus,
FilterTags = filterTags,

Check warning on line 322 in Server/ReasnAPI/ReasnAPI/Controllers/EventsController.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (ubuntu-latest)

Possible null reference assignment.

Check warning on line 322 in Server/ReasnAPI/ReasnAPI/Controllers/EventsController.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (macos-latest)

Possible null reference assignment.

Check warning on line 322 in Server/ReasnAPI/ReasnAPI/Controllers/EventsController.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (windows-latest)

Possible null reference assignment.
FilterStartAt = filterStartAt,
FilterEndAt = filterEndAt,
Offset = offset,
Limit = limit,
SortBy = sortBy,
SortOrder = sortOrder
};

var eventsPaged = eventService.GetPagedEvents(request);
return Ok(eventsPaged);
}
catch (Exception ex)
{
return StatusCode(500, $"Internal server error: {ex.Message}");
}
}
}
31 changes: 31 additions & 0 deletions Server/ReasnAPI/ReasnAPI/Models/API/PagedRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using ReasnAPI.Models.Enums;

namespace ReasnAPI.Models.API
{
public enum SortBy
{
CreatedAt,
StartAt,
Name
}

public enum SortOrder
{
Ascending,
Descending
}

public class PagedRequest
{
public string FilterName { get; set; }
public EventStatus? FilterStatus { get; set; }
public List<string> FilterTags { get; set; }
public DateTime? FilterStartAt { get; set; }
public DateTime? FilterEndAt { get; set; }
public int Offset { get; set; }
public int Limit { get; set; }
public SortBy SortBy { get; set; }
public SortOrder SortOrder { get; set; }
}
}

32 changes: 32 additions & 0 deletions Server/ReasnAPI/ReasnAPI/Models/API/PagedResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using ReasnAPI.Models.Enums;

namespace ReasnAPI.Models.API
{
public class PagedResponse<T>
{
public enum SortByEnum
{
CreatedAt,
StartAt,
Name
}

public enum SortOrderEnum
{
Ascending,
Descending
}

public List<T> Items { get; set; }

Check warning on line 20 in Server/ReasnAPI/ReasnAPI/Models/API/PagedResponse.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (ubuntu-latest)

Non-nullable property 'Items' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 20 in Server/ReasnAPI/ReasnAPI/Models/API/PagedResponse.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (macos-latest)

Non-nullable property 'Items' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 20 in Server/ReasnAPI/ReasnAPI/Models/API/PagedResponse.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (windows-latest)

Non-nullable property 'Items' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string FilterName { get; set; }

Check warning on line 21 in Server/ReasnAPI/ReasnAPI/Models/API/PagedResponse.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (ubuntu-latest)

Non-nullable property 'FilterName' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 21 in Server/ReasnAPI/ReasnAPI/Models/API/PagedResponse.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (macos-latest)

Non-nullable property 'FilterName' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 21 in Server/ReasnAPI/ReasnAPI/Models/API/PagedResponse.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (windows-latest)

Non-nullable property 'FilterName' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public EventStatus? FilterStatus { get; set; }
public List<string> FilterTags { get; set; }

Check warning on line 23 in Server/ReasnAPI/ReasnAPI/Models/API/PagedResponse.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (ubuntu-latest)

Non-nullable property 'FilterTags' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 23 in Server/ReasnAPI/ReasnAPI/Models/API/PagedResponse.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (macos-latest)

Non-nullable property 'FilterTags' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 23 in Server/ReasnAPI/ReasnAPI/Models/API/PagedResponse.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (windows-latest)

Non-nullable property 'FilterTags' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public DateTime? FilterStartAt { get; set; }
public DateTime? FilterEndAt { get; set; }
public int Offset { get; set; }
public int Limit { get; set; }
public SortByEnum SortBy { get; set; }
public SortOrderEnum SortOrder { get; set; }
public int TotalCount { get; set; }
}
}
73 changes: 72 additions & 1 deletion Server/ReasnAPI/ReasnAPI/Services/EventService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,5 +399,76 @@ private string CreateSlug(EventDto eventDto)
return finalSlug;

}
public PagedResponse<EventDto> GetPagedEvents(PagedRequest request)
{
var query = context.Events
.Include(e => e.Parameters)
.Include(e => e.Tags)
.AsQueryable();

if (!string.IsNullOrEmpty(request.FilterName))
query = query.Where(e => e.Name.Contains(request.FilterName));

if (request.FilterStatus.HasValue)
query = query.Where(e => e.Status == request.FilterStatus.Value);
if (request.FilterTags != null && request.FilterTags.Any())

query = query.Where(e => e.Tags.Any(t => request.FilterTags.Contains(t.Name)));

if (request.FilterStartAt.HasValue)
query = query.Where(e => e.StartAt >= request.FilterStartAt);

if (request.FilterEndAt.HasValue)
query = query.Where(e => e.EndAt <= request.FilterEndAt);


var totalCount = query.Count();

switch (request.SortBy)
{
case SortBy.StartAt:
query = request.SortOrder == SortOrder.Ascending ?
query.OrderBy(e => e.StartAt) :
query.OrderByDescending(e => e.StartAt);
break;
case SortBy.Name:
query = request.SortOrder == SortOrder.Ascending ?
query.OrderBy(e => e.Name) :
query.OrderByDescending(e => e.Name);
break;
case SortBy.CreatedAt:
query = request.SortOrder == SortOrder.Ascending ?
query.OrderBy(e => e.CreatedAt) :
query.OrderByDescending(e => e.CreatedAt);
break;
default:
query = query.OrderBy(e => e.StartAt);
break;
}

var events = query
.Skip((request.Offset))
.Take(request.Limit)
.ToList();

var response = new PagedResponse<EventDto>
{
Items = events.Select(e => e.ToDto()).ToList(),
TotalCount = totalCount,
FilterName = request.FilterName,
FilterStatus = request.FilterStatus,
FilterTags = request.FilterTags,
FilterStartAt = request.FilterStartAt,
FilterEndAt = request.FilterEndAt,
Offset = request.Offset,
Limit = request.Limit,
SortBy = (PagedResponse<EventDto>.SortByEnum)request.SortBy,
SortOrder = (PagedResponse<EventDto>.SortOrderEnum)request.SortOrder
};

return response;
}

}


}

0 comments on commit 17afb81

Please sign in to comment.