Skip to content

Commit

Permalink
added dashboard service
Browse files Browse the repository at this point in the history
  • Loading branch information
iammukeshm committed Mar 18, 2021
1 parent f2b511b commit 514a972
Show file tree
Hide file tree
Showing 17 changed files with 238 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BlazorHero.CleanArchitecture.Application.Features.Dashboard.GetData
{
public class DashboardDataResponse
{
public int ProductCount { get; set; }
public int BrandCount { get; set; }
public int UserCount { get; set; }
public int RoleCount { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using BlazorHero.CleanArchitecture.Application.Interfaces.Repositories;
using BlazorHero.CleanArchitecture.Application.Interfaces.Services.Identity;
using BlazorHero.CleanArchitecture.Domain.Entities.Catalog;
using BlazorHero.CleanArchitecture.Shared.Wrapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace BlazorHero.CleanArchitecture.Application.Features.Dashboard.GetData
{
public class GetDashboardDataQuery : IRequest<Result<DashboardDataResponse>>
{
public class GetDashboardDataQueryHandler : IRequestHandler<GetDashboardDataQuery, Result<DashboardDataResponse>>
{
private readonly IUnitOfWork _unitOfWork;
private readonly IUserService _userService;
private readonly IRoleService _roleService;

public GetDashboardDataQueryHandler(IUnitOfWork unitOfWork, IUserService userService, IRoleService roleService)
{
_unitOfWork = unitOfWork;
_userService = userService;
_roleService = roleService;
}
public async Task<Result<DashboardDataResponse>> Handle(GetDashboardDataQuery query, CancellationToken cancellationToken)
{
var response = new DashboardDataResponse();
response.ProductCount = await _unitOfWork.Repository<Product>().Entities.CountAsync();
response.BrandCount = await _unitOfWork.Repository<Brand>().Entities.CountAsync();
response.UserCount = await _userService.GetCountAsync();
response.RoleCount = await _roleService.GetCountAsync();
return Result<DashboardDataResponse>.Success(response);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace BlazorHero.CleanArchitecture.Application.Interfaces.Services.Identity
public interface IRoleService : IService
{
Task<Result<List<RoleResponse>>> GetAllAsync();
Task<int> GetCountAsync();

Task<Result<RoleResponse>> GetByIdAsync(string id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace BlazorHero.CleanArchitecture.Application.Interfaces.Services.Identity
public interface IUserService : IService
{
Task<Result<List<UserResponse>>> GetAllAsync();
Task<int> GetCountAsync();

Task<IResult<UserResponse>> GetAsync(string userId);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using BlazorHero.CleanArchitecture.Application.Features.Dashboard.GetData;
using BlazorHero.CleanArchitecture.Client.Infrastructure.Extensions;
using BlazorHero.CleanArchitecture.Shared.Wrapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace BlazorHero.CleanArchitecture.Client.Infrastructure.Managers.Dashboard
{
public class DashboardManager : IDashboardManager
{
private readonly HttpClient _httpClient;

public DashboardManager(HttpClient httpClient)
{
_httpClient = httpClient;
}

public async Task<IResult<DashboardDataResponse>> GetDataAsync()
{
try
{
var response = await _httpClient.GetAsync(Routes.DashboardEndpoint.GetData);
var data = await response.ToResult<DashboardDataResponse>();
return data;
}
catch (Exception ex)
{

throw;
}

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using BlazorHero.CleanArchitecture.Application.Features.Dashboard.GetData;
using BlazorHero.CleanArchitecture.Shared.Wrapper;
using System.Threading.Tasks;

namespace BlazorHero.CleanArchitecture.Client.Infrastructure.Managers.Dashboard
{
public interface IDashboardManager : IManager
{
Task<IResult<DashboardDataResponse>> GetDataAsync();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ public static class BrandsEndpoint
public static string GetAll = "api/v1/brands";
public static string Delete = "api/v1/brands";
public static string Save = "api/v1/brands";
public static string GetCount = "api/v1/brands/count";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BlazorHero.CleanArchitecture.Client.Infrastructure.Routes
{
public class DashboardEndpoint
{
public static string GetData = "api/v1/dashboard";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public static string GetAllPaged(int pageNumber, int pageSize)
{
return $"api/v1/products?pageNumber={pageNumber}&pageSize={pageSize}";
}

public static string GetCount = "api/v1/products/count";
public static string GetProductImage(int productId)
{
return $"api/v1/products/image/{productId}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,10 @@ public async Task<Result<string>> UpdatePermissionsAsync(PermissionRequest reque
return Result<string>.Fail(ex.Message);
}
}
public async Task<int> GetCountAsync()
{
var count = await _roleManager.Roles.CountAsync();
return count;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -215,5 +215,11 @@ public async Task<IResult> ResetPasswordAsync(ResetPasswordRequest request)
return Result.Fail("An Error has occured!");
}
}

public async Task<int> GetCountAsync()
{
var count = await _userManager.Users.CountAsync();
return count;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ private async Task SaveAsync()
form.Validate();
if (form.IsValid)
{
//TODO: Try to integrate validation with Mudblazor component - Select
if (BrandId == 0)
{
_snackBar.Add("Select a Brand.", Severity.Error);
Expand Down
75 changes: 75 additions & 0 deletions BlazorHero.CleanArchitecture/Client/Pages/Content/Dashboard.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
@page "/dashboard"
@inject Microsoft.Extensions.Localization.IStringLocalizer<Dashboard> localizer
<HeroTitle Title="@localizer["Dashboard"]" Description="@localizer["Quick Insights."]" />
<MudGrid>
<MudItem xs="12" sm="6" md="3">
<MudPaper Elevation="25" Class="d-flex flex-row pt-6 pb-4" Style="height:100px;">
<MudIcon Icon="@Icons.Material.Filled.Euro" Color="Color.Primary" Class="mx-4" Style="width:54px; height:54px;"></MudIcon>
<div>
<MudText Typo="Typo.subtitle1" Class="mud-text-secondary mb-n1">Products</MudText>
<MudText Typo="Typo.h5">@ProductCount</MudText>
</div>
</MudPaper>
</MudItem>
<MudItem xs="12" sm="6" md="3">
<MudPaper Elevation="25" Class="d-flex flex-row pt-6 pb-4" Style="height:100px;">
<MudIcon Icon="@Icons.Material.Filled.Home" Color="Color.Secondary" Class="mx-4" Style="width:54px; height:54px;"></MudIcon>
<div>
<MudText Typo="Typo.subtitle1" Class="mud-text-secondary mb-n1">Brands</MudText>
<MudText Typo="Typo.h5">@BrandCount</MudText>
</div>
</MudPaper>
</MudItem>
<MudItem xs="12" sm="6" md="3">
<MudPaper Elevation="25" Class="d-flex flex-row pt-6 pb-4" Style="height:100px;">
<MudIcon Icon="@Icons.Material.Filled.Public" Color="Color.Success" Class="mx-4" Style="width:54px; height:54px;"></MudIcon>
<div>
<MudText Typo="Typo.subtitle1" Class="mud-text-secondary mb-n1">Registered Users</MudText>
<MudText Typo="Typo.h5">@UserCount</MudText>
</div>
</MudPaper>
</MudItem>
<MudItem xs="12" sm="6" md="3">
<MudPaper Elevation="25" Class="d-flex flex-row pt-6 pb-4" Style="height:100px;">
<MudIcon Icon="@Icons.Custom.Uncategorized.Radioactive" Color="Color.Warning" Class="mx-4" Style="width:54px; height:54px;"></MudIcon>
<div>
<MudText Typo="Typo.subtitle1" Class="mud-text-secondary mb-n1">Registered Roles</MudText>
<MudText Typo="Typo.h5">@RoleCount</MudText>
</div>
</MudPaper>
</MudItem>
<MudItem xs="12" sm="8">
<MudPaper Elevation="25" Class="pa-4" Style="height:300px;">
Charts comming soon ish...
</MudPaper>
</MudItem>
<MudItem xs="12" sm="4">
<MudPaper Elevation="25" Class="pa-4" Style="height:300px;">
Charts comming soon ish...
</MudPaper>
</MudItem>
</MudGrid>
@code{

[Parameter]
public int ProductCount { get; set; }
[Parameter]
public int BrandCount { get; set; }
[Parameter]
public int UserCount { get; set; }
[Parameter]
public int RoleCount { get; set; }
protected override async Task OnInitializedAsync() => await LoadDataAsync();

private async Task LoadDataAsync()
{
var data = await _dashboardManager.GetDataAsync();
if(data.Succeeded)
{
ProductCount = data.Data.ProductCount;
BrandCount = data.Data.BrandCount;
UserCount = data.Data.UserCount;
RoleCount = data.Data.RoleCount;
}
}
}
4 changes: 3 additions & 1 deletion BlazorHero.CleanArchitecture/Client/_Imports.razor
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
@using BlazorHero.CleanArchitecture.Client.Infrastructure.Managers.Preferences
@using BlazorHero.CleanArchitecture.Client.Infrastructure.Managers.Catalog.Product
@using BlazorHero.CleanArchitecture.Client.Infrastructure.Managers.Catalog.Brand
@using BlazorHero.CleanArchitecture.Client.Infrastructure.Managers.Dashboard

@*Shared Libraries*@
@using BlazorHero.CleanArchitecture.Shared.Constants.Permission
Expand Down Expand Up @@ -51,4 +52,5 @@
@inject IRoleManager _roleManager
@inject IUserManager _userManager
@inject IProductManager _productManager
@inject IBrandManager _brandManager
@inject IBrandManager _brandManager
@inject IDashboardManager _dashboardManager
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async Task<IActionResult> GetById(int id)
var brand = await _mediator.Send(new GetBrandByIdQuery() { Id = id });
return Ok(brand);
}

[Authorize(Policy = Permissions.Brands.Create)]
[HttpPost]
public async Task<IActionResult> Post(AddEditBrandCommand command)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public async Task<IActionResult> GetProductImageAsync(int id)
var result = await _mediator.Send(new GetProductImageQuery(id));
return Ok(result);
}

[Authorize(Policy = Permissions.Products.Create)]
[HttpPost]
public async Task<IActionResult> Post(AddEditProductCommand command)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using BlazorHero.CleanArchitecture.Application.Features.Dashboard.GetData;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BlazorHero.CleanArchitecture.Server.Controllers.v1
{
[ApiController]
public class DashboardController : BaseApiController<DashboardController>
{
[Authorize]
[HttpGet]
public async Task<IActionResult> GetDataAsync()
{
var result = await _mediator.Send(new GetDashboardDataQuery());
return Ok(result);
}
}
}

0 comments on commit 514a972

Please sign in to comment.