Files
ColaFlow/colaflow-api/src/ColaFlow.API/Extensions/ModuleExtensions.cs
Yaojia Wang 2466cd4020 feat(backend): Add AuditLog repository interface and implementation
Implement repository pattern for AuditLog entity for Sprint 2 Story 1 Task 2.

Changes:
- Created IAuditLogRepository interface with 6 query methods
- Implemented AuditLogRepository with efficient querying
- Registered repository in DI container
- All queries use AsNoTracking for read-only operations

Query Methods:
- GetByIdAsync: Get single audit log by ID
- GetByEntityAsync: Get audit history for specific entity
- GetByUserAsync: Get user activity with pagination
- GetRecentAsync: Get recent audit logs
- AddAsync: Add new audit log
- GetCountAsync: Get total audit log count

Performance:
- All queries automatically filtered by TenantId (Global Query Filter)
- Efficient use of composite indexes
- AsNoTracking for read-only operations

Testing:
- All tests passing (192 domain + 113 identity + 8 arch + 32 app + 12 infra = 357 tests)
- No compilation errors
- Zero test failures

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-04 23:14:41 +01:00

119 lines
5.1 KiB
C#

using Microsoft.EntityFrameworkCore;
using FluentValidation;
using MediatR;
using ColaFlow.Modules.ProjectManagement.Application.Behaviors;
using ColaFlow.Modules.ProjectManagement.Application.Commands.CreateProject;
using ColaFlow.Modules.ProjectManagement.Domain.Repositories;
using ColaFlow.Modules.ProjectManagement.Infrastructure.Persistence;
using ColaFlow.Modules.ProjectManagement.Infrastructure.Repositories;
using ColaFlow.Modules.IssueManagement.Application.Commands.CreateIssue;
using ColaFlow.Modules.IssueManagement.Infrastructure.Persistence;
using ColaFlow.Modules.IssueManagement.Infrastructure.Persistence.Repositories;
using Microsoft.Extensions.Hosting;
namespace ColaFlow.API.Extensions;
/// <summary>
/// Extension methods for registering modules
/// </summary>
public static class ModuleExtensions
{
/// <summary>
/// Register ProjectManagement Module
/// </summary>
public static IServiceCollection AddProjectManagementModule(
this IServiceCollection services,
IConfiguration configuration,
IHostEnvironment? environment = null)
{
// Only register PostgreSQL DbContext in non-Testing environments
// In Testing environment, WebApplicationFactory will register InMemory provider
if (environment == null || environment.EnvironmentName != "Testing")
{
// Register DbContext
var connectionString = configuration.GetConnectionString("PMDatabase");
services.AddDbContext<PMDbContext>(options =>
options.UseNpgsql(connectionString));
}
// Register HTTP Context Accessor (for tenant context)
services.AddHttpContextAccessor();
// Register Tenant Context (for multi-tenant isolation)
services.AddScoped<ColaFlow.Modules.ProjectManagement.Application.Common.Interfaces.ITenantContext,
ColaFlow.Modules.ProjectManagement.Infrastructure.Services.TenantContext>();
// Register repositories
services.AddScoped<IProjectRepository, ProjectRepository>();
services.AddScoped<IAuditLogRepository, ColaFlow.Modules.ProjectManagement.Infrastructure.Repositories.AuditLogRepository>();
services.AddScoped<IUnitOfWork, ColaFlow.Modules.ProjectManagement.Infrastructure.Persistence.UnitOfWork>();
// Register services
services.AddScoped<ColaFlow.Modules.ProjectManagement.Application.Services.IProjectPermissionService,
ColaFlow.Modules.ProjectManagement.Infrastructure.Services.ProjectPermissionService>();
// Register MediatR handlers from Application assembly (v13.x syntax)
services.AddMediatR(cfg =>
{
cfg.LicenseKey = configuration["MediatR:LicenseKey"];
cfg.RegisterServicesFromAssembly(typeof(CreateProjectCommand).Assembly);
});
// Register FluentValidation validators
services.AddValidatorsFromAssembly(typeof(CreateProjectCommand).Assembly);
// Register pipeline behaviors
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
Console.WriteLine("[ProjectManagement] Module registered");
return services;
}
/// <summary>
/// Register IssueManagement Module
/// </summary>
public static IServiceCollection AddIssueManagementModule(
this IServiceCollection services,
IConfiguration configuration,
IHostEnvironment? environment = null)
{
// Only register PostgreSQL DbContext in non-Testing environments
if (environment == null || environment.EnvironmentName != "Testing")
{
// Register DbContext
var connectionString = configuration.GetConnectionString("IMDatabase");
services.AddDbContext<IssueManagementDbContext>(options =>
options.UseNpgsql(connectionString));
}
// Register HTTP Context Accessor (for tenant context)
services.AddHttpContextAccessor();
// Register Tenant Context (for multi-tenant isolation)
services.AddScoped<ColaFlow.Modules.IssueManagement.Infrastructure.Services.ITenantContext,
ColaFlow.Modules.IssueManagement.Infrastructure.Services.TenantContext>();
// Register repositories
services.AddScoped<ColaFlow.Modules.IssueManagement.Domain.Repositories.IIssueRepository, IssueRepository>();
services.AddScoped<ColaFlow.Modules.IssueManagement.Domain.Repositories.IUnitOfWork, ColaFlow.Modules.IssueManagement.Infrastructure.Persistence.Repositories.UnitOfWork>();
// Register MediatR handlers from Application assembly
services.AddMediatR(cfg =>
{
cfg.LicenseKey = configuration["MediatR:LicenseKey"];
cfg.RegisterServicesFromAssembly(typeof(CreateIssueCommand).Assembly);
});
// Register FluentValidation validators
services.AddValidatorsFromAssembly(typeof(CreateIssueCommand).Assembly);
// Register pipeline behaviors
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ColaFlow.Modules.IssueManagement.Application.Behaviors.ValidationBehavior<,>));
Console.WriteLine("[IssueManagement] Module registered");
return services;
}
}