forked from hamed-shirbandi/TaskoMask
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get Reports for Organization details feature in Aggregator hamed-shir…
- Loading branch information
1 parent
ebe75ec
commit e80d0e7
Showing
19 changed files
with
442 additions
and
12 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
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
13 changes: 13 additions & 0 deletions
13
src/1-BuildingBlocks/Contracts/Protos/get_organizationReport_by_id.proto
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,13 @@ | ||
syntax = "proto3"; | ||
|
||
package TaskoMask.BuildingBlocks.Contracts.Protos; | ||
|
||
import "base.proto"; | ||
|
||
service GetOrganizationReportIdGrpcService { | ||
rpc Handle (GetOrganizationReportIdGrpcRequest) returns (GetOrganizationReportGrpcResponse); | ||
} | ||
|
||
message GetOrganizationReportIdGrpcRequest { | ||
string organizationId = 1; | ||
} |
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
26 changes: 26 additions & 0 deletions
26
...Features/Organizations/GetOrganizationReportById/GetOrganizationReportByIdGrpcEndpoint.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,26 @@ | ||
using AutoMapper; | ||
using Grpc.Core; | ||
using System.Threading.Tasks; | ||
using TaskoMask.BuildingBlocks.Application.Bus; | ||
using TaskoMask.BuildingBlocks.Contracts.Protos; | ||
|
||
namespace TaskoMask.Services.Owners.Read.Api.Features.Organizations.GetOrganizationReportById; | ||
|
||
|
||
public class GetOrganizationReportByIdGrpcEndpoint : GetOrganizationReportIdGrpcService.GetOrganizationReportIdGrpcServiceBase | ||
{ | ||
private readonly IInMemoryBus _inMemoryBus; | ||
private readonly IMapper _mapper; | ||
|
||
public GetOrganizationReportByIdGrpcEndpoint(IInMemoryBus inMemoryBus, IMapper mapper) | ||
{ | ||
_inMemoryBus = inMemoryBus; | ||
_mapper = mapper; | ||
} | ||
|
||
public override async Task<GetOrganizationReportGrpcResponse> Handle(GetOrganizationReportIdGrpcRequest request, ServerCallContext context) | ||
{ | ||
var report = await _inMemoryBus.SendQuery(new GetOrganizationReportByIdRequest(request.OrganizationId)); | ||
return _mapper.Map<GetOrganizationReportGrpcResponse>(report.Value); | ||
} | ||
} |
207 changes: 207 additions & 0 deletions
207
....Api/Features/Organizations/GetOrganizationReportById/GetOrganizationReportByIdHandler.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,207 @@ | ||
using AutoMapper; | ||
using Grpc.Core; | ||
using MediatR; | ||
using MongoDB.Driver; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using TaskoMask.BuildingBlocks.Application.Exceptions; | ||
using TaskoMask.BuildingBlocks.Application.Queries; | ||
using TaskoMask.BuildingBlocks.Contracts.Dtos.Boards; | ||
using TaskoMask.BuildingBlocks.Contracts.Dtos.Cards; | ||
using TaskoMask.BuildingBlocks.Contracts.Dtos.Organizations; | ||
using TaskoMask.BuildingBlocks.Contracts.Dtos.Tasks; | ||
using TaskoMask.BuildingBlocks.Contracts.Enums; | ||
using TaskoMask.BuildingBlocks.Contracts.Protos; | ||
using TaskoMask.BuildingBlocks.Contracts.Resources; | ||
using TaskoMask.BuildingBlocks.Contracts.ViewModels; | ||
using TaskoMask.BuildingBlocks.Domain.Resources; | ||
using TaskoMask.Services.Owners.Read.Api.Infrastructure.DbContext; | ||
using static TaskoMask.BuildingBlocks.Contracts.Protos.GetBoardsByOrganizationIdGrpcService; | ||
using static TaskoMask.BuildingBlocks.Contracts.Protos.GetCardsByBoardIdGrpcService; | ||
using static TaskoMask.BuildingBlocks.Contracts.Protos.GetTasksByCardIdGrpcService; | ||
|
||
|
||
namespace TaskoMask.Services.Owners.Read.Api.Features.Organizations.GetOrganizationReportById; | ||
|
||
public class GetOrganizationReportByIdHandler : BaseQueryHandler, IRequestHandler<GetOrganizationReportByIdRequest, OrganizationReportDto> | ||
{ | ||
#region Fields | ||
|
||
private readonly OwnerReadDbContext _ownerReadDbContext; | ||
private readonly GetBoardsByOrganizationIdGrpcServiceClient _getBoardsByOrganizationIdGrpcServiceClient; | ||
private readonly GetCardsByBoardIdGrpcServiceClient _getCardsByBoardIdGrpcServiceClient; | ||
private readonly GetTasksByCardIdGrpcServiceClient _getTasksByCardIdGrpcServiceClient; | ||
#endregion | ||
|
||
#region Ctors | ||
|
||
public GetOrganizationReportByIdHandler(OwnerReadDbContext ownerReadDbContext, | ||
GetBoardsByOrganizationIdGrpcServiceClient getBoardsByOrganizationIdGrpcServiceClient, | ||
GetCardsByBoardIdGrpcServiceClient getCardsByBoardIdGrpcServiceClient, | ||
GetTasksByCardIdGrpcServiceClient getTasksByCardIdGrpcServiceClient, | ||
IMapper mapper) | ||
: base(mapper) | ||
{ | ||
_ownerReadDbContext = ownerReadDbContext; | ||
_getBoardsByOrganizationIdGrpcServiceClient = getBoardsByOrganizationIdGrpcServiceClient; | ||
_getCardsByBoardIdGrpcServiceClient = getCardsByBoardIdGrpcServiceClient; | ||
_getTasksByCardIdGrpcServiceClient = getTasksByCardIdGrpcServiceClient; | ||
} | ||
|
||
#endregion | ||
|
||
#region Handlers | ||
|
||
|
||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
public async Task<OrganizationReportDto> Handle(GetOrganizationReportByIdRequest request, CancellationToken cancellationToken) | ||
{ | ||
var organizationReportDto = new OrganizationReportDto(); | ||
var organization = await _ownerReadDbContext.Organizations | ||
.Find(e => e.Id == request.Id) | ||
.FirstOrDefaultAsync(cancellationToken: cancellationToken); | ||
|
||
if (organization == null) | ||
throw new ApplicationException(ContractsMessages.Data_Not_exist, DomainMetadata.Organization); | ||
|
||
#region Project Reports | ||
var organizationProjects = await _ownerReadDbContext.Projects.Find(e => e.Id == organization.Id).ToListAsync(cancellationToken); | ||
organizationReportDto.ProjectsCount = organizationProjects?.Count ?? 0; | ||
#endregion | ||
|
||
#region Board Reports | ||
var boards = new List<GetBoardDto>(); | ||
var boardsGrpcCall = _getBoardsByOrganizationIdGrpcServiceClient.Handle( | ||
new GetBoardsByOrganizationIdGrpcRequest { OrganizationId = organization.Id } | ||
); | ||
|
||
await foreach (var response in boardsGrpcCall.ResponseStream.ReadAllAsync()) | ||
boards.Add(_mapper.Map<GetBoardDto>(response)); | ||
|
||
organizationReportDto.BoardsCount = boards?.Count ?? 0; | ||
#endregion | ||
|
||
#region Task Reports | ||
long toDoTasksCount = 0; | ||
long doingTasksCount = 0; | ||
long doneTasksCount = 0; | ||
long backlogTasksCount = 0; | ||
|
||
var allBoardTasks = await GetAllTasksForBoardsAsync(boards, cancellationToken); | ||
|
||
foreach (var boardTask in allBoardTasks) | ||
{ | ||
foreach (var cardTask in boardTask.CardTasks) | ||
{ | ||
foreach (var task in cardTask.Tasks) | ||
{ | ||
switch (task.CardType) | ||
{ | ||
case BoardCardType.ToDo: | ||
toDoTasksCount++; | ||
break; | ||
case BoardCardType.Doing: | ||
doingTasksCount++; | ||
break; | ||
case BoardCardType.Done: | ||
doneTasksCount++; | ||
break; | ||
case BoardCardType.Backlog: | ||
backlogTasksCount++; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
organizationReportDto.ToDoTasksCount = toDoTasksCount; | ||
organizationReportDto.DoingTasksCount = doingTasksCount; | ||
organizationReportDto.DoneTasksCount = doneTasksCount; | ||
organizationReportDto.BacklogTasksCount = backlogTasksCount; | ||
|
||
|
||
#endregion | ||
|
||
return _mapper.Map<OrganizationReportDto>(organizationReportDto); | ||
} | ||
|
||
#endregion | ||
|
||
#region Private Methods | ||
private async Task<IEnumerable<BoardTasksViewModel>> GetAllTasksForBoardsAsync(IEnumerable<GetBoardDto> boards, CancellationToken cancellationToken) | ||
{ | ||
var boardTasks = new List<BoardTasksViewModel>(); | ||
|
||
foreach (var board in boards) | ||
{ | ||
var cards = await GetCardsAsync(board.Id, cancellationToken); | ||
var cardTasks = new List<CardDetailsViewModel>(); | ||
|
||
foreach (var card in cards) | ||
{ | ||
var tasks = await GetTasksAsync(card.Card.Id); | ||
cardTasks.Add(new CardDetailsViewModel | ||
{ | ||
Card = card.Card, | ||
Tasks = tasks | ||
}); | ||
} | ||
|
||
boardTasks.Add(new BoardTasksViewModel | ||
{ | ||
Board = board, | ||
CardTasks = cardTasks | ||
}); | ||
} | ||
|
||
return boardTasks; | ||
} | ||
|
||
public class BoardTasksViewModel | ||
{ | ||
public GetBoardDto Board { get; set; } | ||
public IEnumerable<CardDetailsViewModel> CardTasks { get; set; } | ||
} | ||
|
||
private async Task<IEnumerable<CardDetailsViewModel>> GetCardsAsync(string boardId, CancellationToken cancellationToken) | ||
{ | ||
var cards = new List<CardDetailsViewModel>(); | ||
|
||
var cardsGrpcCall = _getCardsByBoardIdGrpcServiceClient.Handle(new GetCardsByBoardIdGrpcRequest { BoardId = boardId }); | ||
|
||
while (await cardsGrpcCall.ResponseStream.MoveNext(cancellationToken)) | ||
{ | ||
var currentCardGrpcResponse = cardsGrpcCall.ResponseStream.Current; | ||
|
||
cards.Add( | ||
new CardDetailsViewModel { Card = MapToCard(currentCardGrpcResponse), Tasks = await GetTasksAsync(currentCardGrpcResponse.Id), } | ||
); | ||
} | ||
|
||
return cards.AsEnumerable(); | ||
} | ||
|
||
private async Task<IEnumerable<GetTaskDto>> GetTasksAsync(string cardId) | ||
{ | ||
var tasks = new List<GetTaskDto>(); | ||
|
||
var tasksGrpcCall = _getTasksByCardIdGrpcServiceClient.Handle(new GetTasksByCardIdGrpcRequest { CardId = cardId }); | ||
|
||
await foreach (var response in tasksGrpcCall.ResponseStream.ReadAllAsync()) | ||
tasks.Add(_mapper.Map<GetTaskDto>(response)); | ||
|
||
return tasks; | ||
} | ||
|
||
private GetCardDto MapToCard(GetCardGrpcResponse cardGrpcResponse) | ||
{ | ||
return _mapper.Map<GetCardDto>(cardGrpcResponse); | ||
} | ||
|
||
#endregion | ||
} |
15 changes: 15 additions & 0 deletions
15
....Api/Features/Organizations/GetOrganizationReportById/GetOrganizationReportByIdRequest.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,15 @@ | ||
using TaskoMask.BuildingBlocks.Application.Queries; | ||
using TaskoMask.BuildingBlocks.Contracts.Dtos.Organizations; | ||
|
||
namespace TaskoMask.Services.Owners.Read.Api.Features.Organizations.GetOrganizationReportById; | ||
|
||
|
||
public class GetOrganizationReportByIdRequest : BaseQuery<OrganizationReportDto> | ||
{ | ||
public GetOrganizationReportByIdRequest(string id) | ||
{ | ||
Id = id; | ||
} | ||
|
||
public string Id { get; } | ||
} |
31 changes: 31 additions & 0 deletions
31
...Features/Organizations/GetOrganizationReportById/GetOrganizationReportByIdRestEndpoint.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,31 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using System.Threading.Tasks; | ||
using TaskoMask.BuildingBlocks.Application.Bus; | ||
using TaskoMask.BuildingBlocks.Contracts.Dtos.Organizations; | ||
using TaskoMask.BuildingBlocks.Contracts.Helpers; | ||
using TaskoMask.BuildingBlocks.Contracts.Services; | ||
using TaskoMask.BuildingBlocks.Web.MVC.Controllers; | ||
|
||
namespace TaskoMask.Services.Owners.Read.Api.Features.Organizations.GetOrganizationReportById; | ||
|
||
|
||
|
||
[Authorize("user-read-access")] | ||
[Tags("Organizations")] | ||
public class GetOrganizationReportByIdRestEndpoint : BaseApiController | ||
{ | ||
public GetOrganizationReportByIdRestEndpoint(IAuthenticatedUserService authenticatedUserService, IInMemoryBus inMemoryBus) | ||
: base(authenticatedUserService, inMemoryBus) { } | ||
|
||
/// <summary> | ||
/// get organization Report | ||
/// </summary> | ||
[HttpGet] | ||
[Route("organizations/report")] | ||
public async Task<Result<OrganizationReportDto>> Get(string organizationId) | ||
{ | ||
return await _inMemoryBus.SendQuery(new GetOrganizationReportByIdRequest(organizationId)); | ||
} | ||
} |
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
Oops, something went wrong.