Skip to content

Commit

Permalink
Move IdentityServer To Separate & Shared Project
Browse files Browse the repository at this point in the history
  • Loading branch information
phongnguyend committed Dec 13, 2022
1 parent 631d18c commit 7c3476a
Show file tree
Hide file tree
Showing 1,317 changed files with 13,307 additions and 76,834 deletions.
9 changes: 0 additions & 9 deletions .github/workflows/.net-build-microservices.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ jobs:
- name: Publish Identity.Api
run: dotnet publish Services.Identity/ClassifiedAds.Services.Identity.Api/*.csproj --configuration Release

- name: Publish Identity.AuthServer
run: dotnet publish Services.Identity/ClassifiedAds.Services.Identity.AuthServer/*.csproj --configuration Release

- name: Publish Identity.Grpc
run: dotnet publish Services.Identity/ClassifiedAds.Services.Identity.Grpc/*.csproj --configuration Release

Expand Down Expand Up @@ -78,12 +75,6 @@ jobs:
name: ClassifiedAds.Services.Identity.Api
path: src/Microservices/Services.Identity/ClassifiedAds.Services.Identity.Api/bin/Release/net6.0/publish

- name: Upload ClassifiedAds.Services.Identity.AuthServer
uses: actions/[email protected]
with:
name: ClassifiedAds.Services.Identity.AuthServer
path: src/Microservices/Services.Identity/ClassifiedAds.Services.Identity.AuthServer/bin/Release/net6.0/publish

- name: Upload ClassifiedAds.Services.Identity.Grpc
uses: actions/[email protected]
with:
Expand Down
9 changes: 0 additions & 9 deletions .github/workflows/.net-build-modularmonolith.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ jobs:
- name: Publish ClassifiedAds.BackgroundServer
run: dotnet publish ClassifiedAds.BackgroundServer/*.csproj --configuration Release

- name: Publish ClassifiedAds.IdentityServer
run: dotnet publish ClassifiedAds.IdentityServer/*.csproj --configuration Release

- name: Publish ClassifiedAds.Migrator
run: dotnet publish ClassifiedAds.Migrator/*.csproj --configuration Release

Expand All @@ -45,12 +42,6 @@ jobs:
name: ClassifiedAds.BackgroundServer
path: src/ModularMonolith/ClassifiedAds.BackgroundServer/bin/Release/net6.0/publish

- name: Upload ClassifiedAds.IdentityServer
uses: actions/[email protected]
with:
name: ClassifiedAds.IdentityServer
path: src/ModularMonolith/ClassifiedAds.IdentityServer/bin/Release/net6.0/publish

- name: Upload ClassifiedAds.Migrator
uses: actions/[email protected]
with:
Expand Down
9 changes: 0 additions & 9 deletions .github/workflows/.net-build-monolith.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ jobs:
- name: Publish ClassifiedAds.WebAPI
run: dotnet publish ClassifiedAds.WebAPI/*.csproj --configuration Release

- name: Publish ClassifiedAds.IdentityServer
run: dotnet publish ClassifiedAds.IdentityServer/*.csproj --configuration Release

- name: Publish ClassifiedAds.WebMVC
run: dotnet publish ClassifiedAds.WebMVC/*.csproj --configuration Release

Expand Down Expand Up @@ -74,12 +71,6 @@ jobs:
with:
name: ClassifiedAds.WebAPI
path: src/Monolith/ClassifiedAds.WebAPI/bin/Release/net6.0/publish

- name: Upload ClassifiedAds.IdentityServer
uses: actions/[email protected]
with:
name: ClassifiedAds.IdentityServer
path: src/Monolith/ClassifiedAds.IdentityServer/bin/Release/net6.0/publish

- name: Upload ClassifiedAds.WebMVC
uses: actions/[email protected]
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Ignore the Migrations folder for now since this repo is for learning and testing purposes
## We don't want to mess things up with lot of migration files anytime we make changes to the database structure.
/src/Monolith/ClassifiedAds.Persistence/Migrations/
/src/IdentityServer/ClassifiedAds.Migrator/Migrations/
/src/Monolith/ClassifiedAds.Migrator/Migrations/
/src/ModularMonolith/ClassifiedAds.Migrator/Migrations/
/src/Microservices/Services.AuditLog/ClassifiedAds.Services.AuditLog.Api/Migrations/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using ClassifiedAds.Application;
using ClassifiedAds.Application.EmailMessages.Services;
using ClassifiedAds.Application.EventLogs;
using ClassifiedAds.Application.Products.Services;
using ClassifiedAds.Application.SmsMessages.Services;
using ClassifiedAds.Application.Users.Services;
using ClassifiedAds.Domain.Entities;
using ClassifiedAds.Domain.Events;
using System;
using System.Linq;
using System.Reflection;

namespace Microsoft.Extensions.DependencyInjection
{
public static class ApplicationServicesExtensions
{
public static IServiceCollection AddApplicationServices(this IServiceCollection services, Action<Type, Type, ServiceLifetime> configureInterceptor = null)
{
DomainEvents.RegisterHandlers(Assembly.GetExecutingAssembly(), services);

services
.AddScoped<IDomainEvents, DomainEvents>()
.AddScoped(typeof(ICrudService<>), typeof(CrudService<>))
.AddScoped<IUserService, UserService>()
.AddScoped<IProductService, ProductService>()
.AddScoped<EmailMessageService>()
.AddScoped<SmsMessageService>()
.AddScoped<PublishEventService>();

if (configureInterceptor != null)
{
var aggregateRootTypes = typeof(IAggregateRoot).Assembly.GetTypes().Where(x => x.BaseType == typeof(Entity<Guid>) && x.GetInterfaces().Contains(typeof(IAggregateRoot))).ToList();
foreach (var type in aggregateRootTypes)
{
configureInterceptor(typeof(ICrudService<>).MakeGenericType(type), typeof(CrudService<>).MakeGenericType(type), ServiceLifetime.Scoped);
}

configureInterceptor(typeof(IUserService), typeof(UserService), ServiceLifetime.Scoped);
configureInterceptor(typeof(IProductService), typeof(ProductService), ServiceLifetime.Scoped);
}

return services;
}

public static IServiceCollection AddMessageHandlers(this IServiceCollection services)
{
services.AddScoped<Dispatcher>();

var assemblyTypes = Assembly.GetExecutingAssembly().GetTypes();

foreach (var type in assemblyTypes)
{
var handlerInterfaces = type.GetInterfaces()
.Where(Utils.IsHandlerInterface)
.ToList();

if (!handlerInterfaces.Any())
{
continue;
}

var handlerFactory = new HandlerFactory(type);
foreach (var interfaceType in handlerInterfaces)
{
services.AddTransient(interfaceType, provider => handlerFactory.Create(provider, interfaceType));
}
}

var aggregateRootTypes = typeof(IAggregateRoot).Assembly.GetTypes().Where(x => x.BaseType == typeof(Entity<Guid>) && x.GetInterfaces().Contains(typeof(IAggregateRoot))).ToList();

var genericHandlerTypes = new[]
{
typeof(GetEntititesQueryHandler<>),
typeof(GetEntityByIdQueryHandler<>),
typeof(AddOrUpdateEntityCommandHandler<>),
typeof(DeleteEntityCommandHandler<>),
};

foreach (var aggregateRootType in aggregateRootTypes)
{
foreach (var genericHandlerType in genericHandlerTypes)
{
var handlerType = genericHandlerType.MakeGenericType(aggregateRootType);
var handlerInterfaces = handlerType.GetInterfaces();

var handlerFactory = new HandlerFactory(handlerType);
foreach (var interfaceType in handlerInterfaces)
{
services.AddTransient(interfaceType, provider => handlerFactory.Create(provider, interfaceType));
}
}
}

return services;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace ClassifiedAds.Application.AuditLogEntries.DTOs
{
public class AuditLogEntryDTO
{
public Guid Id { get; set; }

public Guid UserId { get; set; }

public string UserName { get; set; }

public string Action { get; set; }

public string ObjectId { get; set; }

public string Log { get; set; }

public DateTimeOffset CreatedDateTime { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using ClassifiedAds.Application.AuditLogEntries.DTOs;
using ClassifiedAds.Domain.Repositories;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace ClassifiedAds.Application.AuditLogEntries.Queries
{
public class GetAuditEntriesQuery : AuditLogEntryQueryOptions, IQuery<List<AuditLogEntryDTO>>
{
}

internal class GetAuditEntriesQueryHandler : IQueryHandler<GetAuditEntriesQuery, List<AuditLogEntryDTO>>
{
private readonly IAuditLogEntryRepository _auditLogEntryRepository;
private readonly IUserRepository _userRepository;

public GetAuditEntriesQueryHandler(IAuditLogEntryRepository auditLogEntryRepository, IUserRepository userRepository)
{
_auditLogEntryRepository = auditLogEntryRepository;
_userRepository = userRepository;
}

public async Task<List<AuditLogEntryDTO>> HandleAsync(GetAuditEntriesQuery query, CancellationToken cancellationToken = default)
{
var auditLogs = _auditLogEntryRepository.Get(query);
var users = _userRepository.GetAll();

var rs = auditLogs.Join(users, x => x.UserId, y => y.Id,
(x, y) => new AuditLogEntryDTO
{
Id = x.Id,
UserId = x.UserId,
Action = x.Action,
ObjectId = x.ObjectId,
Log = x.Log,
CreatedDateTime = x.CreatedDateTime,
UserName = y.UserName,
});

return await _userRepository.ToListAsync(rs.OrderByDescending(x => x.CreatedDateTime));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using ClassifiedAds.Application.AuditLogEntries.DTOs;
using ClassifiedAds.Application.Common.DTOs;
using ClassifiedAds.CrossCuttingConcerns.ExtensionMethods;
using ClassifiedAds.Domain.Repositories;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace ClassifiedAds.Application.AuditLogEntries.Queries
{
public class GetPagedAuditEntriesQuery : AuditLogEntryQueryOptions, IQuery<Paged<AuditLogEntryDTO>>
{
public int Page { get; set; }

public int PageSize { get; set; }
}

internal class GetPagedAuditEntriesQueryHandler : IQueryHandler<GetPagedAuditEntriesQuery, Paged<AuditLogEntryDTO>>
{
private readonly IAuditLogEntryRepository _auditLogEntryRepository;
private readonly IUserRepository _userRepository;

public GetPagedAuditEntriesQueryHandler(IAuditLogEntryRepository auditLogEntryRepository, IUserRepository userRepository)
{
_auditLogEntryRepository = auditLogEntryRepository;
_userRepository = userRepository;
}

public async Task<Paged<AuditLogEntryDTO>> HandleAsync(GetPagedAuditEntriesQuery queryOptions, CancellationToken cancellationToken = default)
{
var query = _auditLogEntryRepository.Get(queryOptions);
var users = _userRepository.GetAll();

var result = new Paged<AuditLogEntryDTO>
{
TotalItems = query.Count(),
};

var auditLogs = query.OrderByDescending(x => x.CreatedDateTime)
.Paged(queryOptions.Page, queryOptions.PageSize);

var rs = auditLogs.Join(users, x => x.UserId, y => y.Id,
(x, y) => new AuditLogEntryDTO
{
Id = x.Id,
UserId = x.UserId,
Action = x.Action,
ObjectId = x.ObjectId,
Log = x.Log,
CreatedDateTime = x.CreatedDateTime,
UserName = y.UserName,
});

result.Items = await _userRepository.ToListAsync(rs);

return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AnalysisMode>Recommended</AnalysisMode>
<AnalysisModeSecurity>All</AnalysisModeSecurity>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ClassifiedAds.Domain\ClassifiedAds.Domain.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using ClassifiedAds.Domain.Entities;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace ClassifiedAds.Application
{
public class AddEntityCommand<TEntity> : ICommand
where TEntity : Entity<Guid>, IAggregateRoot
{
public AddEntityCommand(TEntity entity)
{
Entity = entity;
}

public TEntity Entity { get; set; }
}

internal class AddEntityCommandHandler<TEntity> : ICommandHandler<AddEntityCommand<TEntity>>
where TEntity : Entity<Guid>, IAggregateRoot
{
private readonly ICrudService<TEntity> _crudService;

public AddEntityCommandHandler(ICrudService<TEntity> crudService)
{
_crudService = crudService;
}

public async Task HandleAsync(AddEntityCommand<TEntity> command, CancellationToken cancellationToken = default)
{
await _crudService.AddAsync(command.Entity);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using ClassifiedAds.Domain.Entities;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace ClassifiedAds.Application
{
public class AddOrUpdateEntityCommand<TEntity> : ICommand
where TEntity : Entity<Guid>, IAggregateRoot
{
public AddOrUpdateEntityCommand(TEntity entity)
{
Entity = entity;
}

public TEntity Entity { get; set; }
}

internal class AddOrUpdateEntityCommandHandler<TEntity> : ICommandHandler<AddOrUpdateEntityCommand<TEntity>>
where TEntity : Entity<Guid>, IAggregateRoot
{
private readonly ICrudService<TEntity> _crudService;

public AddOrUpdateEntityCommandHandler(ICrudService<TEntity> crudService)
{
_crudService = crudService;
}

public async Task HandleAsync(AddOrUpdateEntityCommand<TEntity> command, CancellationToken cancellationToken = default)
{
await _crudService.AddOrUpdateAsync(command.Entity);
}
}
}
Loading

0 comments on commit 7c3476a

Please sign in to comment.