-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #132 from SWD392-Domus/feature/admin-dashboard
Feature/admin dashboard
- Loading branch information
Showing
5 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
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,28 @@ | ||
using Domus.Api.Controllers.Base; | ||
using Domus.Service.Constants; | ||
using Domus.Service.Interfaces; | ||
using Domus.Service.Models.Requests.Dashboard; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Domus.Api.Controllers; | ||
|
||
[Authorize(Roles = UserRoleConstants.ADMIN, AuthenticationSchemes = "Bearer")] | ||
[Route("api/[controller]")] | ||
public class AdminController : BaseApiController | ||
{ | ||
private readonly IAdminService _adminService; | ||
|
||
public AdminController(IAdminService adminService) | ||
{ | ||
_adminService = adminService; | ||
} | ||
|
||
[HttpGet("dashboard")] | ||
public async Task<IActionResult> Dashboard([FromQuery] GetDashboardInfoRequest request) | ||
{ | ||
return await ExecuteServiceLogic( | ||
async () => await _adminService.GetDashboardInfo(request).ConfigureAwait(false) | ||
).ConfigureAwait(false); | ||
} | ||
} |
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,74 @@ | ||
using System.Globalization; | ||
using Domus.DAL.Interfaces; | ||
using Domus.Service.Enums; | ||
using Domus.Service.Interfaces; | ||
using Domus.Service.Models; | ||
using Domus.Service.Models.Requests.Dashboard; | ||
using Domus.Service.Models.Responses; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Domus.Service.Implementations; | ||
|
||
public class AdminService : IAdminService | ||
{ | ||
private readonly IContractRepository _contractRepository; | ||
private readonly IQuotationRevisionRepository _quotationRevisionRepository; | ||
|
||
public AdminService(IContractRepository contractRepository, IQuotationRevisionRepository quotationRevisionRepository) | ||
{ | ||
_contractRepository = contractRepository; | ||
_quotationRevisionRepository = quotationRevisionRepository; | ||
} | ||
|
||
public async Task<ServiceActionResult> GetDashboardInfo(GetDashboardInfoRequest request) | ||
{ | ||
var contracts = await (await _contractRepository.FindAsync(c => | ||
!c.IsDeleted | ||
&& c.Status == ContractStatus.SIGNED | ||
&& c.SignedAt.HasValue | ||
&& c.SignedAt.Value.Year == request.Year)) | ||
.ToListAsync(); | ||
|
||
var dashboardResponse = new DashboardResponse(); | ||
|
||
for (var i = 1; i <= 12; i++) | ||
{ | ||
var contractsByMonth = contracts.Where(c => c.SignedAt.HasValue && c.SignedAt.Value.Month == i); | ||
var revenueByMonth = await (await _quotationRevisionRepository.FindAsync(qr => | ||
contractsByMonth.Select(c => c.QuotationRevisionId).Contains(qr.Id))) | ||
.Select(qr => qr.TotalPrice) | ||
.SumAsync(); | ||
|
||
dashboardResponse.RevenueByMonths.Add(new() | ||
{ | ||
Revenue = (float)revenueByMonth, | ||
MonthAsNumber = i, | ||
MonthAsString = GetMonthAsString(i) | ||
}); | ||
} | ||
|
||
dashboardResponse.TotalRevenue = dashboardResponse.RevenueByMonths.Select(rbm => rbm.Revenue).Sum(); | ||
|
||
return new ServiceActionResult(true) { Data = dashboardResponse }; | ||
} | ||
|
||
private string GetMonthAsString(int month) | ||
{ | ||
return month switch | ||
{ | ||
1 => "January", | ||
2 => "February", | ||
3 => "March", | ||
4 => "April", | ||
5 => "May", | ||
6 => "June", | ||
7 => "July", | ||
8 => "August", | ||
9 => "September", | ||
10 => "October", | ||
11 => "November", | ||
12 => "December", | ||
_ => throw new ArgumentOutOfRangeException(nameof(month), month, null) | ||
}; | ||
} | ||
} |
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,10 @@ | ||
using Domus.Common.Interfaces; | ||
using Domus.Service.Models; | ||
using Domus.Service.Models.Requests.Dashboard; | ||
|
||
namespace Domus.Service.Interfaces; | ||
|
||
public interface IAdminService : IAutoRegisterable | ||
{ | ||
Task<ServiceActionResult> GetDashboardInfo(GetDashboardInfoRequest request); | ||
} |
9 changes: 9 additions & 0 deletions
9
Domus.Service/Models/Requests/Dashboard/GetDashboardInfoRequest.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,9 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace Domus.Service.Models.Requests.Dashboard; | ||
|
||
public class GetDashboardInfoRequest | ||
{ | ||
[Range(typeof(int),"1970", "2030")] | ||
public int Year { get; set; } | ||
} |
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,15 @@ | ||
namespace Domus.Service.Models.Responses; | ||
|
||
public class DashboardResponse | ||
{ | ||
public float TotalRevenue { get; set; } | ||
public int NewUsersCount { get; set; } | ||
public IList<RevenueByMonth> RevenueByMonths { get; set; } = new List<RevenueByMonth>(); | ||
} | ||
|
||
public class RevenueByMonth | ||
{ | ||
public string MonthAsString { get; set; } | ||
public int MonthAsNumber { get; set; } | ||
public float Revenue { get; set; } | ||
} |