diff --git a/Server/ReasnAPI/ReasnAPI/Controllers/EventsController.cs b/Server/ReasnAPI/ReasnAPI/Controllers/EventsController.cs index e65e56e..b4ebc5a 100644 --- a/Server/ReasnAPI/ReasnAPI/Controllers/EventsController.cs +++ b/Server/ReasnAPI/ReasnAPI/Controllers/EventsController.cs @@ -299,5 +299,41 @@ public IActionResult DeleteEventsTag([FromRoute] int tagId) { tagService.DeleteTag(tagId); return NoContent(); - } + } + [HttpGet] + [Route("api/EventsPaging")] + public async Task>> GetPagedEvents( + [FromQuery] string? filterName, + [FromQuery] EventStatus? filterStatus, + [FromQuery] List? 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, + FilterStatus = filterStatus, + FilterTags = filterTags, + 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}"); + } + } } \ No newline at end of file diff --git a/Server/ReasnAPI/ReasnAPI/Models/API/PagedRequest.cs b/Server/ReasnAPI/ReasnAPI/Models/API/PagedRequest.cs new file mode 100644 index 0000000..1d82897 --- /dev/null +++ b/Server/ReasnAPI/ReasnAPI/Models/API/PagedRequest.cs @@ -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 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; } + } +} + diff --git a/Server/ReasnAPI/ReasnAPI/Models/API/PagedResponse.cs b/Server/ReasnAPI/ReasnAPI/Models/API/PagedResponse.cs new file mode 100644 index 0000000..b2648d0 --- /dev/null +++ b/Server/ReasnAPI/ReasnAPI/Models/API/PagedResponse.cs @@ -0,0 +1,32 @@ +using ReasnAPI.Models.Enums; + +namespace ReasnAPI.Models.API +{ + public class PagedResponse + { + public enum SortByEnum + { + CreatedAt, + StartAt, + Name + } + + public enum SortOrderEnum + { + Ascending, + Descending + } + + public List Items { get; set; } + public string FilterName { get; set; } + public EventStatus? FilterStatus { get; set; } + public List FilterTags { get; set; } + 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; } + } +} diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index b158abd..d47ac18 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -399,5 +399,76 @@ private string CreateSlug(EventDto eventDto) return finalSlug; } + public PagedResponse 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 + { + 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.SortByEnum)request.SortBy, + SortOrder = (PagedResponse.SortOrderEnum)request.SortOrder + }; + + return response; + } + +} + -} \ No newline at end of file