This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6450452
commit 6bcce94
Showing
8 changed files
with
99 additions
and
5 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
src/RookieShop.ApiService/Endpoints/Reports/RevenueYear.Request.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace RookieShop.ApiService.Endpoints.Reports; | ||
|
||
public sealed record RevenueYearRequest(int Year); |
29 changes: 29 additions & 0 deletions
29
src/RookieShop.ApiService/Endpoints/Reports/RevenueYear.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using MediatR; | ||
using Microsoft.AspNetCore.Http.HttpResults; | ||
using RookieShop.Application.Reports.DTOs; | ||
using RookieShop.Application.Reports.Queries.RevenueYear; | ||
using RookieShop.Infrastructure.Endpoints.Abstractions; | ||
using RookieShop.Infrastructure.RateLimiter; | ||
|
||
namespace RookieShop.ApiService.Endpoints.Reports; | ||
|
||
public sealed class RevenueYear(ISender sender) : IEndpoint<Ok<List<RevenueYearDto>>, RevenueYearRequest> | ||
{ | ||
public void MapEndpoint(IEndpointRouteBuilder app) => | ||
app.MapGet("/reports/revenue-year", async (int year) => await HandleAsync(new(year))) | ||
.Produces<Ok<List<RevenueYearDto>>>() | ||
.WithTags(nameof(Reports)) | ||
.WithName("Revenue Year") | ||
.MapToApiVersion(new(1, 0)) | ||
.RequirePerIpRateLimit(); | ||
|
||
public async Task<Ok<List<RevenueYearDto>>> HandleAsync(RevenueYearRequest request, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
RevenueYearQuery query = new(request.Year); | ||
|
||
var result = await sender.Send(query, cancellationToken); | ||
|
||
return TypedResults.Ok(result.Value.ToList()); | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace RookieShop.Application.Reports.DTOs; | ||
|
||
public sealed class RevenueYearDto | ||
{ | ||
public string? Month { get; set; } | ||
public decimal Revenue { get; set; } | ||
} |
43 changes: 43 additions & 0 deletions
43
src/RookieShop.Application/Reports/Queries/RevenueYear/RevenueYearHandler.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using Ardalis.Result; | ||
using Dapper; | ||
using RookieShop.Application.Reports.DTOs; | ||
using RookieShop.Domain.SharedKernel; | ||
using RookieShop.Persistence; | ||
|
||
namespace RookieShop.Application.Reports.Queries.RevenueYear; | ||
|
||
public sealed class RevenueYearHandler(IDatabaseFactory factory) | ||
: IQueryHandler<RevenueYearQuery, Result<IEnumerable<RevenueYearDto>>> | ||
{ | ||
public async Task<Result<IEnumerable<RevenueYearDto>>> Handle(RevenueYearQuery request, | ||
CancellationToken cancellationToken) | ||
{ | ||
const string sql = $""" | ||
WITH monthly_revenue AS ( | ||
SELECT | ||
to_char(o.created_date, 'Mon') AS month, | ||
SUM(od.price * od.quantity) AS revenue | ||
FROM orders o | ||
JOIN order_details od ON o.id = od.order_id | ||
WHERE EXTRACT(YEAR FROM o.created_date) = @year | ||
GROUP BY to_char(o.created_date, 'Mon') | ||
) | ||
SELECT | ||
month_names.month AS {nameof(RevenueYearDto.Month)}, | ||
COALESCE(monthly_revenue.revenue, 0) AS {nameof(RevenueYearDto.Revenue)} | ||
FROM ( | ||
VALUES ('Jan'), ('Feb'), ('Mar'), ('Apr'), ('May'), ('Jun'), | ||
('Jul'), ('Aug'), ('Sep'), ('Oct'), ('Nov'), ('Dec') | ||
) AS month_names(month) | ||
LEFT JOIN monthly_revenue ON month_names.month = monthly_revenue.month | ||
ORDER BY month_names.month; | ||
"""; | ||
|
||
using var conn = factory.GetOpenConnection(); | ||
|
||
var result = await conn.QueryAsync<RevenueYearDto>(sql, | ||
new { year = request.Year }); | ||
|
||
return result.ToList(); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/RookieShop.Application/Reports/Queries/RevenueYear/RevenueYearQuery.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using Ardalis.Result; | ||
using RookieShop.Application.Reports.DTOs; | ||
using RookieShop.Domain.SharedKernel; | ||
|
||
namespace RookieShop.Application.Reports.Queries.RevenueYear; | ||
|
||
public sealed record RevenueYearQuery(int Year) : IQuery<Result<IEnumerable<RevenueYearDto>>>; |
8 changes: 8 additions & 0 deletions
8
src/RookieShop.Application/Reports/Queries/RevenueYear/RevenueYearValidator.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using FluentValidation; | ||
|
||
namespace RookieShop.Application.Reports.Queries.RevenueYear; | ||
|
||
public sealed class RevenueYearValidator : AbstractValidator<RevenueYearQuery> | ||
{ | ||
public RevenueYearValidator() => RuleFor(x => x.Year).NotEmpty().GreaterThan(2000); | ||
} |
This file contains 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