Skip to content

Commit

Permalink
add areachart and fix bugs on some charts
Browse files Browse the repository at this point in the history
  • Loading branch information
lehquy13 committed May 21, 2023
1 parent e8ea67a commit 149ff1d
Show file tree
Hide file tree
Showing 14 changed files with 336 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using CED.Contracts.Charts;
using MediatR;

namespace CED.Application.Services.DashBoard.Queries;

public record GetAreaChartDataQuery
(
string ByTime = ""
): IRequest<AreaChartData>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using Castle.Core.Internal;
using CED.Application.Services.Abstractions.QueryHandlers;
using CED.Contracts.Charts;
using CED.Domain.ClassInformations;
using CED.Domain.Shared.ClassInformationConsts;
using MapsterMapper;

namespace CED.Application.Services.DashBoard.Queries;

public class GetAreaChartDataQueryHandler : GetByIdQueryHandler<GetAreaChartDataQuery, AreaChartData>
{
private readonly IClassInformationRepository _classInformationRepository;

public GetAreaChartDataQueryHandler(IMapper mapper, IClassInformationRepository classInformationRepository) :
base(mapper)
{
_classInformationRepository = classInformationRepository;
}

public override async Task<AreaChartData?> Handle(GetAreaChartDataQuery query,
CancellationToken cancellationToken)
{
await Task.CompletedTask;


List<int> dates = new List<int>();

var startDay = DateTime.Today.Subtract(TimeSpan.FromDays(6));
switch (query.ByTime)
{
case ByTime.Month:
startDay = DateTime.Today.Subtract(TimeSpan.FromDays(29));
for (int i = 0; i < 30; i++)
{
dates.Add(startDay.Day);
startDay = startDay.AddDays(1);
}
break;
case ByTime.Week:
for (int i = 0; i < 7; i++)
{
dates.Add(startDay.Day);
startDay = startDay.AddDays(1);
}
break;
}
startDay = DateTime.Today.Subtract(TimeSpan.FromDays(6));


var classesInWeek = dates.GroupJoin(
_classInformationRepository.GetAll()
.Where(x => x.CreationTime >= startDay && x.Status == Status.Confirmed)
.GroupBy(x => x.CreationTime.Day),
d => d,
c => c.Key,
(d, c) => new
{
dates = d,
sum = c.FirstOrDefault()?.Sum(r => r.ChargeFee)
});
var classesInWeek1 = dates.GroupJoin(
_classInformationRepository.GetAll()
.Where(x => x.CreationTime >= startDay && x.Status == Status.Canceled)
.GroupBy(x => x.CreationTime.Day),
d => d,
c => c.Key,
(d, c) => new
{
dates = d,
sum = c.FirstOrDefault()?.Sum(r => r.ChargeFee)
});
var classesInWeek2 = dates.GroupJoin(
_classInformationRepository.GetAll()
.Where(x => x.CreationTime >= startDay && x.Status == Status.OnConfirming)
.GroupBy(x => x.CreationTime.Day),
d => d,
c => c.Key,
(d, c) => new
{
dates = d,
sum = c.FirstOrDefault()?.Sum(r => r.ChargeFee)
});


List<float> resultInts = classesInWeek
.Select(x => x.sum ?? 0)
.ToList();
if (resultInts.IsNullOrEmpty())
{
resultInts.Add(1);
}
List<float> resultInts1 = classesInWeek1
.Select(x => x.sum ?? 0)
.ToList();
if (resultInts1.IsNullOrEmpty())
{
resultInts.Add(1);
}
List<float> resultInts2 = classesInWeek2
.Select(x => x.sum ?? 0)
.ToList();
if (resultInts2.IsNullOrEmpty())
{
resultInts.Add(1);
}

List<string> resultStrings = classesInWeek
.Select(x => x.dates.ToString())
.ToList();
if (resultStrings.IsNullOrEmpty())
{
resultStrings.Add("None");
}


return new AreaChartData
(
new AreaData("Total Revenues",resultInts ),
new AreaData("Charged",resultInts2),
new AreaData( "Refunded",resultInts1),
resultStrings
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using CED.Application.Services.Abstractions.QueryHandlers;
using CED.Contracts.Charts;
using CED.Domain.ClassInformations;
using CED.Domain.Shared.ClassInformationConsts;
using MapsterMapper;

namespace CED.Application.Services.DashBoard.Queries;
Expand All @@ -22,11 +23,11 @@ public GetDonutChartDataQueryHandler(IMapper mapper, IClassInformationRepository
var startDay = DateTime.Today;
switch (query.ByTime)
{
case "month":
case ByTime.Month:
startDay = DateTime.Today.Subtract(TimeSpan.FromDays(29));

break;
case "week":
case ByTime.Week:
startDay = DateTime.Today.Subtract(TimeSpan.FromDays(6));
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public GetLineChartDataQueryHandler(IMapper mapper, IClassInformationRepository

startDay = DateTime.Today.Subtract(TimeSpan.FromDays(6));

var classesInWeek = dates.GroupJoin(
var classesInWeek =dates.GroupJoin(
_classInformationRepository.GetAll()
.Where(x => x.CreationTime >= startDay)
.GroupBy(x => x.CreationTime.Day),
Expand Down
4 changes: 4 additions & 0 deletions CED.Contracts/Charts/AreaChartData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace CED.Contracts.Charts;

public record AreaData( string name,List<float> data);
public record AreaChartData(AreaData totalRevuenues,AreaData incoming,AreaData cenceleds,List<string> dates);
1 change: 1 addition & 0 deletions CED.Domain.Shared/ClassInformationConsts/ByTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public static class ByTime
{
public const string Today = "Today";
public const string Month = "This Month";
public const string Week = "This Week";
public const string Year = "This Year";
}
Loading

0 comments on commit 149ff1d

Please sign in to comment.