Skip to content

Commit

Permalink
Merge pull request #53 from TunNetCom/ProfileUser
Browse files Browse the repository at this point in the history
add project producer consumer
  • Loading branch information
MarwenSaad authored Sep 14, 2024
2 parents e675c47 + 4ff79d6 commit 4fdf718
Show file tree
Hide file tree
Showing 55 changed files with 429 additions and 303 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using AzureDevopsService.Application.AzureDevopsExternalResourceService.OrganizationProjectService;
using AzureDevopsService.Application.AzureDevopsExternalResourceService.ProfileUserService;
using AzureDevopsService.Application.AzureDevopsExternalResourceService.WorkItem;
using AzureDevopsService.Contracts.AzureRequestResourceModel;
using AzureDevopsService.Contracts.AzureResponceModel;

namespace AzureDevopsService.API;
namespace AzureDevopsService.API;

public static class AzureDevopsEndpoints
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
global using AzureDevopsService.API;
global using AzureDevopsService.Application;
global using AzureDevopsService.Application.AzureDevopsExternalResourceService.OrganizationProjectService;
global using AzureDevopsService.Application.AzureDevopsExternalResourceService.ProfileUserService;
global using AzureDevopsService.Application.AzureDevopsExternalResourceService.WorkItem;
global using AzureDevopsService.Contracts.AzureRequestResourceModel;
global using AzureDevopsService.Contracts.AzureResponceModel;
global using AzureDevopsService.Application;
global using AzureDevopsService.Contracts.Settings;
global using FluentResults;
global using FluentValidation;
global using MediatR;
Expand Down
2 changes: 0 additions & 2 deletions src/AzureDevopsService/AzureDevopsService.API/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using AzureDevopsService.Contracts.Settings;

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public interface IProjectService
{
Task<OneOf<AllProjectResponce?, CustomProblemDetailsResponce?>> AllProjectUnderOrganization(AllProjectUnderOrganizationRequest request);
Task<OneOf<AllProjectResponce, CustomProblemDetailsResponce>> AllProjectUnderOrganization(AllProjectUnderOrganizationRequest request);
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
namespace AzureDevopsService.Application.AzureDevopsExternalResourceService.OrganizationProjectService
namespace AzureDevopsService.Application.AzureDevopsExternalResourceService.OrganizationProjectService;

public class ProjectService(HttpClient httpClient, ILogger<ProjectService> logger) : IProjectService
{
public class ProjectService(HttpClient httpClient, ILogger<ProjectService> logger) : IProjectService
{
private readonly HttpClient _httpClient = httpClient;
private readonly ILogger<ProjectService> _logger = logger;
private readonly HttpClient _httpClient = httpClient;
private readonly ILogger<ProjectService> _logger = logger;

public async Task<OneOf<AllProjectResponce?, CustomProblemDetailsResponce?>> AllProjectUnderOrganization(AllProjectUnderOrganizationRequest request)
{
HttpClientHelper.SetAuthHeader(_httpClient, request.Path);
public async Task<OneOf<AllProjectResponce, CustomProblemDetailsResponce>> AllProjectUnderOrganization(AllProjectUnderOrganizationRequest request)
{
HttpClientHelper.SetAuthHeader(_httpClient, request.Path);

HttpResponseMessage projectsResult = await _httpClient.GetAsync("_apis/profile/profiles/me?api-version=7.0");
HttpResponseMessage projectsResult = await _httpClient.GetAsync("_apis/profile/profiles/me?api-version=7.0");

if (projectsResult.StatusCode == HttpStatusCode.OK)
{
AllProjectResponce? projects = await projectsResult.Content.ReadFromJsonAsync<AllProjectResponce>();
#pragma warning disable CS8602 // Dereference of a possibly null reference.
projects.Path = request.Path;
#pragma warning restore CS8602 // Dereference of a possibly null reference.
projects.Email = request.Email;
return projects;
}
if (projectsResult.StatusCode == HttpStatusCode.OK)
{
AllProjectResponce projects = await projectsResult.Content.ReadFromJsonAsync<AllProjectResponce>();
projects.Path = request.Path;
projects.Email = request.Email;

_logger.LogError(await projectsResult.Content.ReadAsStringAsync());
return new CustomProblemDetailsResponce()
{
Path = request.Path,
Email = request.Email,
Status = (int)projectsResult.StatusCode,
Detail = AzureResponseMessage.VerifyAzureDevOpsKeyOrOrgName,
};
return projects;
}

_logger.LogError(await projectsResult.Content.ReadAsStringAsync());
return new CustomProblemDetailsResponce()
{
Path = request.Path,
Email = request.Email,
Status = (int)projectsResult.StatusCode,
Detail = AzureResponseMessage.VerifyAzureDevOpsKeyOrOrgName,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
{
public interface IUserProfileApiClient
{
Task<OneOf<UserProfile?, CustomProblemDetailsResponce?>> GetAdminInfo(BaseRequest request);
Task<OneOf<UserProfile, CustomProblemDetailsResponce>> GetAdminInfo(BaseRequest request);

Task<OneOf<UserAccount?, CustomProblemDetailsResponce?>> GeUserOrganizations(GetUserOrganizationRequest request);
Task<OneOf<UserAccount, CustomProblemDetailsResponce>> GeUserOrganizations(GetUserOrganizationRequest request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ public class UserProfileApiClient(HttpClient httpClient, ILogger<UserProfileApiC
private readonly HttpClient _httpClient = httpClient;
private readonly ILogger<UserProfileApiClient> _logger = logger;

public async Task<OneOf<UserProfile?, CustomProblemDetailsResponce?>> GetAdminInfo(BaseRequest request)
public async Task<OneOf<UserProfile, CustomProblemDetailsResponce>> GetAdminInfo(BaseRequest request)
{
HttpClientHelper.SetAuthHeader(_httpClient, request.Path);

HttpResponseMessage userProfileResult = await _httpClient.GetAsync("_apis/profile/profiles/me?api-version=7.0");

if (userProfileResult.StatusCode == HttpStatusCode.OK)
{
UserProfile? user = await userProfileResult.Content.ReadFromJsonAsync<UserProfile>();
UserProfile user = await userProfileResult.Content.ReadFromJsonAsync<UserProfile>();

#pragma warning disable CS8602 // Dereference of a possibly null reference.
user.Path = request.Path;
#pragma warning restore CS8602 // Dereference of a possibly null reference.
user.Email = request.Email;

return user;
}

Expand All @@ -32,18 +31,16 @@ public class UserProfileApiClient(HttpClient httpClient, ILogger<UserProfileApiC
};
}

public async Task<OneOf<UserAccount?, CustomProblemDetailsResponce?>> GeUserOrganizations(GetUserOrganizationRequest request)
public async Task<OneOf<UserAccount, CustomProblemDetailsResponce>> GeUserOrganizations(GetUserOrganizationRequest request)
{
HttpClientHelper.SetAuthHeader(_httpClient, request.Path);

HttpResponseMessage userOrganizationResult = await _httpClient.GetAsync($"_apis/accounts?memberId={request.MemberId}&api-version=7.0");

if (userOrganizationResult.StatusCode == HttpStatusCode.OK)
{
UserAccount? responce = await userOrganizationResult.Content.ReadFromJsonAsync<UserAccount>();
#pragma warning disable CS8602 // Dereference of a possibly null reference.
UserAccount responce = await userOrganizationResult.Content.ReadFromJsonAsync<UserAccount>();
responce.Path = request.Path;
#pragma warning restore CS8602 // Dereference of a possibly null reference.
responce.Email = request.Email;

return responce;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public interface IWorkItemExternalService
{
Task<OneOf<WiqlResponses?, WiqlBadRequestResponce?>> GetWorkItemByUser(WorkItemRequest resource);
Task<OneOf<WiqlResponses, WiqlBadRequestResponce>> GetWorkItemByUser(WorkItemRequest resource);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,41 @@ public class WorkItemExternalService(HttpClient httpClient, ILogger<WorkItemExte
private readonly HttpClient _httpClient = httpClient;
private readonly ILogger<WorkItemExternalService> _logger = logger;

public async Task<OneOf<WiqlResponses?, WiqlBadRequestResponce?>> GetWorkItemByUser(WorkItemRequest resource)
public async Task<OneOf<WiqlResponses, WiqlBadRequestResponce>> GetWorkItemByUser(WorkItemRequest resource)
{
WiqlRequest wiqlRequest = WorkItemHelper.FillGetWorkItemByUser(resource);

HttpClientHelper.SetAuthHeader(_httpClient, resource.Path);

using HttpResponseMessage response = await _httpClient.PostAsJsonAsync(
using HttpResponseMessage workItemResponse = await _httpClient.PostAsJsonAsync(
@$"/{wiqlRequest.Organization}/{wiqlRequest.Project}/{wiqlRequest.Team}/_apis/wit/wiql?api-version={wiqlRequest.ApiVersion}",
wiqlRequest);

if (response.StatusCode == HttpStatusCode.OK)
if (workItemResponse.StatusCode == HttpStatusCode.OK)
{
WiqlResponses? wiqlResponses = await response.Content.ReadFromJsonAsync<WiqlResponses>();
#pragma warning disable CS8602 // Dereference of a possibly null reference.
WiqlResponses? wiqlResponses = await workItemResponse.Content.ReadFromJsonAsync<WiqlResponses>();
wiqlResponses.Email = wiqlRequest.Email;
#pragma warning restore CS8602 // Dereference of a possibly null reference.
wiqlResponses.Path = wiqlRequest.Path;
return wiqlResponses;
}

_logger.LogError(await response.Content.ReadAsStringAsync());
_logger.LogError(await workItemResponse.Content.ReadAsStringAsync());

WiqlBadRequestResponce? wiqlBadResponses = await response.Content.ReadFromJsonAsync<WiqlBadRequestResponce>();
#pragma warning disable CS8602 // Dereference of a possibly null reference.
wiqlBadResponses.Path = wiqlRequest.Path;
#pragma warning restore CS8602 // Dereference of a possibly null reference.
wiqlBadResponses.Email = wiqlRequest.Email;
return wiqlBadResponses;
if (workItemResponse.StatusCode == HttpStatusCode.BadRequest)
{
WiqlBadRequestResponce wiqlBadResponses = await workItemResponse.Content.ReadFromJsonAsync<WiqlBadRequestResponce>();
wiqlBadResponses.Path = wiqlRequest.Path;
wiqlBadResponses.Email = wiqlRequest.Email;

return wiqlBadResponses;
}

return new WiqlBadRequestResponce()
{
Email = resource.Email,
ErrorCode = (int?)workItemResponse.StatusCode,
Message = AzureResponseMessage.WorkItemError,
Path = resource.Path,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MassTransit" Version="8.2.4-develop.1856" />
<PackageReference Include="MassTransit.RabbitMQ" Version="8.2.4-develop.1856" />
<PackageReference Include="MassTransit" Version="8.2.5" />
<PackageReference Include="MassTransit.Newtonsoft" Version="8.2.5" />
<PackageReference Include="MassTransit.RabbitMQ" Version="8.2.5" />
<PackageReference Include="MediatR" Version="12.3.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using AzureDevopsService.Application.Featurs.MessageBroker.Consumer;

namespace AzureDevopsService.Application;
namespace AzureDevopsService.Application;

public static class ExtentionRegistrationService
{
Expand All @@ -10,7 +8,7 @@ public static IServiceCollection AddApplicationService(this IServiceCollection s
_ = services.AddMassTransit(x =>
{
x.SetDefaultEndpointNameFormatter();
_ = x.AddConsumer<ProfileUserConsumer>();
_ = x.AddConsumer<AzureDevopsConsumer>();
x.SetDefaultEndpointNameFormatter();
x.UsingRabbitMq((context, cfg) =>
Expand All @@ -22,11 +20,11 @@ public static IServiceCollection AddApplicationService(this IServiceCollection s
h.Username(rabbitMqSettings.UserName);
h.Password(rabbitMqSettings.Password);
});
cfg.ReceiveEndpoint("ProfileUser", e =>
cfg.UseNewtonsoftJsonSerializer();
cfg.ReceiveEndpoint("AzureDevops", e =>
{
e.SetQueueArgument("x-message-ttl", 60000);
e.ConfigureConsumer<ProfileUserConsumer>(context);
e.ConfigureConsumer<AzureDevopsConsumer>(context);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace AzureDevopsService.Application.Featurs.MessageBroker.Consumer;

public class AzureDevopsConsumer(IMediator mediator) : IConsumer<BaseRequest>, IConsumer<AllProjectUnderOrganizationRequest>, IConsumer<WorkItemRequest>
{
private readonly IMediator _mediator = mediator;

public async Task Consume(ConsumeContext<BaseRequest> context)
{
await _mediator.Send(new ProfileUserCommand(context.Message));
}

public async Task Consume(ConsumeContext<AllProjectUnderOrganizationRequest> context)
{
await _mediator.Send(new ProjectCommand(context.Message));
}

public async Task Consume(ConsumeContext<WorkItemRequest> context)
{
await _mediator.Send(new WorkItemCommand(context.Message));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace AzureDevopsService.Application.Featurs.MessageBroker.Producer.ProfileUser;

public record class ProfileUserCommand(BaseRequest BaseRequest) : IRequest;
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace AzureDevopsService.Application.Featurs.MessageBroker.Producer.ProfileUser;

public class ProfileUserCommandHandler(IUserProfileApiClient userProfileApiClient, ISendEndpointProvider sendEndpointProvider) :
IRequestHandler<ProfileUserCommand>
{
private readonly IUserProfileApiClient _userProfileApiClient = userProfileApiClient;

private readonly ISendEndpointProvider _sendEndpointProvider = sendEndpointProvider;

public async Task Handle(ProfileUserCommand request, CancellationToken cancellationToken)
{
ISendEndpoint endpoint = await _sendEndpointProvider.GetSendEndpoint(new Uri("rabbitmq://rabbitmq/ProfileUserResponce"));

OneOf<UserProfile, CustomProblemDetailsResponce> adminInfoResponse = await _userProfileApiClient.GetAdminInfo(request.BaseRequest);
if (adminInfoResponse.IsT0)
{

OneOf<UserAccount, CustomProblemDetailsResponce> organizationResponce = await _userProfileApiClient.GeUserOrganizations(
new GetUserOrganizationRequest
{
Email = adminInfoResponse!.AsT0!.Email,
MemberId = adminInfoResponse!.AsT0!.Id,
Path = adminInfoResponse!.AsT0!.Path,
});

await endpoint.Send(organizationResponce.AsT0, cancellationToken);
}
else
{
await endpoint.Send(
new CustomProblemDetailsResponce
{
Detail = adminInfoResponse!.AsT1!.Detail,
Email = adminInfoResponse!.AsT1!.Email,
Path = adminInfoResponse!.AsT1!.Path,
Status = adminInfoResponse!.AsT1!.Status,
},
cancellationToken);
}
}
}

This file was deleted.

Loading

0 comments on commit 4fdf718

Please sign in to comment.