Skip to content

Commit

Permalink
Merge pull request #132 from SWD392-Domus/feature/admin-dashboard
Browse files Browse the repository at this point in the history
Feature/admin dashboard
  • Loading branch information
duykasama authored Mar 17, 2024
2 parents 4212449 + 67a644f commit 33dea3d
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Domus.Api/Controllers/AdminController.cs
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);
}
}
74 changes: 74 additions & 0 deletions Domus.Service/Implementations/AdminService.cs
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)
};
}
}
10 changes: 10 additions & 0 deletions Domus.Service/Interfaces/IAdminService.cs
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);
}
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; }
}
15 changes: 15 additions & 0 deletions Domus.Service/Models/Responses/DashboardResponse.cs
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; }
}

0 comments on commit 33dea3d

Please sign in to comment.