Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions LTS/LTS.API/Common/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace LTS.API.Common;

public static class DateTimeExtensions
{
public static DateTime ToUtc(this DateTime dt) =>
dt.Kind switch
{
DateTimeKind.Utc => dt,
DateTimeKind.Local => dt.ToUniversalTime(),
_ => DateTime.SpecifyKind(dt, DateTimeKind.Utc)
};

public static DateTime? ToUtc(this DateTime? dt) =>
dt.HasValue ? dt.Value.ToUtc() : null;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
namespace LTS.API.Common.Response
{
public class PagedResult<T>
public class CasesPaginatedResult<T>
{
public List<T> Items { get; set; } = [];
public int TotalCount { get; set; }
public int PageNumber { get; set; }
public int PageSize { get; set; }
public int TotalPages => (int)Math.Ceiling((double)TotalCount / PageSize);
public int PendingCount { get; set; }
public int FinalizedCount { get; set; }
}
}
1 change: 1 addition & 0 deletions LTS/LTS.API/Features/CaseFeature/CaseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class CaseController(IMediator mediator) : ControllerBase
[HttpPost("Create")]
public async Task<IActionResult> CreateCase(CreateCaseCommand request, CancellationToken ct)
{
request.OrganizationId = Guid.Parse(User.FindFirst("OrganizationId")?.Value!);
var result = await _mediator.Send(request, ct);
return StatusCode((int)result.Status, result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

namespace LTS.API.Features.CaseFeature.Queries.GetCases
{
public record GetAllCasesQuery() : IRequest<ApiResponse<PagedResult<GetCaseDto>>>
public record GetAllCasesQuery() : IRequest<ApiResponse<CasesPaginatedResult<GetCaseDto>>>
{
public Guid OrganizationId { get; set; }
public int Page { get; set; } = 1;
public int PageSize { get; set; } = 20;
public int PageSize { get; set; } = 5;
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using CloudinaryDotNet.Core;
using LTS.API.Common.Response;
using LTS.API.Domain.Entities;
using LTS.API.Domain.Enums;
using LTS.API.Infrastructure.Persistence;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace LTS.API.Features.CaseFeature.Queries.GetCases
{
public class GetCasesHandler(AppDbContext context)
: IRequestHandler<GetAllCasesQuery, ApiResponse<PagedResult<GetCaseDto>>>
: IRequestHandler<GetAllCasesQuery, ApiResponse<CasesPaginatedResult<GetCaseDto>>>
{
private readonly AppDbContext _context = context;

public async Task<ApiResponse<PagedResult<GetCaseDto>>> Handle(
public async Task<ApiResponse<CasesPaginatedResult<GetCaseDto>>> Handle(
GetAllCasesQuery request, CancellationToken ct)
{
try
Expand All @@ -21,15 +22,18 @@ public async Task<ApiResponse<PagedResult<GetCaseDto>>> Handle(

var totalCount = await query.CountAsync(ct);
if (totalCount == 0)
return ApiResponse<PagedResult<GetCaseDto>>.Ok("Case Table is Empty");
return ApiResponse<CasesPaginatedResult<GetCaseDto>>.Ok("Case Table is Empty");

var pendingCount = await query.CountAsync(c => c.Status == CaseStatus.Pending, ct);
var finalizedCount = await query.CountAsync(c => c.Status == CaseStatus.Finalized, ct);

var cases = await FetchPageAsync(query, request, ct);

return ApiResponse<PagedResult<GetCaseDto>>.Ok(ToPagedResult(cases, totalCount, request));
return ApiResponse<CasesPaginatedResult<GetCaseDto>>.Ok(ToPagedResult(cases, totalCount,pendingCount,finalizedCount, request));
}
catch (Exception ex)
{
return ApiResponse<PagedResult<GetCaseDto>>
return ApiResponse<CasesPaginatedResult<GetCaseDto>>
.Fail($"Internal server error: {ex.Message}");
}
}
Expand Down Expand Up @@ -69,14 +73,16 @@ await query



private static PagedResult<GetCaseDto> ToPagedResult(
List<GetCaseDto> cases, int totalCount, GetAllCasesQuery request) =>
private static CasesPaginatedResult<GetCaseDto> ToPagedResult(
List<GetCaseDto> cases, int totalCount,int pendingCount,int finalizedCount,GetAllCasesQuery request) =>
new()
{
Items = cases,
TotalCount = totalCount,
PageNumber = request.Page,
PageSize = request.PageSize
PageSize = request.PageSize,
PendingCount=pendingCount,
FinalizedCount=finalizedCount
};
#endregion
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace LTS.API.Features.CaseFeature.Queries.SearchCases
{
public class SearchCasesHandler : IRequestHandler<SearchCasesQuery, ApiResponse<PagedResult<GetCaseDto>>>
public class SearchCasesHandler : IRequestHandler<SearchCasesQuery, ApiResponse<CasesPaginatedResult<GetCaseDto>>>
{
private readonly AppDbContext _context;

Expand All @@ -16,7 +16,7 @@ public SearchCasesHandler(AppDbContext context)
_context = context;
}

public async Task<ApiResponse<PagedResult<GetCaseDto>>> Handle(SearchCasesQuery request, CancellationToken cancellationToken)
public async Task<ApiResponse<CasesPaginatedResult<GetCaseDto>>> Handle(SearchCasesQuery request, CancellationToken cancellationToken)
{
try
{
Expand Down Expand Up @@ -90,19 +90,19 @@ public async Task<ApiResponse<PagedResult<GetCaseDto>>> Handle(SearchCasesQuery
)).ToListAsync(cancellationToken);

// 8. RETURN PAGED RESPONSE
var pagedResponse = new PagedResult<GetCaseDto>
var pagedResponse = new CasesPaginatedResult<GetCaseDto>
{
Items = cases,
PageNumber = request.PageNumber,
PageSize = request.PageSize,
TotalCount = totalCount,
};

return ApiResponse<PagedResult<GetCaseDto>>.Ok(pagedResponse);
return ApiResponse<CasesPaginatedResult<GetCaseDto>>.Ok(pagedResponse);
}
catch (Exception ex)
{
return ApiResponse<PagedResult<GetCaseDto>>.Fail($"Search failed: {ex.Message}");
return ApiResponse<CasesPaginatedResult<GetCaseDto>>.Fail($"Search failed: {ex.Message}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace LTS.API.Features.CaseFeature.Queries.SearchCases
{
public class SearchCasesQuery : IRequest<ApiResponse<PagedResult<GetCaseDto>>>
public class SearchCasesQuery : IRequest<ApiResponse<CasesPaginatedResult<GetCaseDto>>>
{
public string? SearchTerm { get; set; }
public int PageNumber { get; set; } = 1;
public int PageSize { get; set; } = 20;
public int PageSize { get; set; } = 5;

// Filters (optional)
public string? Status { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using LTS.API.Domain.Entities;
using LTS.API.Common;
using LTS.API.Domain.Entities;
using LTS.API.Features.Followups.Commands.CreateFollowup;

namespace LTS.API.Features.Followups.Commands
Expand All @@ -11,8 +12,8 @@ public static Followup ToEntity(this CreateFollowupCommand command)
{
Id = Guid.NewGuid(),
CaseId = command.CaseId,
HearingDate = DateTime.SpecifyKind(command.HearingDate, DateTimeKind.Utc),
NextHearingDate = command.NextHearingDate.HasValue ? DateTime.SpecifyKind(command.NextHearingDate.Value, DateTimeKind.Utc) : null,
HearingDate = command.HearingDate.ToUtc(),
NextHearingDate =command.NextHearingDate.ToUtc(),
InterimOrder = command.InterimOrder,
Decision = command.Decision,
Remarks = command.Remarks,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using LTS.API.Common.Response;
using LTS.API.Common;
using LTS.API.Common.Response;
using LTS.API.Infrastructure.Persistence;
using MediatR;

Expand All @@ -19,8 +20,8 @@ public async Task<ApiResponse<string>> Handle(UpdateFollowupCommand request, Can
if (followup is null)
return ApiResponse<string>.Fail("Followup not found.");

followup.HearingDate = request.HearingDate;
followup.NextHearingDate = request.NextHearingDate;
followup.HearingDate = request.HearingDate.ToUtc();
followup.NextHearingDate = request.NextHearingDate.ToUtc();
followup.InterimOrder = request.InterimOrder;
followup.Decision = request.Decision;
followup.Remarks = request.Remarks;
Expand Down