feat: Scaffold initial MiningService .NET solution including domain, API, infrastructure, and test projects.
This commit is contained in:
22
services/mining-service-net/Directory.Build.props
Normal file
22
services/mining-service-net/Directory.Build.props
Normal file
@@ -0,0 +1,22 @@
|
||||
<Project>
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<LangVersion>14.0</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<NoWarn>$(NoWarn);1591;CA2017</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<Authors>GoodGo Team</Authors>
|
||||
<Company>GoodGo</Company>
|
||||
<Copyright>© 2026 GoodGo. All rights reserved.</Copyright>
|
||||
<RepositoryType>git</RepositoryType>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
66
services/mining-service-net/Dockerfile
Normal file
66
services/mining-service-net/Dockerfile
Normal file
@@ -0,0 +1,66 @@
|
||||
# Build stage / Giai đoạn build
|
||||
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
|
||||
WORKDIR /src
|
||||
|
||||
# EN: Copy project files for layer caching
|
||||
# VI: Sao chép các file project để tận dụng layer caching
|
||||
COPY ["src/MiningService.API/MiningService.API.csproj", "src/MiningService.API/"]
|
||||
COPY ["src/MiningService.Domain/MiningService.Domain.csproj", "src/MiningService.Domain/"]
|
||||
COPY ["src/MiningService.Infrastructure/MiningService.Infrastructure.csproj", "src/MiningService.Infrastructure/"]
|
||||
COPY ["Directory.Build.props", "./"]
|
||||
|
||||
# EN: Restore dependencies
|
||||
# VI: Khôi phục dependencies
|
||||
RUN dotnet restore "src/MiningService.API/MiningService.API.csproj"
|
||||
|
||||
# EN: Copy all source code
|
||||
# VI: Sao chép toàn bộ source code
|
||||
COPY src/ ./src/
|
||||
|
||||
# EN: Build the application
|
||||
# VI: Build ứng dụng
|
||||
WORKDIR "/src/src/MiningService.API"
|
||||
RUN dotnet build "MiningService.API.csproj" -c Release -o /app/build --no-restore
|
||||
|
||||
# Publish stage / Giai đoạn publish
|
||||
FROM build AS publish
|
||||
RUN dotnet publish "MiningService.API.csproj" -c Release -o /app/publish /p:UseAppHost=false --no-restore
|
||||
|
||||
# Runtime stage / Giai đoạn runtime
|
||||
FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS final
|
||||
WORKDIR /app
|
||||
|
||||
# EN: Create non-root user for security
|
||||
# VI: Tạo user non-root cho bảo mật
|
||||
RUN groupadd -g 1001 dotnetuser && \
|
||||
useradd -u 1001 -g dotnetuser -s /bin/sh dotnetuser
|
||||
|
||||
# EN: Copy published application
|
||||
# VI: Sao chép ứng dụng đã publish
|
||||
COPY --from=publish /app/publish .
|
||||
|
||||
# EN: Change ownership to non-root user
|
||||
# VI: Thay đổi quyền sở hữu sang user non-root
|
||||
RUN chown -R dotnetuser:dotnetuser /app
|
||||
|
||||
# EN: Switch to non-root user
|
||||
# VI: Chuyển sang user non-root
|
||||
USER dotnetuser
|
||||
|
||||
# EN: Expose port
|
||||
# VI: Mở cổng
|
||||
EXPOSE 8080
|
||||
|
||||
# EN: Set environment variables
|
||||
# VI: Thiết lập biến môi trường
|
||||
ENV ASPNETCORE_URLS=http://+:8080
|
||||
ENV ASPNETCORE_ENVIRONMENT=Production
|
||||
|
||||
# EN: Health check
|
||||
# VI: Kiểm tra health
|
||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
|
||||
CMD curl -f http://localhost:8080/health/live || exit 1
|
||||
|
||||
# EN: Start the application
|
||||
# VI: Khởi động ứng dụng
|
||||
ENTRYPOINT ["dotnet", "MiningService.API.dll"]
|
||||
11
services/mining-service-net/MiningService.slnx
Normal file
11
services/mining-service-net/MiningService.slnx
Normal file
@@ -0,0 +1,11 @@
|
||||
<Solution>
|
||||
<Folder Name="/src/">
|
||||
<Project Path="src/MiningService.API/MiningService.API.csproj" />
|
||||
<Project Path="src/MiningService.Domain/MiningService.Domain.csproj" />
|
||||
<Project Path="src/MiningService.Infrastructure/MiningService.Infrastructure.csproj" />
|
||||
</Folder>
|
||||
<Folder Name="/tests/">
|
||||
<Project Path="tests/MiningService.FunctionalTests/MiningService.FunctionalTests.csproj" />
|
||||
<Project Path="tests/MiningService.UnitTests/MiningService.UnitTests.csproj" />
|
||||
</Folder>
|
||||
</Solution>
|
||||
73
services/mining-service-net/docker-compose.yml
Normal file
73
services/mining-service-net/docker-compose.yml
Normal file
@@ -0,0 +1,73 @@
|
||||
version: '3.8'
|
||||
|
||||
# EN: Docker Compose for Mining Service local development
|
||||
# VI: Docker Compose cho phát triển Mining Service local
|
||||
|
||||
services:
|
||||
mining-api:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
container_name: mining-service-api
|
||||
ports:
|
||||
- "5010:8080"
|
||||
environment:
|
||||
- ASPNETCORE_ENVIRONMENT=Development
|
||||
- DATABASE_URL=Host=postgres;Port=5432;Database=mining_db;Username=postgres;Password=postgres
|
||||
- REDIS_URL=redis:6379
|
||||
- RABBITMQ_URL=amqp://guest:guest@rabbitmq:5672
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
redis:
|
||||
condition: service_healthy
|
||||
networks:
|
||||
- mining-network
|
||||
healthcheck:
|
||||
test: ["CMD", "curl", "-f", "http://localhost:8080/health/live"]
|
||||
interval: 30s
|
||||
timeout: 10s
|
||||
retries: 3
|
||||
start_period: 10s
|
||||
|
||||
postgres:
|
||||
image: postgres:16-alpine
|
||||
container_name: mining-postgres
|
||||
environment:
|
||||
POSTGRES_USER: postgres
|
||||
POSTGRES_PASSWORD: postgres
|
||||
POSTGRES_DB: mining_db
|
||||
ports:
|
||||
- "5442:5432"
|
||||
volumes:
|
||||
- postgres_data:/var/lib/postgresql/data
|
||||
networks:
|
||||
- mining-network
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U postgres"]
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 5
|
||||
|
||||
redis:
|
||||
image: redis:7-alpine
|
||||
container_name: mining-redis
|
||||
ports:
|
||||
- "6389:6379"
|
||||
volumes:
|
||||
- redis_data:/data
|
||||
networks:
|
||||
- mining-network
|
||||
healthcheck:
|
||||
test: ["CMD", "redis-cli", "ping"]
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 5
|
||||
|
||||
volumes:
|
||||
postgres_data:
|
||||
redis_data:
|
||||
|
||||
networks:
|
||||
mining-network:
|
||||
driver: bridge
|
||||
7
services/mining-service-net/global.json
Normal file
7
services/mining-service-net/global.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"sdk": {
|
||||
"version": "10.0.101",
|
||||
"rollForward": "latestMinor",
|
||||
"allowPrerelease": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
using System.Diagnostics;
|
||||
using MediatR;
|
||||
|
||||
namespace MiningService.API.Application.Behaviors;
|
||||
|
||||
/// <summary>
|
||||
/// EN: MediatR behavior for logging request handling.
|
||||
/// VI: MediatR behavior để logging việc xử lý request.
|
||||
/// </summary>
|
||||
/// <typeparam name="TRequest">EN: Request type / VI: Loại request</typeparam>
|
||||
/// <typeparam name="TResponse">EN: Response type / VI: Loại response</typeparam>
|
||||
public class LoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
|
||||
where TRequest : IRequest<TResponse>
|
||||
{
|
||||
private readonly ILogger<LoggingBehavior<TRequest, TResponse>> _logger;
|
||||
|
||||
public LoggingBehavior(ILogger<LoggingBehavior<TRequest, TResponse>> logger)
|
||||
{
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
}
|
||||
|
||||
public async Task<TResponse> Handle(
|
||||
TRequest request,
|
||||
RequestHandlerDelegate<TResponse> next,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var requestName = typeof(TRequest).Name;
|
||||
|
||||
_logger.LogInformation(
|
||||
"Handling {RequestName} / Đang xử lý {RequestName}",
|
||||
requestName);
|
||||
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
|
||||
try
|
||||
{
|
||||
var response = await next();
|
||||
|
||||
stopwatch.Stop();
|
||||
|
||||
_logger.LogInformation(
|
||||
"Handled {RequestName} in {ElapsedMs}ms / Đã xử lý {RequestName} trong {ElapsedMs}ms",
|
||||
requestName, stopwatch.ElapsedMilliseconds);
|
||||
|
||||
return response;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
stopwatch.Stop();
|
||||
|
||||
_logger.LogError(ex,
|
||||
"Error handling {RequestName} after {ElapsedMs}ms / Lỗi xử lý {RequestName} sau {ElapsedMs}ms",
|
||||
requestName, stopwatch.ElapsedMilliseconds);
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MiningService.Infrastructure;
|
||||
|
||||
namespace MiningService.API.Application.Behaviors;
|
||||
|
||||
/// <summary>
|
||||
/// EN: MediatR behavior for handling database transactions.
|
||||
/// VI: MediatR behavior để xử lý database transactions.
|
||||
/// </summary>
|
||||
/// <typeparam name="TRequest">EN: Request type / VI: Loại request</typeparam>
|
||||
/// <typeparam name="TResponse">EN: Response type / VI: Loại response</typeparam>
|
||||
public class TransactionBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
|
||||
where TRequest : IRequest<TResponse>
|
||||
{
|
||||
private readonly MiningServiceContext _dbContext;
|
||||
private readonly ILogger<TransactionBehavior<TRequest, TResponse>> _logger;
|
||||
|
||||
public TransactionBehavior(
|
||||
MiningServiceContext dbContext,
|
||||
ILogger<TransactionBehavior<TRequest, TResponse>> logger)
|
||||
{
|
||||
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
}
|
||||
|
||||
public async Task<TResponse> Handle(
|
||||
TRequest request,
|
||||
RequestHandlerDelegate<TResponse> next,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var requestName = typeof(TRequest).Name;
|
||||
|
||||
// EN: Skip transaction for queries (read operations)
|
||||
// VI: Bỏ qua transaction cho queries (các thao tác đọc)
|
||||
if (requestName.EndsWith("Query"))
|
||||
{
|
||||
return await next();
|
||||
}
|
||||
|
||||
// EN: Skip if already in a transaction
|
||||
// VI: Bỏ qua nếu đã trong transaction
|
||||
if (_dbContext.HasActiveTransaction)
|
||||
{
|
||||
return await next();
|
||||
}
|
||||
|
||||
var strategy = _dbContext.Database.CreateExecutionStrategy();
|
||||
|
||||
return await strategy.ExecuteAsync(async () =>
|
||||
{
|
||||
await using var transaction = await _dbContext.BeginTransactionAsync();
|
||||
|
||||
_logger.LogInformation(
|
||||
"Begin transaction {TransactionId} for {RequestName} / Bắt đầu transaction {TransactionId} cho {RequestName}",
|
||||
transaction?.TransactionId, requestName);
|
||||
|
||||
try
|
||||
{
|
||||
var response = await next();
|
||||
|
||||
if (transaction != null)
|
||||
{
|
||||
await _dbContext.CommitTransactionAsync(transaction);
|
||||
|
||||
_logger.LogInformation(
|
||||
"Committed transaction {TransactionId} for {RequestName} / Đã commit transaction {TransactionId} cho {RequestName}",
|
||||
transaction.TransactionId, requestName);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex,
|
||||
"Error during transaction {TransactionId} for {RequestName} / Lỗi trong transaction {TransactionId} cho {RequestName}",
|
||||
transaction?.TransactionId, requestName);
|
||||
|
||||
_dbContext.RollbackTransaction();
|
||||
throw;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace MiningService.API.Application.Behaviors;
|
||||
|
||||
/// <summary>
|
||||
/// EN: MediatR behavior for FluentValidation integration.
|
||||
/// VI: MediatR behavior để tích hợp FluentValidation.
|
||||
/// </summary>
|
||||
/// <typeparam name="TRequest">EN: Request type / VI: Loại request</typeparam>
|
||||
/// <typeparam name="TResponse">EN: Response type / VI: Loại response</typeparam>
|
||||
public class ValidatorBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
|
||||
where TRequest : IRequest<TResponse>
|
||||
{
|
||||
private readonly IEnumerable<IValidator<TRequest>> _validators;
|
||||
private readonly ILogger<ValidatorBehavior<TRequest, TResponse>> _logger;
|
||||
|
||||
public ValidatorBehavior(
|
||||
IEnumerable<IValidator<TRequest>> validators,
|
||||
ILogger<ValidatorBehavior<TRequest, TResponse>> logger)
|
||||
{
|
||||
_validators = validators ?? throw new ArgumentNullException(nameof(validators));
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
}
|
||||
|
||||
public async Task<TResponse> Handle(
|
||||
TRequest request,
|
||||
RequestHandlerDelegate<TResponse> next,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var requestName = typeof(TRequest).Name;
|
||||
|
||||
if (!_validators.Any())
|
||||
{
|
||||
return await next();
|
||||
}
|
||||
|
||||
_logger.LogDebug(
|
||||
"Validating {RequestName} / Đang validate {RequestName}",
|
||||
requestName);
|
||||
|
||||
var context = new ValidationContext<TRequest>(request);
|
||||
|
||||
var validationResults = await Task.WhenAll(
|
||||
_validators.Select(v => v.ValidateAsync(context, cancellationToken)));
|
||||
|
||||
var failures = validationResults
|
||||
.SelectMany(r => r.Errors)
|
||||
.Where(f => f != null)
|
||||
.ToList();
|
||||
|
||||
if (failures.Count != 0)
|
||||
{
|
||||
_logger.LogWarning(
|
||||
"Validation failed for {RequestName} with {ErrorCount} errors / Validation thất bại cho {RequestName} với {ErrorCount} lỗi",
|
||||
requestName, failures.Count);
|
||||
|
||||
throw new ValidationException(failures);
|
||||
}
|
||||
|
||||
return await next();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using MediatR;
|
||||
using MiningService.Domain.AggregatesModel.MinerAggregate;
|
||||
using MiningService.Domain.Exceptions;
|
||||
|
||||
namespace MiningService.API.Application.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Command to suspend a miner.
|
||||
/// VI: Command để tạm ngừng thợ đào.
|
||||
/// </summary>
|
||||
public record SuspendMinerCommand(Guid MinerId, string Reason) : IRequest<bool>;
|
||||
|
||||
public class SuspendMinerCommandHandler : IRequestHandler<SuspendMinerCommand, bool>
|
||||
{
|
||||
private readonly IMinerRepository _minerRepository;
|
||||
|
||||
public SuspendMinerCommandHandler(IMinerRepository minerRepository)
|
||||
{
|
||||
_minerRepository = minerRepository;
|
||||
}
|
||||
|
||||
public async Task<bool> Handle(SuspendMinerCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var miner = await _minerRepository.GetByIdAsync(request.MinerId, cancellationToken)
|
||||
?? throw new MinerNotFoundException(request.MinerId);
|
||||
|
||||
miner.Suspend();
|
||||
_minerRepository.Update(miner);
|
||||
await _minerRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Command to restore a suspended miner.
|
||||
/// VI: Command để khôi phục thợ đào bị tạm ngừng.
|
||||
/// </summary>
|
||||
public record RestoreMinerCommand(Guid MinerId) : IRequest<bool>;
|
||||
|
||||
public class RestoreMinerCommandHandler : IRequestHandler<RestoreMinerCommand, bool>
|
||||
{
|
||||
private readonly IMinerRepository _minerRepository;
|
||||
|
||||
public RestoreMinerCommandHandler(IMinerRepository minerRepository)
|
||||
{
|
||||
_minerRepository = minerRepository;
|
||||
}
|
||||
|
||||
public async Task<bool> Handle(RestoreMinerCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var miner = await _minerRepository.GetByIdAsync(request.MinerId, cancellationToken)
|
||||
?? throw new MinerNotFoundException(request.MinerId);
|
||||
|
||||
miner.Restore();
|
||||
_minerRepository.Update(miner);
|
||||
await _minerRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using MediatR;
|
||||
|
||||
namespace MiningService.API.Application.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Command to apply a referral code.
|
||||
/// VI: Command để áp dụng mã giới thiệu.
|
||||
/// </summary>
|
||||
public record ApplyReferralCodeCommand(Guid UserId, string ReferralCode) : IRequest<ApplyReferralResult>;
|
||||
|
||||
public record ApplyReferralResult(Guid ReferralId, Guid ReferrerId, bool IsActive);
|
||||
@@ -0,0 +1,51 @@
|
||||
using MediatR;
|
||||
using MiningService.Domain.AggregatesModel.MinerAggregate;
|
||||
using MiningService.Domain.AggregatesModel.ReferralAggregate;
|
||||
using MiningService.Domain.Exceptions;
|
||||
|
||||
namespace MiningService.API.Application.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Handler for ApplyReferralCodeCommand.
|
||||
/// VI: Handler cho ApplyReferralCodeCommand.
|
||||
/// </summary>
|
||||
public class ApplyReferralCodeCommandHandler : IRequestHandler<ApplyReferralCodeCommand, ApplyReferralResult>
|
||||
{
|
||||
private readonly IReferralRepository _referralRepository;
|
||||
private readonly IMinerRepository _minerRepository;
|
||||
|
||||
public ApplyReferralCodeCommandHandler(
|
||||
IReferralRepository referralRepository,
|
||||
IMinerRepository minerRepository)
|
||||
{
|
||||
_referralRepository = referralRepository;
|
||||
_minerRepository = minerRepository;
|
||||
}
|
||||
|
||||
public async Task<ApplyReferralResult> Handle(ApplyReferralCodeCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// Get the user who is applying the code
|
||||
var referredMiner = await _minerRepository.GetByUserIdAsync(request.UserId, cancellationToken)
|
||||
?? throw new MinerNotFoundException($"Miner with UserId {request.UserId} not found");
|
||||
|
||||
// Check if already has a referrer
|
||||
if (referredMiner.ReferredById.HasValue)
|
||||
throw new ReferralDomainException("User already has a referrer");
|
||||
|
||||
// Find referrer by code
|
||||
var referrer = await _minerRepository.GetByReferralCodeAsync(request.ReferralCode, cancellationToken)
|
||||
?? throw new ReferralDomainException("Invalid referral code");
|
||||
|
||||
// Cannot self-refer
|
||||
if (referrer.Id == referredMiner.Id)
|
||||
throw new ReferralDomainException("Cannot use your own referral code");
|
||||
|
||||
// Create referral (inactive until KYC)
|
||||
var referral = Referral.Create(referrer.Id, referredMiner.Id, request.ReferralCode);
|
||||
_referralRepository.Add(referral);
|
||||
|
||||
await _referralRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);
|
||||
|
||||
return new ApplyReferralResult(referral.Id, referrer.Id, referral.IsActive);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using MediatR;
|
||||
|
||||
namespace MiningService.API.Application.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Command to claim mining reward.
|
||||
/// VI: Command để nhận thưởng đào.
|
||||
/// </summary>
|
||||
public record ClaimMiningRewardCommand(Guid UserId) : IRequest<ClaimMiningRewardResult>;
|
||||
|
||||
public record ClaimMiningRewardResult(
|
||||
decimal PointsEarned,
|
||||
decimal TotalPoints,
|
||||
int StreakDays,
|
||||
decimal StreakBonus);
|
||||
@@ -0,0 +1,36 @@
|
||||
using MediatR;
|
||||
using MiningService.Domain.AggregatesModel.MinerAggregate;
|
||||
using MiningService.Domain.Exceptions;
|
||||
|
||||
namespace MiningService.API.Application.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Handler for ClaimMiningRewardCommand.
|
||||
/// VI: Handler cho ClaimMiningRewardCommand.
|
||||
/// </summary>
|
||||
public class ClaimMiningRewardCommandHandler : IRequestHandler<ClaimMiningRewardCommand, ClaimMiningRewardResult>
|
||||
{
|
||||
private readonly IMinerRepository _minerRepository;
|
||||
|
||||
public ClaimMiningRewardCommandHandler(IMinerRepository minerRepository)
|
||||
{
|
||||
_minerRepository = minerRepository;
|
||||
}
|
||||
|
||||
public async Task<ClaimMiningRewardResult> Handle(ClaimMiningRewardCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var miner = await _minerRepository.GetByUserIdAsync(request.UserId, cancellationToken)
|
||||
?? throw new MinerNotFoundException($"Miner with UserId {request.UserId} not found");
|
||||
|
||||
var pointsEarned = miner.ClaimMiningReward();
|
||||
|
||||
_minerRepository.Update(miner);
|
||||
await _minerRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);
|
||||
|
||||
return new ClaimMiningRewardResult(
|
||||
pointsEarned,
|
||||
miner.TotalMinedPoints,
|
||||
miner.Streak.CurrentStreak,
|
||||
miner.Streak.BonusMultiplier);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using MediatR;
|
||||
|
||||
namespace MiningService.API.Application.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Command to create a security circle.
|
||||
/// VI: Command để tạo vòng tròn an toàn.
|
||||
/// </summary>
|
||||
public record CreateCircleCommand(Guid UserId, string Name) : IRequest<CreateCircleResult>;
|
||||
|
||||
public record CreateCircleResult(Guid CircleId, string Name);
|
||||
@@ -0,0 +1,50 @@
|
||||
using MediatR;
|
||||
using MiningService.Domain.AggregatesModel.CircleAggregate;
|
||||
using MiningService.Domain.AggregatesModel.MinerAggregate;
|
||||
using MiningService.Domain.Exceptions;
|
||||
|
||||
namespace MiningService.API.Application.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Handler for CreateCircleCommand.
|
||||
/// VI: Handler cho CreateCircleCommand.
|
||||
/// </summary>
|
||||
public class CreateCircleCommandHandler : IRequestHandler<CreateCircleCommand, CreateCircleResult>
|
||||
{
|
||||
private readonly ICircleRepository _circleRepository;
|
||||
private readonly IMinerRepository _minerRepository;
|
||||
|
||||
public CreateCircleCommandHandler(
|
||||
ICircleRepository circleRepository,
|
||||
IMinerRepository minerRepository)
|
||||
{
|
||||
_circleRepository = circleRepository;
|
||||
_minerRepository = minerRepository;
|
||||
}
|
||||
|
||||
public async Task<CreateCircleResult> Handle(CreateCircleCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var miner = await _minerRepository.GetByUserIdAsync(request.UserId, cancellationToken)
|
||||
?? throw new MinerNotFoundException($"Miner with UserId {request.UserId} not found");
|
||||
|
||||
// Check if miner already owns a circle
|
||||
var existingCircle = await _circleRepository.GetByOwnerIdAsync(miner.Id, cancellationToken);
|
||||
if (existingCircle != null)
|
||||
throw new CircleDomainException("You already own a circle");
|
||||
|
||||
// Check if miner is already in a circle
|
||||
if (miner.CircleId.HasValue)
|
||||
throw new CircleDomainException("You are already a member of a circle");
|
||||
|
||||
var circle = Circle.Create(miner.Id, request.Name);
|
||||
_circleRepository.Add(circle);
|
||||
|
||||
// Update miner's circle reference
|
||||
miner.JoinCircle(circle.Id);
|
||||
_minerRepository.Update(miner);
|
||||
|
||||
await _circleRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);
|
||||
|
||||
return new CreateCircleResult(circle.Id, circle.Name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using MediatR;
|
||||
|
||||
namespace MiningService.API.Application.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Command to invite a member to circle.
|
||||
/// VI: Command để mời thành viên vào vòng tròn.
|
||||
/// </summary>
|
||||
public record InviteToCircleCommand(Guid UserId, Guid TargetMinerId) : IRequest<bool>;
|
||||
@@ -0,0 +1,61 @@
|
||||
using MediatR;
|
||||
using MiningService.Domain.AggregatesModel.CircleAggregate;
|
||||
using MiningService.Domain.AggregatesModel.MinerAggregate;
|
||||
using MiningService.Domain.Exceptions;
|
||||
|
||||
namespace MiningService.API.Application.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Handler for InviteToCircleCommand.
|
||||
/// VI: Handler cho InviteToCircleCommand.
|
||||
/// </summary>
|
||||
public class InviteToCircleCommandHandler : IRequestHandler<InviteToCircleCommand, bool>
|
||||
{
|
||||
private readonly ICircleRepository _circleRepository;
|
||||
private readonly IMinerRepository _minerRepository;
|
||||
|
||||
public InviteToCircleCommandHandler(
|
||||
ICircleRepository circleRepository,
|
||||
IMinerRepository minerRepository)
|
||||
{
|
||||
_circleRepository = circleRepository;
|
||||
_minerRepository = minerRepository;
|
||||
}
|
||||
|
||||
public async Task<bool> Handle(InviteToCircleCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var inviter = await _minerRepository.GetByUserIdAsync(request.UserId, cancellationToken)
|
||||
?? throw new MinerNotFoundException($"Miner with UserId {request.UserId} not found");
|
||||
|
||||
var circle = await _circleRepository.GetByOwnerIdAsync(inviter.Id, cancellationToken)
|
||||
?? throw new CircleDomainException("You don't own a circle");
|
||||
|
||||
var targetMiner = await _minerRepository.GetByIdAsync(request.TargetMinerId, cancellationToken)
|
||||
?? throw new MinerNotFoundException($"Target miner not found");
|
||||
|
||||
if (targetMiner.CircleId.HasValue)
|
||||
throw new CircleDomainException("Target miner is already in a circle");
|
||||
|
||||
// Add member to circle
|
||||
circle.AddMember(targetMiner.Id);
|
||||
_circleRepository.Update(circle);
|
||||
|
||||
// Update target miner's circle reference
|
||||
targetMiner.JoinCircle(circle.Id);
|
||||
_minerRepository.Update(targetMiner);
|
||||
|
||||
// Recalculate rates for all members if circle becomes valid
|
||||
if (circle.IsValid)
|
||||
{
|
||||
foreach (var member in circle.Members.Where(m => m.IsActive))
|
||||
{
|
||||
var memberMiner = await _minerRepository.GetByIdAsync(member.MinerId, cancellationToken);
|
||||
memberMiner?.RecalculateRate(circleBonus: circle.BonusMultiplier);
|
||||
if (memberMiner != null) _minerRepository.Update(memberMiner);
|
||||
}
|
||||
}
|
||||
|
||||
await _circleRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using MediatR;
|
||||
|
||||
namespace MiningService.API.Application.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Command to start a mining session.
|
||||
/// VI: Command để bắt đầu phiên đào.
|
||||
/// </summary>
|
||||
public record StartMiningCommand(Guid UserId) : IRequest<StartMiningResult>;
|
||||
|
||||
public record StartMiningResult(
|
||||
Guid SessionId,
|
||||
decimal HourlyRate,
|
||||
DateTime EndTime,
|
||||
int StreakDays);
|
||||
@@ -0,0 +1,36 @@
|
||||
using MediatR;
|
||||
using MiningService.Domain.AggregatesModel.MinerAggregate;
|
||||
using MiningService.Domain.Exceptions;
|
||||
|
||||
namespace MiningService.API.Application.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Handler for StartMiningCommand.
|
||||
/// VI: Handler cho StartMiningCommand.
|
||||
/// </summary>
|
||||
public class StartMiningCommandHandler : IRequestHandler<StartMiningCommand, StartMiningResult>
|
||||
{
|
||||
private readonly IMinerRepository _minerRepository;
|
||||
|
||||
public StartMiningCommandHandler(IMinerRepository minerRepository)
|
||||
{
|
||||
_minerRepository = minerRepository;
|
||||
}
|
||||
|
||||
public async Task<StartMiningResult> Handle(StartMiningCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var miner = await _minerRepository.GetByUserIdAsync(request.UserId, cancellationToken)
|
||||
?? throw new MinerNotFoundException($"Miner with UserId {request.UserId} not found");
|
||||
|
||||
var session = miner.StartMiningSession();
|
||||
|
||||
_minerRepository.Update(miner);
|
||||
await _minerRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);
|
||||
|
||||
return new StartMiningResult(
|
||||
session.SessionId,
|
||||
session.HourlyRate,
|
||||
session.EndTime,
|
||||
miner.Streak.CurrentStreak);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using MediatR;
|
||||
using MiningService.Domain.AggregatesModel.MinerAggregate;
|
||||
|
||||
namespace MiningService.API.Application.Queries;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Query for admin dashboard overview.
|
||||
/// VI: Query tổng quan dashboard admin.
|
||||
/// </summary>
|
||||
public record GetAdminOverviewQuery() : IRequest<AdminOverviewDto>;
|
||||
|
||||
public record AdminOverviewDto(
|
||||
int TotalMiners,
|
||||
int ActiveMiners,
|
||||
int MinersWithActiveSession,
|
||||
decimal TotalPointsMined,
|
||||
int TotalCircles,
|
||||
int ValidCircles,
|
||||
int TotalReferrals,
|
||||
int ActiveReferrals);
|
||||
|
||||
public class GetAdminOverviewQueryHandler : IRequestHandler<GetAdminOverviewQuery, AdminOverviewDto>
|
||||
{
|
||||
private readonly IMinerRepository _minerRepository;
|
||||
|
||||
public GetAdminOverviewQueryHandler(IMinerRepository minerRepository)
|
||||
{
|
||||
_minerRepository = minerRepository;
|
||||
}
|
||||
|
||||
public async Task<AdminOverviewDto> Handle(GetAdminOverviewQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
// Note: In production, use Dapper for read-optimized queries
|
||||
// This is a simplified version
|
||||
return new AdminOverviewDto(
|
||||
TotalMiners: 0, // TODO: Implement with Dapper
|
||||
ActiveMiners: 0,
|
||||
MinersWithActiveSession: 0,
|
||||
TotalPointsMined: 0,
|
||||
TotalCircles: 0,
|
||||
ValidCircles: 0,
|
||||
TotalReferrals: 0,
|
||||
ActiveReferrals: 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
using MediatR;
|
||||
using MiningService.Domain.AggregatesModel.CircleAggregate;
|
||||
using MiningService.Domain.AggregatesModel.MinerAggregate;
|
||||
|
||||
namespace MiningService.API.Application.Queries;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Query to get circle details.
|
||||
/// VI: Query để lấy chi tiết vòng tròn.
|
||||
/// </summary>
|
||||
public record GetCircleQuery(Guid UserId) : IRequest<CircleDto?>;
|
||||
|
||||
public record CircleDto(
|
||||
Guid CircleId,
|
||||
string Name,
|
||||
Guid OwnerId,
|
||||
int MemberCount,
|
||||
decimal TrustScore,
|
||||
decimal BonusMultiplier,
|
||||
bool IsValid,
|
||||
string Status,
|
||||
List<CircleMemberDto> Members);
|
||||
|
||||
public record CircleMemberDto(
|
||||
Guid MinerId,
|
||||
DateTime JoinedAt,
|
||||
bool IsActive);
|
||||
|
||||
public class GetCircleQueryHandler : IRequestHandler<GetCircleQuery, CircleDto?>
|
||||
{
|
||||
private readonly ICircleRepository _circleRepository;
|
||||
private readonly IMinerRepository _minerRepository;
|
||||
|
||||
public GetCircleQueryHandler(
|
||||
ICircleRepository circleRepository,
|
||||
IMinerRepository minerRepository)
|
||||
{
|
||||
_circleRepository = circleRepository;
|
||||
_minerRepository = minerRepository;
|
||||
}
|
||||
|
||||
public async Task<CircleDto?> Handle(GetCircleQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var miner = await _minerRepository.GetByUserIdAsync(request.UserId, cancellationToken);
|
||||
if (miner == null || !miner.CircleId.HasValue) return null;
|
||||
|
||||
var circle = await _circleRepository.GetByIdAsync(miner.CircleId.Value, cancellationToken);
|
||||
if (circle == null) return null;
|
||||
|
||||
return new CircleDto(
|
||||
circle.Id,
|
||||
circle.Name,
|
||||
circle.OwnerId,
|
||||
circle.ActiveMemberCount,
|
||||
circle.TrustScore,
|
||||
circle.BonusMultiplier,
|
||||
circle.IsValid,
|
||||
circle.Status.ToString(),
|
||||
circle.Members.Select(m => new CircleMemberDto(
|
||||
m.MinerId,
|
||||
m.JoinedAt,
|
||||
m.IsActive
|
||||
)).ToList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using MediatR;
|
||||
using MiningService.Domain.AggregatesModel.MinerAggregate;
|
||||
using MiningService.Domain.Exceptions;
|
||||
|
||||
namespace MiningService.API.Application.Queries;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Query to get miner status.
|
||||
/// VI: Query để lấy trạng thái thợ đào.
|
||||
/// </summary>
|
||||
public record GetMinerStatusQuery(Guid UserId) : IRequest<MinerStatusDto?>;
|
||||
|
||||
public record MinerStatusDto(
|
||||
Guid MinerId,
|
||||
string Role,
|
||||
decimal TotalMinedPoints,
|
||||
decimal HourlyRate,
|
||||
decimal DailyRate,
|
||||
int CurrentStreak,
|
||||
int LongestStreak,
|
||||
decimal StreakBonus,
|
||||
bool HasActiveSession,
|
||||
DateTime? SessionEndTime,
|
||||
string Status);
|
||||
|
||||
public class GetMinerStatusQueryHandler : IRequestHandler<GetMinerStatusQuery, MinerStatusDto?>
|
||||
{
|
||||
private readonly IMinerRepository _minerRepository;
|
||||
|
||||
public GetMinerStatusQueryHandler(IMinerRepository minerRepository)
|
||||
{
|
||||
_minerRepository = minerRepository;
|
||||
}
|
||||
|
||||
public async Task<MinerStatusDto?> Handle(GetMinerStatusQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var miner = await _minerRepository.GetByUserIdAsync(request.UserId, cancellationToken);
|
||||
if (miner == null) return null;
|
||||
|
||||
return new MinerStatusDto(
|
||||
miner.Id,
|
||||
miner.Role.ToString(),
|
||||
miner.TotalMinedPoints,
|
||||
miner.CurrentRate.TotalRate,
|
||||
miner.CurrentRate.DailyRate,
|
||||
miner.Streak.CurrentStreak,
|
||||
miner.Streak.LongestStreak,
|
||||
miner.Streak.BonusMultiplier,
|
||||
miner.ActiveSession?.Status == MiningSessionStatus.Active,
|
||||
miner.ActiveSession?.EndTime,
|
||||
miner.Status.ToString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using MediatR;
|
||||
using MiningService.Domain.AggregatesModel.MinerAggregate;
|
||||
using MiningService.Domain.AggregatesModel.ReferralAggregate;
|
||||
|
||||
namespace MiningService.API.Application.Queries;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Query to get user's referrals.
|
||||
/// VI: Query để lấy danh sách giới thiệu.
|
||||
/// </summary>
|
||||
public record GetReferralsQuery(Guid UserId) : IRequest<ReferralsDto>;
|
||||
|
||||
public record ReferralsDto(
|
||||
string MyReferralCode,
|
||||
int TotalReferrals,
|
||||
int ActiveReferrals,
|
||||
decimal TotalBonusPercent,
|
||||
List<ReferralDto> Referrals);
|
||||
|
||||
public record ReferralDto(
|
||||
Guid ReferralId,
|
||||
Guid ReferredId,
|
||||
bool IsActive,
|
||||
DateTime CreatedAt,
|
||||
DateTime? ActivatedAt);
|
||||
|
||||
public class GetReferralsQueryHandler : IRequestHandler<GetReferralsQuery, ReferralsDto>
|
||||
{
|
||||
private readonly IReferralRepository _referralRepository;
|
||||
private readonly IMinerRepository _minerRepository;
|
||||
|
||||
public GetReferralsQueryHandler(
|
||||
IReferralRepository referralRepository,
|
||||
IMinerRepository minerRepository)
|
||||
{
|
||||
_referralRepository = referralRepository;
|
||||
_minerRepository = minerRepository;
|
||||
}
|
||||
|
||||
public async Task<ReferralsDto> Handle(GetReferralsQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var miner = await _minerRepository.GetByUserIdAsync(request.UserId, cancellationToken);
|
||||
if (miner == null)
|
||||
return new ReferralsDto("", 0, 0, 0, new List<ReferralDto>());
|
||||
|
||||
var referrals = await _referralRepository.GetByReferrerIdAsync(miner.Id, cancellationToken);
|
||||
var activeCount = referrals.Count(r => r.IsActive);
|
||||
var bonusPercent = Math.Min(activeCount * 0.25m, 1.0m); // 25% per referral, max 100%
|
||||
|
||||
return new ReferralsDto(
|
||||
miner.ReferralCode,
|
||||
referrals.Count,
|
||||
activeCount,
|
||||
bonusPercent,
|
||||
referrals.Select(r => new ReferralDto(
|
||||
r.Id,
|
||||
r.ReferredId,
|
||||
r.IsActive,
|
||||
r.CreatedAt,
|
||||
r.ActivatedAt
|
||||
)).ToList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using MiningService.API.Application.Commands;
|
||||
using MiningService.API.Application.Queries;
|
||||
|
||||
namespace MiningService.API.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Admin backoffice controller.
|
||||
/// VI: Controller cho admin backoffice.
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/v1/admin")]
|
||||
public class AdminController : ControllerBase
|
||||
{
|
||||
private readonly IMediator _mediator;
|
||||
|
||||
public AdminController(IMediator mediator)
|
||||
{
|
||||
_mediator = mediator;
|
||||
}
|
||||
|
||||
#region Analytics
|
||||
|
||||
/// <summary>
|
||||
/// EN: Get admin dashboard overview.
|
||||
/// VI: Lấy tổng quan dashboard admin.
|
||||
/// </summary>
|
||||
[HttpGet("analytics/overview")]
|
||||
[ProducesResponseType(typeof(AdminOverviewDto), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> GetOverview(CancellationToken ct)
|
||||
{
|
||||
var result = await _mediator.Send(new GetAdminOverviewQuery(), ct);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Miner Management
|
||||
|
||||
/// <summary>
|
||||
/// EN: Suspend a miner account.
|
||||
/// VI: Tạm ngừng tài khoản thợ đào.
|
||||
/// </summary>
|
||||
[HttpPut("miners/{minerId}/suspend")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> SuspendMiner(Guid minerId, [FromBody] SuspendRequest request, CancellationToken ct)
|
||||
{
|
||||
await _mediator.Send(new SuspendMinerCommand(minerId, request.Reason), ct);
|
||||
return Ok(new { message = "Miner suspended successfully" });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Restore a suspended miner account.
|
||||
/// VI: Khôi phục tài khoản thợ đào bị tạm ngừng.
|
||||
/// </summary>
|
||||
[HttpPut("miners/{minerId}/restore")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> RestoreMiner(Guid minerId, CancellationToken ct)
|
||||
{
|
||||
await _mediator.Send(new RestoreMinerCommand(minerId), ct);
|
||||
return Ok(new { message = "Miner restored successfully" });
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
public record SuspendRequest(string Reason);
|
||||
@@ -0,0 +1,65 @@
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using MiningService.API.Application.Commands;
|
||||
using MiningService.API.Application.Queries;
|
||||
|
||||
namespace MiningService.API.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Security circles controller.
|
||||
/// VI: Controller cho vòng tròn an toàn.
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/v1/[controller]")]
|
||||
public class CirclesController : ControllerBase
|
||||
{
|
||||
private readonly IMediator _mediator;
|
||||
|
||||
public CirclesController(IMediator mediator)
|
||||
{
|
||||
_mediator = mediator;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Get user's circle.
|
||||
/// VI: Lấy vòng tròn của người dùng.
|
||||
/// </summary>
|
||||
[HttpGet("me")]
|
||||
[ProducesResponseType(typeof(CircleDto), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> GetMyCircle([FromQuery] Guid userId, CancellationToken ct)
|
||||
{
|
||||
var result = await _mediator.Send(new GetCircleQuery(userId), ct);
|
||||
if (result == null) return NotFound();
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Create a new security circle.
|
||||
/// VI: Tạo vòng tròn an toàn mới.
|
||||
/// </summary>
|
||||
[HttpPost]
|
||||
[ProducesResponseType(typeof(CreateCircleResult), StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<IActionResult> CreateCircle([FromBody] CreateCircleRequest request, CancellationToken ct)
|
||||
{
|
||||
var result = await _mediator.Send(new CreateCircleCommand(request.UserId, request.Name), ct);
|
||||
return CreatedAtAction(nameof(GetMyCircle), new { userId = request.UserId }, result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Invite a member to circle.
|
||||
/// VI: Mời thành viên vào vòng tròn.
|
||||
/// </summary>
|
||||
[HttpPost("invite")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<IActionResult> InviteMember([FromBody] InviteMemberRequest request, CancellationToken ct)
|
||||
{
|
||||
await _mediator.Send(new InviteToCircleCommand(request.UserId, request.TargetMinerId), ct);
|
||||
return Ok(new { message = "Member invited successfully" });
|
||||
}
|
||||
}
|
||||
|
||||
public record CreateCircleRequest(Guid UserId, string Name);
|
||||
public record InviteMemberRequest(Guid UserId, Guid TargetMinerId);
|
||||
@@ -0,0 +1,66 @@
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using MiningService.API.Application.Commands;
|
||||
using MiningService.API.Application.Queries;
|
||||
|
||||
namespace MiningService.API.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Mining operations controller.
|
||||
/// VI: Controller cho các thao tác đào.
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/v1/[controller]")]
|
||||
public class MiningController : ControllerBase
|
||||
{
|
||||
private readonly IMediator _mediator;
|
||||
|
||||
public MiningController(IMediator mediator)
|
||||
{
|
||||
_mediator = mediator;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Get current miner status.
|
||||
/// VI: Lấy trạng thái thợ đào hiện tại.
|
||||
/// </summary>
|
||||
[HttpGet("me")]
|
||||
[ProducesResponseType(typeof(MinerStatusDto), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> GetMinerStatus([FromQuery] Guid userId, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await _mediator.Send(new GetMinerStatusQuery(userId), cancellationToken);
|
||||
if (result == null)
|
||||
return NotFound();
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Start a new mining session.
|
||||
/// VI: Bắt đầu phiên đào mới.
|
||||
/// </summary>
|
||||
[HttpPost("start")]
|
||||
[ProducesResponseType(typeof(StartMiningResult), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<IActionResult> StartMining([FromBody] StartMiningRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await _mediator.Send(new StartMiningCommand(request.UserId), cancellationToken);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Claim mining reward from completed session.
|
||||
/// VI: Nhận thưởng đào từ phiên hoàn thành.
|
||||
/// </summary>
|
||||
[HttpPost("claim")]
|
||||
[ProducesResponseType(typeof(ClaimMiningRewardResult), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<IActionResult> ClaimReward([FromBody] ClaimMiningRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await _mediator.Send(new ClaimMiningRewardCommand(request.UserId), cancellationToken);
|
||||
return Ok(result);
|
||||
}
|
||||
}
|
||||
|
||||
public record StartMiningRequest(Guid UserId);
|
||||
public record ClaimMiningRequest(Guid UserId);
|
||||
@@ -0,0 +1,49 @@
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using MiningService.API.Application.Commands;
|
||||
using MiningService.API.Application.Queries;
|
||||
|
||||
namespace MiningService.API.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Referrals controller.
|
||||
/// VI: Controller cho giới thiệu.
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/v1/[controller]")]
|
||||
public class ReferralsController : ControllerBase
|
||||
{
|
||||
private readonly IMediator _mediator;
|
||||
|
||||
public ReferralsController(IMediator mediator)
|
||||
{
|
||||
_mediator = mediator;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Get my referral code and referrals list.
|
||||
/// VI: Lấy mã giới thiệu và danh sách người được giới thiệu.
|
||||
/// </summary>
|
||||
[HttpGet]
|
||||
[ProducesResponseType(typeof(ReferralsDto), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> GetReferrals([FromQuery] Guid userId, CancellationToken ct)
|
||||
{
|
||||
var result = await _mediator.Send(new GetReferralsQuery(userId), ct);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Apply a referral code.
|
||||
/// VI: Áp dụng mã giới thiệu.
|
||||
/// </summary>
|
||||
[HttpPost("apply")]
|
||||
[ProducesResponseType(typeof(ApplyReferralResult), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<IActionResult> ApplyReferralCode([FromBody] ApplyReferralRequest request, CancellationToken ct)
|
||||
{
|
||||
var result = await _mediator.Send(new ApplyReferralCodeCommand(request.UserId, request.ReferralCode), ct);
|
||||
return Ok(result);
|
||||
}
|
||||
}
|
||||
|
||||
public record ApplyReferralRequest(Guid UserId, string ReferralCode);
|
||||
@@ -0,0 +1,66 @@
|
||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||
using MiningService.Infrastructure;
|
||||
|
||||
namespace MiningService.API.HealthChecks;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Health check for database connectivity.
|
||||
/// VI: Health check cho kết nối database.
|
||||
/// </summary>
|
||||
public class DatabaseHealthCheck : IHealthCheck
|
||||
{
|
||||
private readonly MiningServiceContext _context;
|
||||
|
||||
public DatabaseHealthCheck(MiningServiceContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<HealthCheckResult> CheckHealthAsync(
|
||||
HealthCheckContext context,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _context.Database.CanConnectAsync(cancellationToken);
|
||||
return HealthCheckResult.Healthy("Database connection is healthy");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return HealthCheckResult.Unhealthy("Database connection failed", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Readiness check - service ready to accept traffic.
|
||||
/// VI: Readiness check - dịch vụ sẵn sàng nhận traffic.
|
||||
/// </summary>
|
||||
public class ReadinessHealthCheck : IHealthCheck
|
||||
{
|
||||
private readonly MiningServiceContext _context;
|
||||
|
||||
public ReadinessHealthCheck(MiningServiceContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<HealthCheckResult> CheckHealthAsync(
|
||||
HealthCheckContext context,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Check database
|
||||
var canConnect = await _context.Database.CanConnectAsync(cancellationToken);
|
||||
if (!canConnect)
|
||||
return HealthCheckResult.Unhealthy("Cannot connect to database");
|
||||
|
||||
return HealthCheckResult.Healthy("Service is ready");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return HealthCheckResult.Unhealthy("Readiness check failed", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
namespace MiningService.API.Hubs;
|
||||
|
||||
/// <summary>
|
||||
/// EN: SignalR Hub for real-time mining updates.
|
||||
/// VI: SignalR Hub cho cập nhật đào thời gian thực.
|
||||
/// </summary>
|
||||
public class MiningHub : Hub
|
||||
{
|
||||
private readonly ILogger<MiningHub> _logger;
|
||||
|
||||
public MiningHub(ILogger<MiningHub> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Join miner's personal update group.
|
||||
/// VI: Tham gia nhóm cập nhật cá nhân của thợ đào.
|
||||
/// </summary>
|
||||
public async Task JoinMinerGroup(Guid minerId)
|
||||
{
|
||||
await Groups.AddToGroupAsync(Context.ConnectionId, $"miner:{minerId}");
|
||||
_logger.LogInformation("Client {ConnectionId} joined miner group {MinerId}",
|
||||
Context.ConnectionId, minerId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Leave miner's personal update group.
|
||||
/// VI: Rời nhóm cập nhật cá nhân của thợ đào.
|
||||
/// </summary>
|
||||
public async Task LeaveMinerGroup(Guid minerId)
|
||||
{
|
||||
await Groups.RemoveFromGroupAsync(Context.ConnectionId, $"miner:{minerId}");
|
||||
_logger.LogInformation("Client {ConnectionId} left miner group {MinerId}",
|
||||
Context.ConnectionId, minerId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Join leaderboard updates group.
|
||||
/// VI: Tham gia nhóm cập nhật bảng xếp hạng.
|
||||
/// </summary>
|
||||
public async Task JoinLeaderboardGroup()
|
||||
{
|
||||
await Groups.AddToGroupAsync(Context.ConnectionId, "leaderboard");
|
||||
}
|
||||
|
||||
public override async Task OnConnectedAsync()
|
||||
{
|
||||
_logger.LogInformation("Client connected: {ConnectionId}", Context.ConnectionId);
|
||||
await base.OnConnectedAsync();
|
||||
}
|
||||
|
||||
public override async Task OnDisconnectedAsync(Exception? exception)
|
||||
{
|
||||
_logger.LogInformation("Client disconnected: {ConnectionId}", Context.ConnectionId);
|
||||
await base.OnDisconnectedAsync(exception);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Service to send mining updates to clients.
|
||||
/// VI: Service gửi cập nhật đào đến clients.
|
||||
/// </summary>
|
||||
public interface IMiningHubService
|
||||
{
|
||||
Task SendPointsUpdated(Guid minerId, decimal earnedPoints, decimal totalPoints, int streakDays);
|
||||
Task SendSessionStarted(Guid minerId, DateTime endTime, decimal hourlyRate);
|
||||
Task SendStreakMilestone(Guid minerId, int streakDays, decimal bonusPoints);
|
||||
}
|
||||
|
||||
public class MiningHubService : IMiningHubService
|
||||
{
|
||||
private readonly IHubContext<MiningHub> _hubContext;
|
||||
|
||||
public MiningHubService(IHubContext<MiningHub> hubContext)
|
||||
{
|
||||
_hubContext = hubContext;
|
||||
}
|
||||
|
||||
public async Task SendPointsUpdated(Guid minerId, decimal earnedPoints, decimal totalPoints, int streakDays)
|
||||
{
|
||||
await _hubContext.Clients.Group($"miner:{minerId}")
|
||||
.SendAsync("PointsUpdated", new { earnedPoints, totalPoints, streakDays });
|
||||
}
|
||||
|
||||
public async Task SendSessionStarted(Guid minerId, DateTime endTime, decimal hourlyRate)
|
||||
{
|
||||
await _hubContext.Clients.Group($"miner:{minerId}")
|
||||
.SendAsync("SessionStarted", new { endTime, hourlyRate });
|
||||
}
|
||||
|
||||
public async Task SendStreakMilestone(Guid minerId, int streakDays, decimal bonusPoints)
|
||||
{
|
||||
await _hubContext.Clients.Group($"miner:{minerId}")
|
||||
.SendAsync("StreakMilestone", new { streakDays, bonusPoints });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<AssemblyName>MiningService.API</AssemblyName>
|
||||
<RootNamespace>MiningService.API</RootNamespace>
|
||||
<Description>Web API layer with CQRS pattern</Description>
|
||||
<UserSecretsId>myservice-api</UserSecretsId>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<!-- EN: MediatR for CQRS / VI: MediatR cho CQRS -->
|
||||
<PackageReference Include="MediatR" Version="12.4.1" />
|
||||
|
||||
<!-- EN: FluentValidation for request validation / VI: FluentValidation cho validation request -->
|
||||
<PackageReference Include="FluentValidation" Version="11.11.0" />
|
||||
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.11.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.2">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
|
||||
<!-- EN: Swagger/OpenAPI / VI: Swagger/OpenAPI -->
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
|
||||
|
||||
<!-- EN: API Versioning / VI: API Versioning -->
|
||||
<PackageReference Include="Asp.Versioning.Mvc" Version="8.1.0" />
|
||||
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
|
||||
|
||||
<!-- EN: Health checks / VI: Health checks -->
|
||||
<PackageReference Include="AspNetCore.HealthChecks.NpgSql" Version="8.0.2" />
|
||||
<PackageReference Include="AspNetCore.HealthChecks.Redis" Version="8.0.1" />
|
||||
|
||||
<!-- EN: Problem Details (RFC 7807) / VI: Problem Details (RFC 7807) -->
|
||||
<PackageReference Include="Hellang.Middleware.ProblemDetails" Version="6.5.1" />
|
||||
|
||||
<!-- EN: Serilog for structured logging / VI: Serilog cho structured logging -->
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="8.0.3" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.Seq" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MiningService.Domain\MiningService.Domain.csproj" />
|
||||
<ProjectReference Include="..\MiningService.Infrastructure\MiningService.Infrastructure.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
144
services/mining-service-net/src/MiningService.API/Program.cs
Normal file
144
services/mining-service-net/src/MiningService.API/Program.cs
Normal file
@@ -0,0 +1,144 @@
|
||||
using Asp.Versioning;
|
||||
using FluentValidation;
|
||||
using Hellang.Middleware.ProblemDetails;
|
||||
using MiningService.API.Application.Behaviors;
|
||||
using MiningService.Infrastructure;
|
||||
using Serilog;
|
||||
|
||||
// EN: Configure Serilog early / VI: Cấu hình Serilog sớm
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.WriteTo.Console()
|
||||
.CreateBootstrapLogger();
|
||||
|
||||
try
|
||||
{
|
||||
Log.Information("Starting MiningService API / Khởi động MiningService API");
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// EN: Configure Serilog / VI: Cấu hình Serilog
|
||||
builder.Host.UseSerilog((context, services, configuration) => configuration
|
||||
.ReadFrom.Configuration(context.Configuration)
|
||||
.ReadFrom.Services(services)
|
||||
.Enrich.FromLogContext()
|
||||
.WriteTo.Console());
|
||||
|
||||
// EN: Add Infrastructure services / VI: Thêm Infrastructure services
|
||||
builder.Services.AddInfrastructure(builder.Configuration);
|
||||
|
||||
// EN: Add MediatR with behaviors / VI: Thêm MediatR với behaviors
|
||||
builder.Services.AddMediatR(cfg =>
|
||||
{
|
||||
cfg.RegisterServicesFromAssemblyContaining<Program>();
|
||||
cfg.AddOpenBehavior(typeof(LoggingBehavior<,>));
|
||||
cfg.AddOpenBehavior(typeof(ValidatorBehavior<,>));
|
||||
cfg.AddOpenBehavior(typeof(TransactionBehavior<,>));
|
||||
});
|
||||
|
||||
// EN: Add FluentValidation / VI: Thêm FluentValidation
|
||||
builder.Services.AddValidatorsFromAssemblyContaining<Program>();
|
||||
|
||||
// EN: Add API versioning / VI: Thêm API versioning
|
||||
builder.Services.AddApiVersioning(options =>
|
||||
{
|
||||
options.DefaultApiVersion = new ApiVersion(1, 0);
|
||||
options.AssumeDefaultVersionWhenUnspecified = true;
|
||||
options.ReportApiVersions = true;
|
||||
options.ApiVersionReader = ApiVersionReader.Combine(
|
||||
new UrlSegmentApiVersionReader(),
|
||||
new HeaderApiVersionReader("X-Api-Version"));
|
||||
})
|
||||
.AddApiExplorer(options =>
|
||||
{
|
||||
options.GroupNameFormat = "'v'VVV";
|
||||
options.SubstituteApiVersionInUrl = true;
|
||||
});
|
||||
|
||||
// EN: Add controllers / VI: Thêm controllers
|
||||
builder.Services.AddControllers();
|
||||
|
||||
// EN: Add ProblemDetails middleware (RFC 7807) / VI: Thêm ProblemDetails middleware
|
||||
builder.Services.AddProblemDetails(options =>
|
||||
{
|
||||
options.IncludeExceptionDetails = (ctx, ex) =>
|
||||
builder.Environment.IsDevelopment();
|
||||
});
|
||||
|
||||
// EN: Add Swagger / VI: Thêm Swagger
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen(options =>
|
||||
{
|
||||
options.SwaggerDoc("v1", new()
|
||||
{
|
||||
Title = "MiningService API",
|
||||
Version = "v1",
|
||||
Description = "MiningService microservice API / API microservice MiningService"
|
||||
});
|
||||
});
|
||||
|
||||
// EN: Add health checks / VI: Thêm health checks
|
||||
builder.Services.AddHealthChecks()
|
||||
.AddNpgSql(
|
||||
builder.Configuration.GetConnectionString("DefaultConnection")
|
||||
?? builder.Configuration["DATABASE_URL"]
|
||||
?? "",
|
||||
name: "postgresql",
|
||||
tags: ["db", "postgresql"]);
|
||||
|
||||
// EN: Add CORS / VI: Thêm CORS
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddDefaultPolicy(policy =>
|
||||
{
|
||||
policy.AllowAnyOrigin()
|
||||
.AllowAnyMethod()
|
||||
.AllowAnyHeader();
|
||||
});
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// EN: Configure middleware pipeline / VI: Cấu hình middleware pipeline
|
||||
app.UseSerilogRequestLogging();
|
||||
app.UseProblemDetails();
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI(c =>
|
||||
{
|
||||
c.SwaggerEndpoint("/swagger/v1/swagger.json", "MiningService API v1");
|
||||
c.RoutePrefix = "swagger";
|
||||
});
|
||||
}
|
||||
|
||||
app.UseCors();
|
||||
app.UseRouting();
|
||||
|
||||
// EN: Map health check endpoints / VI: Map health check endpoints
|
||||
app.MapHealthChecks("/health");
|
||||
app.MapHealthChecks("/health/live", new()
|
||||
{
|
||||
Predicate = _ => false // EN: Just checks app is running / VI: Chỉ kiểm tra app đang chạy
|
||||
});
|
||||
app.MapHealthChecks("/health/ready");
|
||||
|
||||
// EN: Map controllers / VI: Map controllers
|
||||
app.MapControllers();
|
||||
|
||||
// EN: Run the application / VI: Chạy ứng dụng
|
||||
app.Run();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Fatal(ex, "Application terminated unexpectedly / Ứng dụng kết thúc bất ngờ");
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
Log.CloseAndFlush();
|
||||
}
|
||||
|
||||
// EN: Make Program class accessible for integration tests
|
||||
// VI: Làm cho class Program có thể truy cập cho integration tests
|
||||
public partial class Program { }
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "http://localhost:5000",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Debug",
|
||||
"Microsoft.AspNetCore": "Information",
|
||||
"Microsoft.EntityFrameworkCore.Database.Command": "Information"
|
||||
}
|
||||
},
|
||||
"Serilog": {
|
||||
"MinimumLevel": {
|
||||
"Default": "Debug",
|
||||
"Override": {
|
||||
"Microsoft": "Information",
|
||||
"Microsoft.EntityFrameworkCore.Database.Command": "Information",
|
||||
"System": "Information"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning",
|
||||
"Microsoft.EntityFrameworkCore": "Warning"
|
||||
}
|
||||
},
|
||||
"Serilog": {
|
||||
"MinimumLevel": {
|
||||
"Default": "Information",
|
||||
"Override": {
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.EntityFrameworkCore": "Warning",
|
||||
"System": "Warning"
|
||||
}
|
||||
},
|
||||
"WriteTo": [
|
||||
{
|
||||
"Name": "Console",
|
||||
"Args": {
|
||||
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}"
|
||||
}
|
||||
}
|
||||
],
|
||||
"Enrich": [
|
||||
"FromLogContext",
|
||||
"WithMachineName",
|
||||
"WithThreadId"
|
||||
]
|
||||
},
|
||||
"ConnectionStrings": {
|
||||
"DefaultConnection": "Host=localhost;Port=5432;Database=myservice_db;Username=postgres;Password=postgres"
|
||||
},
|
||||
"Redis": {
|
||||
"ConnectionString": "localhost:6379"
|
||||
},
|
||||
"Jwt": {
|
||||
"Secret": "your-super-secret-key-min-32-characters",
|
||||
"Issuer": "goodgo-platform",
|
||||
"Audience": "goodgo-services",
|
||||
"AccessTokenExpiryMinutes": 15,
|
||||
"RefreshTokenExpiryDays": 7
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
using MiningService.Domain.Events;
|
||||
using MiningService.Domain.Exceptions;
|
||||
using MiningService.Domain.SeedWork;
|
||||
|
||||
namespace MiningService.Domain.AggregatesModel.CircleAggregate;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Security Circle Aggregate Root - trusted group for mining bonus.
|
||||
/// VI: Security Circle Aggregate Root - nhóm tin cậy cho thưởng đào.
|
||||
/// </summary>
|
||||
public class Circle : Entity, IAggregateRoot
|
||||
{
|
||||
private readonly List<CircleMember> _members = new();
|
||||
|
||||
private const int MinMembers = 3;
|
||||
private const int MaxMembers = 5;
|
||||
private const decimal ValidCircleBonus = 0.25m; // 25%
|
||||
|
||||
#region Properties
|
||||
|
||||
/// <summary>EN: Circle owner (creator) / VI: Chủ vòng tròn (người tạo)</summary>
|
||||
public Guid OwnerId { get; private set; }
|
||||
|
||||
/// <summary>EN: Circle name / VI: Tên vòng tròn</summary>
|
||||
public string Name { get; private set; } = string.Empty;
|
||||
|
||||
/// <summary>EN: Trust score (0-100) / VI: Điểm tin cậy (0-100)</summary>
|
||||
public decimal TrustScore { get; private set; }
|
||||
|
||||
/// <summary>EN: Bonus multiplier for valid circle / VI: Hệ số thưởng cho vòng tròn hợp lệ</summary>
|
||||
public decimal BonusMultiplier { get; private set; }
|
||||
|
||||
/// <summary>EN: Circle status / VI: Trạng thái vòng tròn</summary>
|
||||
public CircleStatus Status { get; private set; }
|
||||
|
||||
/// <summary>EN: When circle was created / VI: Khi vòng tròn được tạo</summary>
|
||||
public DateTime CreatedAt { get; private set; }
|
||||
|
||||
/// <summary>EN: Last update time / VI: Thời gian cập nhật cuối</summary>
|
||||
public DateTime UpdatedAt { get; private set; }
|
||||
|
||||
/// <summary>EN: Circle members / VI: Các thành viên vòng tròn</summary>
|
||||
public IReadOnlyCollection<CircleMember> Members => _members.AsReadOnly();
|
||||
|
||||
/// <summary>EN: Active member count / VI: Số thành viên hoạt động</summary>
|
||||
public int ActiveMemberCount => _members.Count(m => m.IsActive);
|
||||
|
||||
/// <summary>EN: Is circle valid for bonus / VI: Vòng tròn có hợp lệ cho thưởng không</summary>
|
||||
public bool IsValid => Status == CircleStatus.Active && ActiveMemberCount >= MinMembers;
|
||||
|
||||
#endregion
|
||||
|
||||
// EF Core
|
||||
protected Circle() { }
|
||||
|
||||
#region Factory
|
||||
|
||||
/// <summary>
|
||||
/// EN: Create a new security circle.
|
||||
/// VI: Tạo vòng tròn an toàn mới.
|
||||
/// </summary>
|
||||
public static Circle Create(Guid ownerId, string name)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
throw new CircleDomainException("Circle name is required");
|
||||
|
||||
var circle = new Circle
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
OwnerId = ownerId,
|
||||
Name = name.Trim(),
|
||||
TrustScore = 0,
|
||||
BonusMultiplier = 0,
|
||||
Status = CircleStatus.Incomplete,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
UpdatedAt = DateTime.UtcNow
|
||||
};
|
||||
|
||||
// Add owner as first member
|
||||
var ownerMember = CircleMember.Create(circle.Id, ownerId);
|
||||
circle._members.Add(ownerMember);
|
||||
|
||||
return circle;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Member Management
|
||||
|
||||
/// <summary>
|
||||
/// EN: Add member to circle.
|
||||
/// VI: Thêm thành viên vào vòng tròn.
|
||||
/// </summary>
|
||||
public void AddMember(Guid minerId)
|
||||
{
|
||||
if (Status == CircleStatus.Disbanded)
|
||||
throw new CircleDomainException("Cannot add member to disbanded circle");
|
||||
|
||||
if (ActiveMemberCount >= MaxMembers)
|
||||
throw new CircleDomainException($"Circle cannot have more than {MaxMembers} members");
|
||||
|
||||
if (_members.Any(m => m.MinerId == minerId))
|
||||
throw new CircleDomainException("Miner is already a member of this circle");
|
||||
|
||||
var member = CircleMember.Create(Id, minerId);
|
||||
_members.Add(member);
|
||||
|
||||
RecalculateStatus();
|
||||
UpdatedAt = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Remove member from circle.
|
||||
/// VI: Xóa thành viên khỏi vòng tròn.
|
||||
/// </summary>
|
||||
public void RemoveMember(Guid minerId)
|
||||
{
|
||||
if (minerId == OwnerId)
|
||||
throw new CircleDomainException("Cannot remove circle owner");
|
||||
|
||||
var member = _members.FirstOrDefault(m => m.MinerId == minerId);
|
||||
if (member == null)
|
||||
throw new CircleDomainException("Miner is not a member of this circle");
|
||||
|
||||
member.Deactivate();
|
||||
RecalculateStatus();
|
||||
UpdatedAt = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Disband the circle.
|
||||
/// VI: Giải tán vòng tròn.
|
||||
/// </summary>
|
||||
public void Disband()
|
||||
{
|
||||
Status = CircleStatus.Disbanded;
|
||||
BonusMultiplier = 0;
|
||||
|
||||
foreach (var member in _members)
|
||||
member.Deactivate();
|
||||
|
||||
UpdatedAt = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Helpers
|
||||
|
||||
private void RecalculateStatus()
|
||||
{
|
||||
var wasActive = Status == CircleStatus.Active;
|
||||
|
||||
if (ActiveMemberCount >= MinMembers)
|
||||
{
|
||||
Status = CircleStatus.Active;
|
||||
BonusMultiplier = ValidCircleBonus;
|
||||
CalculateTrustScore();
|
||||
|
||||
if (!wasActive)
|
||||
{
|
||||
AddDomainEvent(new CircleCompletedDomainEvent(Id, OwnerId, ActiveMemberCount));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Status = CircleStatus.Incomplete;
|
||||
BonusMultiplier = 0;
|
||||
TrustScore = 0;
|
||||
}
|
||||
}
|
||||
|
||||
private void CalculateTrustScore()
|
||||
{
|
||||
// Simple trust score: based on member count and activity
|
||||
// Can be enhanced with more factors (member verify status, activity, etc.)
|
||||
TrustScore = ActiveMemberCount switch
|
||||
{
|
||||
3 => 60,
|
||||
4 => 80,
|
||||
5 => 100,
|
||||
_ => 0
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
namespace MiningService.Domain.AggregatesModel.CircleAggregate;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Security circle status.
|
||||
/// VI: Trạng thái vòng tròn an toàn.
|
||||
/// </summary>
|
||||
public enum CircleStatus
|
||||
{
|
||||
/// <summary>Less than 3 members, not valid / Ít hơn 3 thành viên, không hợp lệ</summary>
|
||||
Incomplete = 0,
|
||||
|
||||
/// <summary>3-5 members, valid for bonus / 3-5 thành viên, hợp lệ cho thưởng</summary>
|
||||
Active = 1,
|
||||
|
||||
/// <summary>Circle disbanded / Vòng tròn đã giải tán</summary>
|
||||
Disbanded = 2
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using MiningService.Domain.SeedWork;
|
||||
|
||||
namespace MiningService.Domain.AggregatesModel.CircleAggregate;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Circle member entity.
|
||||
/// VI: Entity thành viên vòng tròn.
|
||||
/// </summary>
|
||||
public class CircleMember : Entity
|
||||
{
|
||||
/// <summary>EN: Circle ID / VI: ID vòng tròn</summary>
|
||||
public Guid CircleId { get; private set; }
|
||||
|
||||
/// <summary>EN: Miner ID who is member / VI: ID thợ đào là thành viên</summary>
|
||||
public Guid MinerId { get; private set; }
|
||||
|
||||
/// <summary>EN: When member joined / VI: Khi thành viên tham gia</summary>
|
||||
public DateTime JoinedAt { get; private set; }
|
||||
|
||||
/// <summary>EN: Whether member is active / VI: Thành viên có hoạt động không</summary>
|
||||
public bool IsActive { get; private set; }
|
||||
|
||||
// EF Core
|
||||
protected CircleMember() { }
|
||||
|
||||
public static CircleMember Create(Guid circleId, Guid minerId)
|
||||
{
|
||||
return new CircleMember
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
CircleId = circleId,
|
||||
MinerId = minerId,
|
||||
JoinedAt = DateTime.UtcNow,
|
||||
IsActive = true
|
||||
};
|
||||
}
|
||||
|
||||
public void Deactivate() => IsActive = false;
|
||||
public void Activate() => IsActive = true;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using MiningService.Domain.SeedWork;
|
||||
|
||||
namespace MiningService.Domain.AggregatesModel.CircleAggregate;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Repository interface for Circle aggregate.
|
||||
/// VI: Interface repository cho Circle aggregate.
|
||||
/// </summary>
|
||||
public interface ICircleRepository : IRepository<Circle>
|
||||
{
|
||||
/// <summary>
|
||||
/// EN: Get circle by ID with members.
|
||||
/// VI: Lấy vòng tròn theo ID với thành viên.
|
||||
/// </summary>
|
||||
Task<Circle?> GetByIdAsync(Guid id, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// EN: Get circle by owner ID.
|
||||
/// VI: Lấy vòng tròn theo ID chủ sở hữu.
|
||||
/// </summary>
|
||||
Task<Circle?> GetByOwnerIdAsync(Guid ownerId, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// EN: Get circle that a miner is member of.
|
||||
/// VI: Lấy vòng tròn mà thợ đào là thành viên.
|
||||
/// </summary>
|
||||
Task<Circle?> GetByMemberIdAsync(Guid minerId, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// EN: Add new circle.
|
||||
/// VI: Thêm vòng tròn mới.
|
||||
/// </summary>
|
||||
Circle Add(Circle circle);
|
||||
|
||||
/// <summary>
|
||||
/// EN: Update existing circle.
|
||||
/// VI: Cập nhật vòng tròn hiện có.
|
||||
/// </summary>
|
||||
void Update(Circle circle);
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using MiningService.Domain.SeedWork;
|
||||
|
||||
namespace MiningService.Domain.AggregatesModel.MinerAggregate;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Repository interface for Miner aggregate.
|
||||
/// VI: Interface repository cho Miner aggregate.
|
||||
/// </summary>
|
||||
public interface IMinerRepository : IRepository<Miner>
|
||||
{
|
||||
/// <summary>
|
||||
/// EN: Get miner by ID with mining histories.
|
||||
/// VI: Lấy thợ đào theo ID với lịch sử đào.
|
||||
/// </summary>
|
||||
Task<Miner?> GetByIdAsync(Guid id, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// EN: Get miner by user ID.
|
||||
/// VI: Lấy thợ đào theo user ID.
|
||||
/// </summary>
|
||||
Task<Miner?> GetByUserIdAsync(Guid userId, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// EN: Get miner by referral code.
|
||||
/// VI: Lấy thợ đào theo mã giới thiệu.
|
||||
/// </summary>
|
||||
Task<Miner?> GetByReferralCodeAsync(string referralCode, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// EN: Add new miner.
|
||||
/// VI: Thêm thợ đào mới.
|
||||
/// </summary>
|
||||
Miner Add(Miner miner);
|
||||
|
||||
/// <summary>
|
||||
/// EN: Update existing miner.
|
||||
/// VI: Cập nhật thợ đào hiện có.
|
||||
/// </summary>
|
||||
void Update(Miner miner);
|
||||
|
||||
/// <summary>
|
||||
/// EN: Check if referral code exists.
|
||||
/// VI: Kiểm tra mã giới thiệu đã tồn tại chưa.
|
||||
/// </summary>
|
||||
Task<bool> ReferralCodeExistsAsync(string referralCode, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// EN: Get active referrals count for a miner.
|
||||
/// VI: Lấy số lượng giới thiệu hoạt động của thợ đào.
|
||||
/// </summary>
|
||||
Task<int> GetActiveReferralsCountAsync(Guid minerId, CancellationToken cancellationToken = default);
|
||||
}
|
||||
@@ -0,0 +1,359 @@
|
||||
using MiningService.Domain.Events;
|
||||
using MiningService.Domain.Exceptions;
|
||||
using MiningService.Domain.SeedWork;
|
||||
|
||||
namespace MiningService.Domain.AggregatesModel.MinerAggregate;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Miner Aggregate Root - represents a user's mining profile.
|
||||
/// VI: Miner Aggregate Root - đại diện cho hồ sơ đào của người dùng.
|
||||
/// </summary>
|
||||
public class Miner : Entity, IAggregateRoot
|
||||
{
|
||||
private readonly List<MiningHistory> _miningHistories = new();
|
||||
|
||||
#region Properties
|
||||
|
||||
/// <summary>EN: User ID from IAM Service / VI: User ID từ IAM Service</summary>
|
||||
public Guid UserId { get; private set; }
|
||||
|
||||
/// <summary>EN: User's mining role / VI: Vai trò đào của người dùng</summary>
|
||||
public MinerRole Role { get; private set; }
|
||||
|
||||
/// <summary>EN: Total mining points accumulated / VI: Tổng điểm đào tích lũy</summary>
|
||||
public decimal TotalMinedPoints { get; private set; }
|
||||
|
||||
/// <summary>EN: Current mining rate / VI: Tỷ lệ đào hiện tại</summary>
|
||||
public MiningRate CurrentRate { get; private set; } = MiningRate.CreateDefault();
|
||||
|
||||
/// <summary>EN: Current active mining session / VI: Phiên đào đang hoạt động</summary>
|
||||
public MiningSession? ActiveSession { get; private set; }
|
||||
|
||||
/// <summary>EN: Mining streak tracking / VI: Theo dõi streak đào</summary>
|
||||
public MiningStreak Streak { get; private set; } = MiningStreak.CreateNew();
|
||||
|
||||
/// <summary>EN: Unique referral code for this miner / VI: Mã giới thiệu duy nhất</summary>
|
||||
public string ReferralCode { get; private set; } = string.Empty;
|
||||
|
||||
/// <summary>EN: ID of miner who referred this user / VI: ID thợ đào giới thiệu người này</summary>
|
||||
public Guid? ReferredById { get; private set; }
|
||||
|
||||
/// <summary>EN: Security circle ID if member / VI: ID vòng tròn an toàn nếu là thành viên</summary>
|
||||
public Guid? CircleId { get; private set; }
|
||||
|
||||
/// <summary>EN: Account status / VI: Trạng thái tài khoản</summary>
|
||||
public MinerStatus Status { get; private set; }
|
||||
|
||||
/// <summary>EN: When account was created / VI: Khi tài khoản được tạo</summary>
|
||||
public DateTime CreatedAt { get; private set; }
|
||||
|
||||
/// <summary>EN: Last update time / VI: Thời gian cập nhật cuối</summary>
|
||||
public DateTime UpdatedAt { get; private set; }
|
||||
|
||||
/// <summary>EN: Concurrency token / VI: Token đồng thời</summary>
|
||||
public byte[] RowVersion { get; private set; } = Array.Empty<byte>();
|
||||
|
||||
/// <summary>EN: Mining history / VI: Lịch sử đào</summary>
|
||||
public IReadOnlyCollection<MiningHistory> MiningHistories => _miningHistories.AsReadOnly();
|
||||
|
||||
#endregion
|
||||
|
||||
// EF Core constructor
|
||||
protected Miner() { }
|
||||
|
||||
#region Factory Methods
|
||||
|
||||
/// <summary>
|
||||
/// EN: Create a new miner profile.
|
||||
/// VI: Tạo hồ sơ thợ đào mới.
|
||||
/// </summary>
|
||||
public static Miner Create(Guid userId, string? referralCode = null, Guid? referredById = null)
|
||||
{
|
||||
var miner = new Miner
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
UserId = userId,
|
||||
Role = MinerRole.Pioneer,
|
||||
TotalMinedPoints = 0,
|
||||
CurrentRate = MiningRate.CreateDefault(),
|
||||
ActiveSession = null,
|
||||
Streak = MiningStreak.CreateNew(),
|
||||
ReferralCode = GenerateReferralCode(),
|
||||
ReferredById = referredById,
|
||||
CircleId = null,
|
||||
Status = MinerStatus.Active,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
UpdatedAt = DateTime.UtcNow
|
||||
};
|
||||
|
||||
miner.AddDomainEvent(new MinerCreatedDomainEvent(miner.Id, userId));
|
||||
return miner;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Mining Session Methods
|
||||
|
||||
/// <summary>
|
||||
/// EN: Start a new mining session.
|
||||
/// VI: Bắt đầu phiên đào mới.
|
||||
/// </summary>
|
||||
public MiningSession StartMiningSession(decimal configBaseRate = 0.25m, int sessionHours = 24)
|
||||
{
|
||||
if (Status != MinerStatus.Active)
|
||||
throw new MiningDomainException("Cannot start mining: account is not active");
|
||||
|
||||
if (ActiveSession != null && ActiveSession.Status == MiningSessionStatus.Active)
|
||||
throw new MiningDomainException("Cannot start mining: active session exists");
|
||||
|
||||
// Recalculate rate with current bonuses
|
||||
RecalculateRate(configBaseRate);
|
||||
|
||||
// Create new session
|
||||
ActiveSession = MiningSession.Create(CurrentRate.TotalRate, sessionHours);
|
||||
UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
AddDomainEvent(new MiningSessionStartedDomainEvent(Id, ActiveSession.SessionId, CurrentRate.TotalRate));
|
||||
return ActiveSession;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Claim mining reward from completed session.
|
||||
/// VI: Nhận thưởng đào từ phiên hoàn thành.
|
||||
/// </summary>
|
||||
public decimal ClaimMiningReward()
|
||||
{
|
||||
if (ActiveSession == null)
|
||||
throw new MiningDomainException("No active session to claim");
|
||||
|
||||
if (!ActiveSession.IsReadyToClaim)
|
||||
throw new MiningDomainException("Session is not ready to claim yet");
|
||||
|
||||
// Calculate earned points
|
||||
var earnedPoints = ActiveSession.CalculateEarnedPoints();
|
||||
|
||||
// Update streak
|
||||
var previousStreak = Streak.CurrentStreak;
|
||||
Streak = Streak.IncrementStreak();
|
||||
|
||||
// Check for milestone rewards
|
||||
var milestoneBonus = CalculateMilestoneBonus(Streak.CurrentStreak, previousStreak);
|
||||
var totalPoints = earnedPoints + milestoneBonus;
|
||||
|
||||
// Add to total
|
||||
TotalMinedPoints += totalPoints;
|
||||
|
||||
// Create history entry
|
||||
var history = MiningHistory.CreateFromSession(
|
||||
Id,
|
||||
earnedPoints,
|
||||
ActiveSession.SessionId,
|
||||
ActiveSession.HourlyRate,
|
||||
Streak.CurrentStreak
|
||||
);
|
||||
_miningHistories.Add(history);
|
||||
|
||||
// If milestone bonus, create separate entry
|
||||
if (milestoneBonus > 0)
|
||||
{
|
||||
var bonusHistory = MiningHistory.CreateFromBonus(Id, milestoneBonus, $"Streak Milestone Day {Streak.CurrentStreak}");
|
||||
_miningHistories.Add(bonusHistory);
|
||||
}
|
||||
|
||||
// Mark session as claimed
|
||||
ActiveSession = ActiveSession.MarkAsClaimed(earnedPoints);
|
||||
UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
AddDomainEvent(new PointsMinedDomainEvent(Id, totalPoints, TotalMinedPoints, Streak.CurrentStreak));
|
||||
return totalPoints;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Rate Calculation
|
||||
|
||||
/// <summary>
|
||||
/// EN: Recalculate mining rate based on current bonuses.
|
||||
/// VI: Tính lại tỷ lệ đào dựa trên các thưởng hiện tại.
|
||||
/// </summary>
|
||||
public void RecalculateRate(
|
||||
decimal baseRate = 0.25m,
|
||||
decimal circleBonus = 0,
|
||||
decimal referralBonus = 0)
|
||||
{
|
||||
var roleBonus = Role switch
|
||||
{
|
||||
MinerRole.Pioneer => 0m,
|
||||
MinerRole.Contributor => 0.10m,
|
||||
MinerRole.Ambassador => 0.25m,
|
||||
MinerRole.NodeOperator => 0.50m,
|
||||
_ => 0m
|
||||
};
|
||||
|
||||
CurrentRate = new MiningRate
|
||||
{
|
||||
BaseRate = baseRate,
|
||||
RoleBonus = roleBonus,
|
||||
CircleBonus = circleBonus,
|
||||
ReferralBonus = referralBonus,
|
||||
StreakBonus = Streak.BonusMultiplier
|
||||
};
|
||||
|
||||
UpdatedAt = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Role & Status Management
|
||||
|
||||
/// <summary>
|
||||
/// EN: Upgrade miner role.
|
||||
/// VI: Nâng cấp vai trò thợ đào.
|
||||
/// </summary>
|
||||
public void UpgradeRole(MinerRole newRole)
|
||||
{
|
||||
if (newRole <= Role)
|
||||
throw new MiningDomainException("Cannot downgrade role");
|
||||
|
||||
Role = newRole;
|
||||
UpdatedAt = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Join a security circle.
|
||||
/// VI: Tham gia vòng tròn an toàn.
|
||||
/// </summary>
|
||||
public void JoinCircle(Guid circleId)
|
||||
{
|
||||
if (CircleId.HasValue)
|
||||
throw new MiningDomainException("Already in a circle");
|
||||
|
||||
CircleId = circleId;
|
||||
|
||||
// Upgrade to Contributor if still Pioneer
|
||||
if (Role == MinerRole.Pioneer)
|
||||
Role = MinerRole.Contributor;
|
||||
|
||||
UpdatedAt = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Leave current security circle.
|
||||
/// VI: Rời vòng tròn an toàn hiện tại.
|
||||
/// </summary>
|
||||
public void LeaveCircle()
|
||||
{
|
||||
CircleId = null;
|
||||
|
||||
// Downgrade from Contributor if no other qualifications
|
||||
if (Role == MinerRole.Contributor)
|
||||
Role = MinerRole.Pioneer;
|
||||
|
||||
UpdatedAt = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Suspend miner account.
|
||||
/// VI: Tạm ngừng tài khoản thợ đào.
|
||||
/// </summary>
|
||||
public void Suspend()
|
||||
{
|
||||
if (Status == MinerStatus.Banned)
|
||||
throw new MiningDomainException("Cannot suspend banned account");
|
||||
|
||||
Status = MinerStatus.Suspended;
|
||||
UpdatedAt = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Restore suspended account.
|
||||
/// VI: Khôi phục tài khoản bị tạm ngừng.
|
||||
/// </summary>
|
||||
public void Restore()
|
||||
{
|
||||
if (Status != MinerStatus.Suspended)
|
||||
throw new MiningDomainException("Can only restore suspended accounts");
|
||||
|
||||
Status = MinerStatus.Active;
|
||||
UpdatedAt = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Ban miner account permanently.
|
||||
/// VI: Cấm tài khoản thợ đào vĩnh viễn.
|
||||
/// </summary>
|
||||
public void Ban()
|
||||
{
|
||||
Status = MinerStatus.Banned;
|
||||
UpdatedAt = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Points Management
|
||||
|
||||
/// <summary>
|
||||
/// EN: Add bonus points (from admin, referral, etc).
|
||||
/// VI: Thêm điểm thưởng (từ admin, giới thiệu, v.v.).
|
||||
/// </summary>
|
||||
public void AddBonusPoints(decimal points, string source)
|
||||
{
|
||||
if (points <= 0)
|
||||
throw new MiningDomainException("Bonus points must be positive");
|
||||
|
||||
TotalMinedPoints += points;
|
||||
|
||||
var history = MiningHistory.CreateFromBonus(Id, points, source);
|
||||
_miningHistories.Add(history);
|
||||
|
||||
UpdatedAt = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Deduct points (for recovery, purchases, etc).
|
||||
/// VI: Trừ điểm (cho khôi phục, mua sắm, v.v.).
|
||||
/// </summary>
|
||||
public void DeductPoints(decimal points, string reason)
|
||||
{
|
||||
if (points <= 0)
|
||||
throw new MiningDomainException("Deduction must be positive");
|
||||
|
||||
if (TotalMinedPoints < points)
|
||||
throw new MiningDomainException("Insufficient points");
|
||||
|
||||
TotalMinedPoints -= points;
|
||||
|
||||
var history = MiningHistory.CreateFromBonus(Id, -points, reason);
|
||||
_miningHistories.Add(history);
|
||||
|
||||
UpdatedAt = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Helpers
|
||||
|
||||
private static string GenerateReferralCode()
|
||||
{
|
||||
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
var random = new Random();
|
||||
return new string(Enumerable.Repeat(chars, 8).Select(s => s[random.Next(s.Length)]).ToArray());
|
||||
}
|
||||
|
||||
private static decimal CalculateMilestoneBonus(int newStreak, int previousStreak)
|
||||
{
|
||||
decimal bonus = 0;
|
||||
|
||||
// Check milestones crossed
|
||||
if (newStreak >= 3 && previousStreak < 3) bonus += 0; // Badge only
|
||||
if (newStreak >= 7 && previousStreak < 7) bonus += 50;
|
||||
if (newStreak >= 14 && previousStreak < 14) bonus += 100;
|
||||
if (newStreak >= 30 && previousStreak < 30) bonus += 300;
|
||||
if (newStreak >= 60 && previousStreak < 60) bonus += 500;
|
||||
if (newStreak >= 90 && previousStreak < 90) bonus += 1000;
|
||||
|
||||
return bonus;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
namespace MiningService.Domain.AggregatesModel.MinerAggregate;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Miner role determines bonus multiplier.
|
||||
/// VI: Vai trò thợ đào xác định hệ số thưởng.
|
||||
/// </summary>
|
||||
public enum MinerRole
|
||||
{
|
||||
/// <summary>Base user, no bonus / Người dùng cơ bản, không có thưởng</summary>
|
||||
Pioneer = 0,
|
||||
|
||||
/// <summary>Has valid circle, +10% / Có vòng tròn hợp lệ, +10%</summary>
|
||||
Contributor = 1,
|
||||
|
||||
/// <summary>5+ referrals, +25% / 5+ giới thiệu, +25%</summary>
|
||||
Ambassador = 2,
|
||||
|
||||
/// <summary>Runs node software, +50% / Chạy phần mềm node, +50%</summary>
|
||||
NodeOperator = 3
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Miner account status.
|
||||
/// VI: Trạng thái tài khoản thợ đào.
|
||||
/// </summary>
|
||||
public enum MinerStatus
|
||||
{
|
||||
/// <summary>Normal active account / Tài khoản hoạt động bình thường</summary>
|
||||
Active = 0,
|
||||
|
||||
/// <summary>Temporarily suspended / Tạm ngừng</summary>
|
||||
Suspended = 1,
|
||||
|
||||
/// <summary>Permanently banned / Cấm vĩnh viễn</summary>
|
||||
Banned = 2
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Mining session status.
|
||||
/// VI: Trạng thái phiên đào.
|
||||
/// </summary>
|
||||
public enum MiningSessionStatus
|
||||
{
|
||||
/// <summary>Session is currently active / Phiên đang hoạt động</summary>
|
||||
Active = 0,
|
||||
|
||||
/// <summary>Session completed, ready to claim / Phiên hoàn thành, sẵn sàng nhận thưởng</summary>
|
||||
Completed = 1,
|
||||
|
||||
/// <summary>Session claimed / Phiên đã nhận thưởng</summary>
|
||||
Claimed = 2,
|
||||
|
||||
/// <summary>Session expired without claim / Phiên hết hạn chưa nhận</summary>
|
||||
Expired = 3
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
using MiningService.Domain.SeedWork;
|
||||
|
||||
namespace MiningService.Domain.AggregatesModel.MinerAggregate;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Value object representing mining rate calculation components.
|
||||
/// VI: Value object đại diện cho các thành phần tính toán tỷ lệ đào.
|
||||
/// </summary>
|
||||
public record MiningRate
|
||||
{
|
||||
/// <summary>EN: Base rate in MP/hour / VI: Tỷ lệ cơ bản MP/giờ</summary>
|
||||
public decimal BaseRate { get; init; }
|
||||
|
||||
/// <summary>EN: Role bonus percentage / VI: Phần trăm thưởng vai trò</summary>
|
||||
public decimal RoleBonus { get; init; }
|
||||
|
||||
/// <summary>EN: Circle bonus percentage / VI: Phần trăm thưởng vòng tròn</summary>
|
||||
public decimal CircleBonus { get; init; }
|
||||
|
||||
/// <summary>EN: Referral bonus percentage / VI: Phần trăm thưởng giới thiệu</summary>
|
||||
public decimal ReferralBonus { get; init; }
|
||||
|
||||
/// <summary>EN: Streak bonus percentage / VI: Phần trăm thưởng streak</summary>
|
||||
public decimal StreakBonus { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// EN: Calculated total rate in MP/hour.
|
||||
/// VI: Tỷ lệ tổng tính toán được MP/giờ.
|
||||
/// Formula: BaseRate × (1 + Role) × (1 + Circle) × (1 + Referral) × (1 + Streak)
|
||||
/// </summary>
|
||||
public decimal TotalRate => BaseRate
|
||||
* (1 + RoleBonus)
|
||||
* (1 + CircleBonus)
|
||||
* (1 + ReferralBonus)
|
||||
* (1 + StreakBonus);
|
||||
|
||||
/// <summary>EN: Daily earnings / VI: Thu nhập hàng ngày</summary>
|
||||
public decimal DailyRate => TotalRate * 24;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Create a new mining rate with default base rate.
|
||||
/// VI: Tạo mining rate mới với tỷ lệ cơ bản mặc định.
|
||||
/// </summary>
|
||||
public static MiningRate CreateDefault(decimal baseRate = 0.25m) => new()
|
||||
{
|
||||
BaseRate = baseRate,
|
||||
RoleBonus = 0,
|
||||
CircleBonus = 0,
|
||||
ReferralBonus = 0,
|
||||
StreakBonus = 0
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Value object representing mining streak tracking.
|
||||
/// VI: Value object theo dõi streak đào liên tục.
|
||||
/// </summary>
|
||||
public record MiningStreak
|
||||
{
|
||||
/// <summary>EN: Current consecutive mining days / VI: Số ngày đào liên tục hiện tại</summary>
|
||||
public int CurrentStreak { get; init; }
|
||||
|
||||
/// <summary>EN: Personal best streak / VI: Kỷ lục streak cá nhân</summary>
|
||||
public int LongestStreak { get; init; }
|
||||
|
||||
/// <summary>EN: Last successful mining date / VI: Ngày đào thành công cuối cùng</summary>
|
||||
public DateTime LastMiningDate { get; init; }
|
||||
|
||||
/// <summary>EN: Available freeze tokens / VI: Số token đóng băng khả dụng</summary>
|
||||
public int FreezeTokens { get; init; }
|
||||
|
||||
/// <summary>EN: Is in grace period (missed 1 day) / VI: Đang trong thời gian ân hạn</summary>
|
||||
public bool IsGracePeriod { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// EN: Get bonus multiplier based on streak tier.
|
||||
/// VI: Lấy hệ số thưởng dựa trên cấp streak.
|
||||
/// </summary>
|
||||
public decimal BonusMultiplier => CurrentStreak switch
|
||||
{
|
||||
< 3 => 0m, // Day 1-2: 0%
|
||||
< 7 => 0.10m, // Day 3-6: +10%
|
||||
< 14 => 0.25m, // Day 7-13: +25%
|
||||
< 30 => 0.50m, // Day 14-29: +50%
|
||||
< 60 => 1.00m, // Day 30-59: +100%
|
||||
< 90 => 1.25m, // Day 60-89: +125%
|
||||
_ => 1.50m // Day 90+: +150%
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// EN: Create initial streak for new miner.
|
||||
/// VI: Tạo streak ban đầu cho thợ đào mới.
|
||||
/// </summary>
|
||||
public static MiningStreak CreateNew() => new()
|
||||
{
|
||||
CurrentStreak = 0,
|
||||
LongestStreak = 0,
|
||||
LastMiningDate = DateTime.MinValue,
|
||||
FreezeTokens = 0,
|
||||
IsGracePeriod = false
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// EN: Increment streak after successful claim.
|
||||
/// VI: Tăng streak sau khi nhận thưởng thành công.
|
||||
/// </summary>
|
||||
public MiningStreak IncrementStreak()
|
||||
{
|
||||
var newStreak = CurrentStreak + 1;
|
||||
var earnedToken = newStreak % 7 == 0; // Earn 1 token per 7 days
|
||||
|
||||
return this with
|
||||
{
|
||||
CurrentStreak = newStreak,
|
||||
LongestStreak = Math.Max(LongestStreak, newStreak),
|
||||
LastMiningDate = DateTime.UtcNow,
|
||||
FreezeTokens = earnedToken ? FreezeTokens + 1 : FreezeTokens,
|
||||
IsGracePeriod = false
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Reset streak (missed beyond grace period).
|
||||
/// VI: Reset streak (bỏ lỡ quá thời gian ân hạn).
|
||||
/// </summary>
|
||||
public MiningStreak Reset() => this with
|
||||
{
|
||||
CurrentStreak = 0,
|
||||
IsGracePeriod = false
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Value object representing a mining session.
|
||||
/// VI: Value object đại diện cho phiên đào.
|
||||
/// </summary>
|
||||
public record MiningSession
|
||||
{
|
||||
/// <summary>EN: Session unique identifier / VI: Định danh phiên duy nhất</summary>
|
||||
public Guid SessionId { get; init; }
|
||||
|
||||
/// <summary>EN: Session start time / VI: Thời gian bắt đầu phiên</summary>
|
||||
public DateTime StartTime { get; init; }
|
||||
|
||||
/// <summary>EN: Session end time / VI: Thời gian kết thúc phiên</summary>
|
||||
public DateTime EndTime { get; init; }
|
||||
|
||||
/// <summary>EN: Mining rate at session start / VI: Tỷ lệ đào khi bắt đầu phiên</summary>
|
||||
public decimal HourlyRate { get; init; }
|
||||
|
||||
/// <summary>EN: Current session status / VI: Trạng thái phiên hiện tại</summary>
|
||||
public MiningSessionStatus Status { get; init; }
|
||||
|
||||
/// <summary>EN: Points accumulated in this session / VI: Điểm tích lũy trong phiên</summary>
|
||||
public decimal AccumulatedPoints { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// EN: Create a new 24-hour mining session.
|
||||
/// VI: Tạo phiên đào 24 giờ mới.
|
||||
/// </summary>
|
||||
public static MiningSession Create(decimal hourlyRate, int sessionHours = 24)
|
||||
{
|
||||
var now = DateTime.UtcNow;
|
||||
return new MiningSession
|
||||
{
|
||||
SessionId = Guid.NewGuid(),
|
||||
StartTime = now,
|
||||
EndTime = now.AddHours(sessionHours),
|
||||
HourlyRate = hourlyRate,
|
||||
Status = MiningSessionStatus.Active,
|
||||
AccumulatedPoints = 0
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Check if session is ready to claim.
|
||||
/// VI: Kiểm tra phiên đã sẵn sàng để nhận thưởng chưa.
|
||||
/// </summary>
|
||||
public bool IsReadyToClaim => Status == MiningSessionStatus.Active && DateTime.UtcNow >= EndTime;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Calculate earned points for completed session.
|
||||
/// VI: Tính điểm kiếm được cho phiên hoàn thành.
|
||||
/// </summary>
|
||||
public decimal CalculateEarnedPoints()
|
||||
{
|
||||
if (Status != MiningSessionStatus.Active)
|
||||
return AccumulatedPoints;
|
||||
|
||||
var elapsedHours = (decimal)(DateTime.UtcNow - StartTime).TotalHours;
|
||||
var cappedHours = Math.Min(elapsedHours, 24); // Cap at 24 hours
|
||||
return Math.Round(cappedHours * HourlyRate, 4);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Mark session as claimed with final points.
|
||||
/// VI: Đánh dấu phiên đã nhận với điểm cuối cùng.
|
||||
/// </summary>
|
||||
public MiningSession MarkAsClaimed(decimal earnedPoints) => this with
|
||||
{
|
||||
Status = MiningSessionStatus.Claimed,
|
||||
AccumulatedPoints = earnedPoints
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
using MiningService.Domain.SeedWork;
|
||||
|
||||
namespace MiningService.Domain.AggregatesModel.MinerAggregate;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Entity tracking mining history entries.
|
||||
/// VI: Entity theo dõi các bản ghi lịch sử đào.
|
||||
/// </summary>
|
||||
public class MiningHistory : Entity
|
||||
{
|
||||
/// <summary>EN: Miner who earned these points / VI: Thợ đào kiếm được điểm này</summary>
|
||||
public Guid MinerId { get; private set; }
|
||||
|
||||
/// <summary>EN: Points earned in this entry / VI: Điểm kiếm được trong bản ghi này</summary>
|
||||
public decimal PointsEarned { get; private set; }
|
||||
|
||||
/// <summary>EN: Source of points / VI: Nguồn điểm</summary>
|
||||
public string Source { get; private set; } = string.Empty;
|
||||
|
||||
/// <summary>EN: Session ID if from mining / VI: ID phiên nếu từ đào</summary>
|
||||
public Guid? SessionId { get; private set; }
|
||||
|
||||
/// <summary>EN: When points were earned / VI: Khi nào điểm được kiếm</summary>
|
||||
public DateTime EarnedAt { get; private set; }
|
||||
|
||||
/// <summary>EN: Mining rate at time of earning / VI: Tỷ lệ đào khi kiếm điểm</summary>
|
||||
public decimal HourlyRateSnapshot { get; private set; }
|
||||
|
||||
/// <summary>EN: Streak day at time of earning / VI: Ngày streak khi kiếm điểm</summary>
|
||||
public int StreakDaySnapshot { get; private set; }
|
||||
|
||||
// EF Core constructor
|
||||
protected MiningHistory() { }
|
||||
|
||||
/// <summary>
|
||||
/// EN: Create mining history from session claim.
|
||||
/// VI: Tạo lịch sử đào từ việc nhận thưởng phiên.
|
||||
/// </summary>
|
||||
public static MiningHistory CreateFromSession(
|
||||
Guid minerId,
|
||||
decimal pointsEarned,
|
||||
Guid sessionId,
|
||||
decimal hourlyRate,
|
||||
int streakDay)
|
||||
{
|
||||
return new MiningHistory
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
MinerId = minerId,
|
||||
PointsEarned = pointsEarned,
|
||||
Source = "Mining",
|
||||
SessionId = sessionId,
|
||||
EarnedAt = DateTime.UtcNow,
|
||||
HourlyRateSnapshot = hourlyRate,
|
||||
StreakDaySnapshot = streakDay
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Create mining history from bonus (referral, milestone, etc).
|
||||
/// VI: Tạo lịch sử đào từ thưởng (giới thiệu, mốc, v.v.).
|
||||
/// </summary>
|
||||
public static MiningHistory CreateFromBonus(
|
||||
Guid minerId,
|
||||
decimal pointsEarned,
|
||||
string source)
|
||||
{
|
||||
return new MiningHistory
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
MinerId = minerId,
|
||||
PointsEarned = pointsEarned,
|
||||
Source = source,
|
||||
SessionId = null,
|
||||
EarnedAt = DateTime.UtcNow,
|
||||
HourlyRateSnapshot = 0,
|
||||
StreakDaySnapshot = 0
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using MiningService.Domain.SeedWork;
|
||||
|
||||
namespace MiningService.Domain.AggregatesModel.ReferralAggregate;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Repository interface for Referral aggregate.
|
||||
/// VI: Interface repository cho Referral aggregate.
|
||||
/// </summary>
|
||||
public interface IReferralRepository : IRepository<Referral>
|
||||
{
|
||||
/// <summary>
|
||||
/// EN: Get referral by ID.
|
||||
/// VI: Lấy giới thiệu theo ID.
|
||||
/// </summary>
|
||||
Task<Referral?> GetByIdAsync(Guid id, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// EN: Get all referrals made by a miner.
|
||||
/// VI: Lấy tất cả giới thiệu bởi một thợ đào.
|
||||
/// </summary>
|
||||
Task<List<Referral>> GetByReferrerIdAsync(Guid referrerId, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// EN: Get active referrals count for a miner.
|
||||
/// VI: Lấy số lượng giới thiệu hoạt động của thợ đào.
|
||||
/// </summary>
|
||||
Task<int> GetActiveCountByReferrerIdAsync(Guid referrerId, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// EN: Get referral by referred miner ID.
|
||||
/// VI: Lấy giới thiệu theo ID thợ đào được giới thiệu.
|
||||
/// </summary>
|
||||
Task<Referral?> GetByReferredIdAsync(Guid referredId, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// EN: Add new referral.
|
||||
/// VI: Thêm giới thiệu mới.
|
||||
/// </summary>
|
||||
Referral Add(Referral referral);
|
||||
|
||||
/// <summary>
|
||||
/// EN: Update existing referral.
|
||||
/// VI: Cập nhật giới thiệu hiện có.
|
||||
/// </summary>
|
||||
void Update(Referral referral);
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
using MiningService.Domain.Events;
|
||||
using MiningService.Domain.Exceptions;
|
||||
using MiningService.Domain.SeedWork;
|
||||
|
||||
namespace MiningService.Domain.AggregatesModel.ReferralAggregate;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Referral Aggregate Root - tracks referral relationships.
|
||||
/// VI: Referral Aggregate Root - theo dõi quan hệ giới thiệu.
|
||||
/// </summary>
|
||||
public class Referral : Entity, IAggregateRoot
|
||||
{
|
||||
#region Properties
|
||||
|
||||
/// <summary>EN: Miner who made the referral / VI: Thợ đào thực hiện giới thiệu</summary>
|
||||
public Guid ReferrerId { get; private set; }
|
||||
|
||||
/// <summary>EN: Miner who was referred / VI: Thợ đào được giới thiệu</summary>
|
||||
public Guid ReferredId { get; private set; }
|
||||
|
||||
/// <summary>EN: Referral code used / VI: Mã giới thiệu đã sử dụng</summary>
|
||||
public string ReferralCode { get; private set; } = string.Empty;
|
||||
|
||||
/// <summary>EN: Bonus rate for referrer (e.g., 0.25 = 25%) / VI: Tỷ lệ thưởng cho người giới thiệu</summary>
|
||||
public decimal BonusRate { get; private set; }
|
||||
|
||||
/// <summary>EN: Whether referral is active (KYC verified) / VI: Giới thiệu có hoạt động không (KYC đã xác minh)</summary>
|
||||
public bool IsActive { get; private set; }
|
||||
|
||||
/// <summary>EN: Referral level (1 = direct) / VI: Cấp giới thiệu (1 = trực tiếp)</summary>
|
||||
public int Level { get; private set; }
|
||||
|
||||
/// <summary>EN: When referral was created / VI: Khi giới thiệu được tạo</summary>
|
||||
public DateTime CreatedAt { get; private set; }
|
||||
|
||||
/// <summary>EN: When referral was activated / VI: Khi giới thiệu được kích hoạt</summary>
|
||||
public DateTime? ActivatedAt { get; private set; }
|
||||
|
||||
#endregion
|
||||
|
||||
// EF Core
|
||||
protected Referral() { }
|
||||
|
||||
#region Factory
|
||||
|
||||
/// <summary>
|
||||
/// EN: Create a new referral (inactive until KYC).
|
||||
/// VI: Tạo giới thiệu mới (chưa hoạt động cho đến khi KYC).
|
||||
/// </summary>
|
||||
public static Referral Create(
|
||||
Guid referrerId,
|
||||
Guid referredId,
|
||||
string referralCode,
|
||||
decimal bonusRate = 0.25m,
|
||||
int level = 1)
|
||||
{
|
||||
if (referrerId == referredId)
|
||||
throw new ReferralDomainException("Cannot refer yourself");
|
||||
|
||||
return new Referral
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
ReferrerId = referrerId,
|
||||
ReferredId = referredId,
|
||||
ReferralCode = referralCode,
|
||||
BonusRate = bonusRate,
|
||||
IsActive = false, // Inactive until KYC
|
||||
Level = level,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
ActivatedAt = null
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <summary>
|
||||
/// EN: Activate referral (after KYC verification).
|
||||
/// VI: Kích hoạt giới thiệu (sau khi xác minh KYC).
|
||||
/// </summary>
|
||||
public void Activate()
|
||||
{
|
||||
if (IsActive)
|
||||
throw new ReferralDomainException("Referral is already active");
|
||||
|
||||
IsActive = true;
|
||||
ActivatedAt = DateTime.UtcNow;
|
||||
|
||||
AddDomainEvent(new ReferralActivatedDomainEvent(Id, ReferrerId, ReferredId));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Deactivate referral (fraud, account ban, etc).
|
||||
/// VI: Hủy kích hoạt giới thiệu (gian lận, cấm tài khoản, v.v.).
|
||||
/// </summary>
|
||||
public void Deactivate()
|
||||
{
|
||||
IsActive = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Calculate bonus amount for referrer.
|
||||
/// VI: Tính số tiền thưởng cho người giới thiệu.
|
||||
/// </summary>
|
||||
public decimal CalculateBonus(decimal baseRate)
|
||||
{
|
||||
if (!IsActive) return 0;
|
||||
return baseRate * BonusRate;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
using MediatR;
|
||||
|
||||
namespace MiningService.Domain.Events;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Event raised when a new miner profile is created.
|
||||
/// VI: Sự kiện khi hồ sơ thợ đào mới được tạo.
|
||||
/// </summary>
|
||||
public record MinerCreatedDomainEvent(Guid MinerId, Guid UserId) : INotification;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Event raised when a mining session is started.
|
||||
/// VI: Sự kiện khi phiên đào được bắt đầu.
|
||||
/// </summary>
|
||||
public record MiningSessionStartedDomainEvent(
|
||||
Guid MinerId,
|
||||
Guid SessionId,
|
||||
decimal HourlyRate) : INotification;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Event raised when points are mined (session claimed).
|
||||
/// VI: Sự kiện khi điểm được đào (phiên được nhận thưởng).
|
||||
/// </summary>
|
||||
public record PointsMinedDomainEvent(
|
||||
Guid MinerId,
|
||||
decimal PointsEarned,
|
||||
decimal TotalPoints,
|
||||
int StreakDays) : INotification;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Event raised when streak tier changes.
|
||||
/// VI: Sự kiện khi cấp streak thay đổi.
|
||||
/// </summary>
|
||||
public record StreakUpdatedDomainEvent(
|
||||
Guid MinerId,
|
||||
int PreviousStreak,
|
||||
int NewStreak,
|
||||
decimal NewBonusMultiplier) : INotification;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Event raised when a security circle is completed (3+ members).
|
||||
/// VI: Sự kiện khi vòng tròn an toàn hoàn thành (3+ thành viên).
|
||||
/// </summary>
|
||||
public record CircleCompletedDomainEvent(
|
||||
Guid CircleId,
|
||||
Guid OwnerId,
|
||||
int MemberCount) : INotification;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Event raised when a referral is activated (KYC verified).
|
||||
/// VI: Sự kiện khi giới thiệu được kích hoạt (KYC đã xác minh).
|
||||
/// </summary>
|
||||
public record ReferralActivatedDomainEvent(
|
||||
Guid ReferralId,
|
||||
Guid ReferrerId,
|
||||
Guid ReferredId) : INotification;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Event raised when admin updates system configuration.
|
||||
/// VI: Sự kiện khi admin cập nhật cấu hình hệ thống.
|
||||
/// </summary>
|
||||
public record ConfigurationUpdatedDomainEvent(
|
||||
string ConfigType,
|
||||
Guid UpdatedBy,
|
||||
DateTime UpdatedAt) : INotification;
|
||||
@@ -0,0 +1,45 @@
|
||||
namespace MiningService.Domain.Exceptions;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Base exception for mining domain errors.
|
||||
/// VI: Exception cơ sở cho lỗi domain mining.
|
||||
/// </summary>
|
||||
public class MiningDomainException : Exception
|
||||
{
|
||||
public MiningDomainException() { }
|
||||
|
||||
public MiningDomainException(string message) : base(message) { }
|
||||
|
||||
public MiningDomainException(string message, Exception innerException)
|
||||
: base(message, innerException) { }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Exception when miner is not found.
|
||||
/// VI: Exception khi không tìm thấy thợ đào.
|
||||
/// </summary>
|
||||
public class MinerNotFoundException : MiningDomainException
|
||||
{
|
||||
public MinerNotFoundException(Guid minerId)
|
||||
: base($"Miner with ID {minerId} was not found") { }
|
||||
|
||||
public MinerNotFoundException(string message) : base(message) { }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Exception when circle operation fails.
|
||||
/// VI: Exception khi thao tác vòng tròn thất bại.
|
||||
/// </summary>
|
||||
public class CircleDomainException : MiningDomainException
|
||||
{
|
||||
public CircleDomainException(string message) : base(message) { }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Exception when referral operation fails.
|
||||
/// VI: Exception khi thao tác giới thiệu thất bại.
|
||||
/// </summary>
|
||||
public class ReferralDomainException : MiningDomainException
|
||||
{
|
||||
public ReferralDomainException(string message) : base(message) { }
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<AssemblyName>MiningService.Domain</AssemblyName>
|
||||
<RootNamespace>MiningService.Domain</RootNamespace>
|
||||
<Description>Domain layer containing core business logic and entities</Description>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<!-- EN: MediatR for domain events / VI: MediatR cho domain events -->
|
||||
<PackageReference Include="MediatR.Contracts" Version="2.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,102 @@
|
||||
using MediatR;
|
||||
|
||||
namespace MiningService.Domain.SeedWork;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Base class for all domain entities.
|
||||
/// VI: Lớp cơ sở cho tất cả các entity trong domain.
|
||||
/// </summary>
|
||||
public abstract class Entity
|
||||
{
|
||||
private int? _requestedHashCode;
|
||||
private Guid _id;
|
||||
private List<INotification> _domainEvents = new();
|
||||
|
||||
/// <summary>
|
||||
/// EN: Unique identifier for the entity.
|
||||
/// VI: Định danh duy nhất cho entity.
|
||||
/// </summary>
|
||||
public virtual Guid Id
|
||||
{
|
||||
get => _id;
|
||||
protected set => _id = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Domain events raised by this entity.
|
||||
/// VI: Các domain event được phát ra bởi entity này.
|
||||
/// </summary>
|
||||
public IReadOnlyCollection<INotification> DomainEvents => _domainEvents.AsReadOnly();
|
||||
|
||||
/// <summary>
|
||||
/// EN: Add a domain event to be dispatched.
|
||||
/// VI: Thêm một domain event để dispatch.
|
||||
/// </summary>
|
||||
public void AddDomainEvent(INotification eventItem)
|
||||
{
|
||||
_domainEvents.Add(eventItem);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Remove a domain event.
|
||||
/// VI: Xóa một domain event.
|
||||
/// </summary>
|
||||
public void RemoveDomainEvent(INotification eventItem)
|
||||
{
|
||||
_domainEvents.Remove(eventItem);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Clear all domain events.
|
||||
/// VI: Xóa tất cả domain events.
|
||||
/// </summary>
|
||||
public void ClearDomainEvents()
|
||||
{
|
||||
_domainEvents.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Check if entity is transient (not persisted yet).
|
||||
/// VI: Kiểm tra xem entity có phải là transient (chưa lưu) không.
|
||||
/// </summary>
|
||||
public bool IsTransient()
|
||||
{
|
||||
return Id == default;
|
||||
}
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
if (obj is not Entity item)
|
||||
return false;
|
||||
|
||||
if (ReferenceEquals(this, item))
|
||||
return true;
|
||||
|
||||
if (GetType() != item.GetType())
|
||||
return false;
|
||||
|
||||
if (item.IsTransient() || IsTransient())
|
||||
return false;
|
||||
|
||||
return item.Id == Id;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
if (IsTransient())
|
||||
return base.GetHashCode();
|
||||
|
||||
_requestedHashCode ??= Id.GetHashCode() ^ 31;
|
||||
return _requestedHashCode.Value;
|
||||
}
|
||||
|
||||
public static bool operator ==(Entity? left, Entity? right)
|
||||
{
|
||||
return left?.Equals(right) ?? right is null;
|
||||
}
|
||||
|
||||
public static bool operator !=(Entity? left, Entity? right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
using System.Reflection;
|
||||
|
||||
namespace MiningService.Domain.SeedWork;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Base class for enumeration classes (type-safe enum pattern).
|
||||
/// VI: Lớp cơ sở cho các lớp enumeration (pattern enum an toàn kiểu).
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// EN: This provides a type-safe alternative to enums with additional functionality
|
||||
/// like validation, parsing, and rich behavior.
|
||||
/// VI: Cung cấp một thay thế an toàn kiểu cho enums với các chức năng bổ sung
|
||||
/// như validation, parsing, và hành vi phong phú.
|
||||
/// </remarks>
|
||||
public abstract class Enumeration : IComparable
|
||||
{
|
||||
/// <summary>
|
||||
/// EN: The name of the enumeration value.
|
||||
/// VI: Tên của giá trị enumeration.
|
||||
/// </summary>
|
||||
public string Name { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// EN: The unique identifier of the enumeration value.
|
||||
/// VI: Định danh duy nhất của giá trị enumeration.
|
||||
/// </summary>
|
||||
public int Id { get; private set; }
|
||||
|
||||
protected Enumeration(int id, string name) => (Id, Name) = (id, name);
|
||||
|
||||
public override string ToString() => Name;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Get all enumeration values of a given type.
|
||||
/// VI: Lấy tất cả các giá trị enumeration của một kiểu cho trước.
|
||||
/// </summary>
|
||||
public static IEnumerable<T> GetAll<T>() where T : Enumeration =>
|
||||
typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly)
|
||||
.Select(f => f.GetValue(null))
|
||||
.Cast<T>();
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
if (obj is not Enumeration otherValue)
|
||||
return false;
|
||||
|
||||
var typeMatches = GetType() == obj.GetType();
|
||||
var valueMatches = Id.Equals(otherValue.Id);
|
||||
|
||||
return typeMatches && valueMatches;
|
||||
}
|
||||
|
||||
public override int GetHashCode() => Id.GetHashCode();
|
||||
|
||||
/// <summary>
|
||||
/// EN: Get absolute difference between two enumeration values.
|
||||
/// VI: Lấy sự khác biệt tuyệt đối giữa hai giá trị enumeration.
|
||||
/// </summary>
|
||||
public static int AbsoluteDifference(Enumeration firstValue, Enumeration secondValue)
|
||||
{
|
||||
return Math.Abs(firstValue.Id - secondValue.Id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Parse an integer ID to the corresponding enumeration value.
|
||||
/// VI: Parse một ID integer thành giá trị enumeration tương ứng.
|
||||
/// </summary>
|
||||
public static T FromValue<T>(int value) where T : Enumeration
|
||||
{
|
||||
var matchingItem = Parse<T, int>(value, "value", item => item.Id == value);
|
||||
return matchingItem;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Parse a display name to the corresponding enumeration value.
|
||||
/// VI: Parse một tên hiển thị thành giá trị enumeration tương ứng.
|
||||
/// </summary>
|
||||
public static T FromDisplayName<T>(string displayName) where T : Enumeration
|
||||
{
|
||||
var matchingItem = Parse<T, string>(displayName, "display name", item => item.Name == displayName);
|
||||
return matchingItem;
|
||||
}
|
||||
|
||||
private static T Parse<T, TValue>(TValue value, string description, Func<T, bool> predicate) where T : Enumeration
|
||||
{
|
||||
var matchingItem = GetAll<T>().FirstOrDefault(predicate);
|
||||
|
||||
if (matchingItem is null)
|
||||
throw new InvalidOperationException($"'{value}' is not a valid {description} in {typeof(T)}");
|
||||
|
||||
return matchingItem;
|
||||
}
|
||||
|
||||
public int CompareTo(object? other) => Id.CompareTo(((Enumeration)other!).Id);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
namespace MiningService.Domain.SeedWork;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Marker interface for aggregate roots.
|
||||
/// VI: Interface đánh dấu cho aggregate roots.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// EN: Aggregate roots are the entry points to aggregates and are the only objects
|
||||
/// that outside code should hold references to.
|
||||
/// VI: Aggregate roots là điểm vào của aggregates và là đối tượng duy nhất
|
||||
/// mà code bên ngoài nên giữ tham chiếu đến.
|
||||
/// </remarks>
|
||||
public interface IAggregateRoot
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
namespace MiningService.Domain.SeedWork;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Generic repository interface for aggregate roots.
|
||||
/// VI: Interface repository generic cho aggregate roots.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">EN: The aggregate root type / VI: Kiểu aggregate root</typeparam>
|
||||
public interface IRepository<T> where T : IAggregateRoot
|
||||
{
|
||||
/// <summary>
|
||||
/// EN: The unit of work for this repository.
|
||||
/// VI: Unit of work cho repository này.
|
||||
/// </summary>
|
||||
IUnitOfWork UnitOfWork { get; }
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
namespace MiningService.Domain.SeedWork;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Unit of Work pattern interface.
|
||||
/// VI: Interface cho Unit of Work pattern.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// EN: Maintains a list of objects affected by a business transaction
|
||||
/// and coordinates the writing out of changes.
|
||||
/// VI: Duy trì danh sách các đối tượng bị ảnh hưởng bởi một transaction nghiệp vụ
|
||||
/// và điều phối việc ghi các thay đổi.
|
||||
/// </remarks>
|
||||
public interface IUnitOfWork : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// EN: Save all changes made in this unit of work.
|
||||
/// VI: Lưu tất cả các thay đổi được thực hiện trong unit of work này.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">EN: Cancellation token / VI: Token hủy</param>
|
||||
/// <returns>EN: Number of entities written / VI: Số entity đã ghi</returns>
|
||||
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// EN: Save all changes and dispatch domain events.
|
||||
/// VI: Lưu tất cả thay đổi và dispatch domain events.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">EN: Cancellation token / VI: Token hủy</param>
|
||||
/// <returns>EN: True if successful / VI: True nếu thành công</returns>
|
||||
Task<bool> SaveEntitiesAsync(CancellationToken cancellationToken = default);
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
namespace MiningService.Domain.SeedWork;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Base class for Value Objects following DDD patterns.
|
||||
/// VI: Lớp cơ sở cho Value Objects theo mẫu DDD.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// EN: Value objects are immutable and compared by their values, not identity.
|
||||
/// VI: Value objects là bất biến và được so sánh theo giá trị, không phải định danh.
|
||||
/// </remarks>
|
||||
public abstract class ValueObject
|
||||
{
|
||||
/// <summary>
|
||||
/// EN: Get the atomic values that make up this value object.
|
||||
/// VI: Lấy các giá trị nguyên tử tạo nên value object này.
|
||||
/// </summary>
|
||||
protected abstract IEnumerable<object?> GetEqualityComponents();
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
if (obj is null || obj.GetType() != GetType())
|
||||
return false;
|
||||
|
||||
var other = (ValueObject)obj;
|
||||
return GetEqualityComponents().SequenceEqual(other.GetEqualityComponents());
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return GetEqualityComponents()
|
||||
.Select(x => x?.GetHashCode() ?? 0)
|
||||
.Aggregate((x, y) => x ^ y);
|
||||
}
|
||||
|
||||
public static bool operator ==(ValueObject? left, ValueObject? right)
|
||||
{
|
||||
return left?.Equals(right) ?? right is null;
|
||||
}
|
||||
|
||||
public static bool operator !=(ValueObject? left, ValueObject? right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: Create a copy of this value object with modifications.
|
||||
/// VI: Tạo bản sao của value object này với các thay đổi.
|
||||
/// </summary>
|
||||
protected ValueObject GetCopy()
|
||||
{
|
||||
return (ValueObject)MemberwiseClone();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using MiningService.Domain.AggregatesModel.MinerAggregate;
|
||||
using MiningService.Domain.AggregatesModel.CircleAggregate;
|
||||
using MiningService.Domain.AggregatesModel.ReferralAggregate;
|
||||
using MiningService.Infrastructure.Repositories;
|
||||
|
||||
namespace MiningService.Infrastructure;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Extension methods for infrastructure service registration.
|
||||
/// VI: Extension methods để đăng ký infrastructure services.
|
||||
/// </summary>
|
||||
public static class DependencyInjection
|
||||
{
|
||||
public static IServiceCollection AddInfrastructure(
|
||||
this IServiceCollection services,
|
||||
IConfiguration configuration)
|
||||
{
|
||||
// Database
|
||||
var connectionString = configuration.GetConnectionString("DefaultConnection")
|
||||
?? configuration["DATABASE_URL"]
|
||||
?? throw new InvalidOperationException("Database connection string not found");
|
||||
|
||||
services.AddDbContext<MiningServiceContext>(options =>
|
||||
{
|
||||
options.UseNpgsql(connectionString, npgsqlOptions =>
|
||||
{
|
||||
npgsqlOptions.EnableRetryOnFailure(
|
||||
maxRetryCount: 5,
|
||||
maxRetryDelay: TimeSpan.FromSeconds(30),
|
||||
errorCodesToAdd: null);
|
||||
});
|
||||
});
|
||||
|
||||
// Repositories
|
||||
services.AddScoped<IMinerRepository, MinerRepository>();
|
||||
services.AddScoped<ICircleRepository, CircleRepository>();
|
||||
services.AddScoped<IReferralRepository, ReferralRepository>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using MiningService.Domain.AggregatesModel.CircleAggregate;
|
||||
|
||||
namespace MiningService.Infrastructure.EntityConfigurations;
|
||||
|
||||
/// <summary>
|
||||
/// EN: EF Core configuration for Circle aggregate.
|
||||
/// VI: Cấu hình EF Core cho Circle aggregate.
|
||||
/// </summary>
|
||||
public class CircleEntityTypeConfiguration : IEntityTypeConfiguration<Circle>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Circle> builder)
|
||||
{
|
||||
builder.ToTable("Circles");
|
||||
|
||||
builder.HasKey(c => c.Id);
|
||||
|
||||
builder.Property(c => c.OwnerId)
|
||||
.IsRequired();
|
||||
|
||||
builder.HasIndex(c => c.OwnerId)
|
||||
.HasDatabaseName("IX_Circles_OwnerId");
|
||||
|
||||
builder.Property(c => c.Name)
|
||||
.HasMaxLength(100);
|
||||
|
||||
builder.Property(c => c.TrustScore)
|
||||
.HasPrecision(5, 2);
|
||||
|
||||
builder.Property(c => c.BonusMultiplier)
|
||||
.HasPrecision(5, 4);
|
||||
|
||||
builder.Property(c => c.Status)
|
||||
.HasConversion<string>()
|
||||
.HasMaxLength(20);
|
||||
|
||||
// Navigation to Members
|
||||
builder.HasMany(c => c.Members)
|
||||
.WithOne()
|
||||
.HasForeignKey(m => m.CircleId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
builder.Ignore(c => c.DomainEvents);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: EF Core configuration for CircleMember.
|
||||
/// VI: Cấu hình EF Core cho CircleMember.
|
||||
/// </summary>
|
||||
public class CircleMemberEntityTypeConfiguration : IEntityTypeConfiguration<CircleMember>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<CircleMember> builder)
|
||||
{
|
||||
builder.ToTable("CircleMembers");
|
||||
|
||||
builder.HasKey(m => m.Id);
|
||||
|
||||
builder.HasIndex(m => new { m.CircleId, m.MinerId })
|
||||
.IsUnique()
|
||||
.HasDatabaseName("IX_CircleMembers_CircleId_MinerId");
|
||||
|
||||
builder.HasIndex(m => m.MinerId)
|
||||
.HasDatabaseName("IX_CircleMembers_MinerId");
|
||||
|
||||
builder.Ignore(m => m.DomainEvents);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using MiningService.Domain.AggregatesModel.MinerAggregate;
|
||||
|
||||
namespace MiningService.Infrastructure.EntityConfigurations;
|
||||
|
||||
/// <summary>
|
||||
/// EN: EF Core configuration for Miner aggregate.
|
||||
/// VI: Cấu hình EF Core cho Miner aggregate.
|
||||
/// </summary>
|
||||
public class MinerEntityTypeConfiguration : IEntityTypeConfiguration<Miner>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Miner> builder)
|
||||
{
|
||||
builder.ToTable("Miners");
|
||||
|
||||
builder.HasKey(m => m.Id);
|
||||
|
||||
builder.Property(m => m.UserId)
|
||||
.IsRequired();
|
||||
|
||||
builder.HasIndex(m => m.UserId)
|
||||
.IsUnique()
|
||||
.HasDatabaseName("IX_Miners_UserId");
|
||||
|
||||
builder.Property(m => m.Role)
|
||||
.HasConversion<string>()
|
||||
.HasMaxLength(20);
|
||||
|
||||
builder.Property(m => m.TotalMinedPoints)
|
||||
.HasPrecision(18, 4);
|
||||
|
||||
builder.Property(m => m.ReferralCode)
|
||||
.HasMaxLength(10);
|
||||
|
||||
builder.HasIndex(m => m.ReferralCode)
|
||||
.IsUnique()
|
||||
.HasDatabaseName("IX_Miners_ReferralCode");
|
||||
|
||||
builder.Property(m => m.Status)
|
||||
.HasConversion<string>()
|
||||
.HasMaxLength(20);
|
||||
|
||||
// MiningRate as owned type (value object)
|
||||
builder.OwnsOne(m => m.CurrentRate, rate =>
|
||||
{
|
||||
rate.Property(r => r.BaseRate).HasPrecision(18, 4);
|
||||
rate.Property(r => r.RoleBonus).HasPrecision(5, 4);
|
||||
rate.Property(r => r.CircleBonus).HasPrecision(5, 4);
|
||||
rate.Property(r => r.ReferralBonus).HasPrecision(5, 4);
|
||||
rate.Property(r => r.StreakBonus).HasPrecision(5, 4);
|
||||
});
|
||||
|
||||
// MiningStreak as owned type (value object)
|
||||
builder.OwnsOne(m => m.Streak, streak =>
|
||||
{
|
||||
streak.Property(s => s.CurrentStreak);
|
||||
streak.Property(s => s.LongestStreak);
|
||||
streak.Property(s => s.LastMiningDate);
|
||||
streak.Property(s => s.FreezeTokens);
|
||||
streak.Property(s => s.IsGracePeriod);
|
||||
});
|
||||
|
||||
// ActiveSession as owned type (nullable)
|
||||
builder.OwnsOne(m => m.ActiveSession, session =>
|
||||
{
|
||||
session.Property(s => s.SessionId);
|
||||
session.Property(s => s.StartTime);
|
||||
session.Property(s => s.EndTime);
|
||||
session.Property(s => s.HourlyRate).HasPrecision(18, 4);
|
||||
session.Property(s => s.Status).HasConversion<string>().HasMaxLength(20);
|
||||
session.Property(s => s.AccumulatedPoints).HasPrecision(18, 4);
|
||||
});
|
||||
|
||||
// RowVersion for concurrency
|
||||
builder.Property(m => m.RowVersion)
|
||||
.IsRowVersion();
|
||||
|
||||
// Navigation to MiningHistories
|
||||
builder.HasMany(m => m.MiningHistories)
|
||||
.WithOne()
|
||||
.HasForeignKey(h => h.MinerId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
builder.Ignore(m => m.DomainEvents);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EN: EF Core configuration for MiningHistory.
|
||||
/// VI: Cấu hình EF Core cho MiningHistory.
|
||||
/// </summary>
|
||||
public class MiningHistoryEntityTypeConfiguration : IEntityTypeConfiguration<MiningHistory>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<MiningHistory> builder)
|
||||
{
|
||||
builder.ToTable("MiningHistories");
|
||||
|
||||
builder.HasKey(h => h.Id);
|
||||
|
||||
builder.Property(h => h.PointsEarned)
|
||||
.HasPrecision(18, 4);
|
||||
|
||||
builder.Property(h => h.Source)
|
||||
.HasMaxLength(50);
|
||||
|
||||
builder.Property(h => h.HourlyRateSnapshot)
|
||||
.HasPrecision(18, 4);
|
||||
|
||||
builder.HasIndex(h => new { h.MinerId, h.EarnedAt })
|
||||
.HasDatabaseName("IX_MiningHistories_MinerId_EarnedAt");
|
||||
|
||||
builder.Ignore(h => h.DomainEvents);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using MiningService.Domain.AggregatesModel.ReferralAggregate;
|
||||
|
||||
namespace MiningService.Infrastructure.EntityConfigurations;
|
||||
|
||||
/// <summary>
|
||||
/// EN: EF Core configuration for Referral aggregate.
|
||||
/// VI: Cấu hình EF Core cho Referral aggregate.
|
||||
/// </summary>
|
||||
public class ReferralEntityTypeConfiguration : IEntityTypeConfiguration<Referral>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Referral> builder)
|
||||
{
|
||||
builder.ToTable("Referrals");
|
||||
|
||||
builder.HasKey(r => r.Id);
|
||||
|
||||
builder.HasIndex(r => r.ReferrerId)
|
||||
.HasDatabaseName("IX_Referrals_ReferrerId");
|
||||
|
||||
builder.HasIndex(r => r.ReferredId)
|
||||
.IsUnique()
|
||||
.HasDatabaseName("IX_Referrals_ReferredId");
|
||||
|
||||
builder.Property(r => r.ReferralCode)
|
||||
.HasMaxLength(10);
|
||||
|
||||
builder.Property(r => r.BonusRate)
|
||||
.HasPrecision(5, 4);
|
||||
|
||||
builder.Ignore(r => r.DomainEvents);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
namespace MiningService.Infrastructure.Idempotency;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Entity for tracking client requests to ensure idempotency.
|
||||
/// VI: Entity để theo dõi các requests từ client đảm bảo idempotency.
|
||||
/// </summary>
|
||||
public class ClientRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// EN: Unique request identifier.
|
||||
/// VI: Định danh request duy nhất.
|
||||
/// </summary>
|
||||
public Guid Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// EN: Name of the command/request type.
|
||||
/// VI: Tên của loại command/request.
|
||||
/// </summary>
|
||||
public string Name { get; set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Timestamp when the request was received.
|
||||
/// VI: Thời điểm request được nhận.
|
||||
/// </summary>
|
||||
public DateTime Time { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
namespace MiningService.Infrastructure.Idempotency;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Interface for managing client request idempotency.
|
||||
/// VI: Interface để quản lý idempotency của client requests.
|
||||
/// </summary>
|
||||
public interface IRequestManager
|
||||
{
|
||||
/// <summary>
|
||||
/// EN: Check if a request with the given ID exists.
|
||||
/// VI: Kiểm tra xem request với ID cho trước có tồn tại không.
|
||||
/// </summary>
|
||||
/// <param name="id">EN: Request ID / VI: ID của request</param>
|
||||
/// <returns>EN: True if exists / VI: True nếu tồn tại</returns>
|
||||
Task<bool> ExistAsync(Guid id);
|
||||
|
||||
/// <summary>
|
||||
/// EN: Create a new request record for tracking.
|
||||
/// VI: Tạo bản ghi request mới để theo dõi.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">EN: Command type / VI: Loại command</typeparam>
|
||||
/// <param name="id">EN: Request ID / VI: ID của request</param>
|
||||
Task CreateRequestForCommandAsync<T>(Guid id);
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace MiningService.Infrastructure.Idempotency;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Implementation of request manager for idempotency.
|
||||
/// VI: Triển khai request manager cho idempotency.
|
||||
/// </summary>
|
||||
public class RequestManager : IRequestManager
|
||||
{
|
||||
private readonly MiningServiceContext _context;
|
||||
|
||||
public RequestManager(MiningServiceContext context)
|
||||
{
|
||||
_context = context ?? throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<bool> ExistAsync(Guid id)
|
||||
{
|
||||
var request = await _context
|
||||
.FindAsync<ClientRequest>(id);
|
||||
|
||||
return request != null;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task CreateRequestForCommandAsync<T>(Guid id)
|
||||
{
|
||||
var exists = await ExistAsync(id);
|
||||
|
||||
var request = exists
|
||||
? throw new InvalidOperationException($"Request with {id} already exists")
|
||||
: new ClientRequest
|
||||
{
|
||||
Id = id,
|
||||
Name = typeof(T).Name,
|
||||
Time = DateTime.UtcNow
|
||||
};
|
||||
|
||||
_context.Add(request);
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,379 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using MiningService.Infrastructure;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace MiningService.Infrastructure.Migrations
|
||||
{
|
||||
[DbContext(typeof(MiningServiceContext))]
|
||||
[Migration("20260117103924_InitialCreate")]
|
||||
partial class InitialCreate
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "10.0.2")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("MiningService.Domain.AggregatesModel.CircleAggregate.Circle", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<decimal>("BonusMultiplier")
|
||||
.HasPrecision(5, 4)
|
||||
.HasColumnType("numeric(5,4)");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("character varying(100)");
|
||||
|
||||
b.Property<Guid>("OwnerId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<string>("Status")
|
||||
.IsRequired()
|
||||
.HasMaxLength(20)
|
||||
.HasColumnType("character varying(20)");
|
||||
|
||||
b.Property<decimal>("TrustScore")
|
||||
.HasPrecision(5, 2)
|
||||
.HasColumnType("numeric(5,2)");
|
||||
|
||||
b.Property<DateTime>("UpdatedAt")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("OwnerId")
|
||||
.HasDatabaseName("IX_Circles_OwnerId");
|
||||
|
||||
b.ToTable("Circles", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MiningService.Domain.AggregatesModel.CircleAggregate.CircleMember", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<Guid>("CircleId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<bool>("IsActive")
|
||||
.HasColumnType("boolean");
|
||||
|
||||
b.Property<DateTime>("JoinedAt")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<Guid>("MinerId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("MinerId")
|
||||
.HasDatabaseName("IX_CircleMembers_MinerId");
|
||||
|
||||
b.HasIndex("CircleId", "MinerId")
|
||||
.IsUnique()
|
||||
.HasDatabaseName("IX_CircleMembers_CircleId_MinerId");
|
||||
|
||||
b.ToTable("CircleMembers", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MiningService.Domain.AggregatesModel.MinerAggregate.Miner", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<Guid?>("CircleId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<string>("ReferralCode")
|
||||
.IsRequired()
|
||||
.HasMaxLength(10)
|
||||
.HasColumnType("character varying(10)");
|
||||
|
||||
b.Property<Guid?>("ReferredById")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<string>("Role")
|
||||
.IsRequired()
|
||||
.HasMaxLength(20)
|
||||
.HasColumnType("character varying(20)");
|
||||
|
||||
b.Property<byte[]>("RowVersion")
|
||||
.IsConcurrencyToken()
|
||||
.IsRequired()
|
||||
.ValueGeneratedOnAddOrUpdate()
|
||||
.HasColumnType("bytea");
|
||||
|
||||
b.Property<string>("Status")
|
||||
.IsRequired()
|
||||
.HasMaxLength(20)
|
||||
.HasColumnType("character varying(20)");
|
||||
|
||||
b.Property<decimal>("TotalMinedPoints")
|
||||
.HasPrecision(18, 4)
|
||||
.HasColumnType("numeric(18,4)");
|
||||
|
||||
b.Property<DateTime>("UpdatedAt")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<Guid>("UserId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ReferralCode")
|
||||
.IsUnique()
|
||||
.HasDatabaseName("IX_Miners_ReferralCode");
|
||||
|
||||
b.HasIndex("UserId")
|
||||
.IsUnique()
|
||||
.HasDatabaseName("IX_Miners_UserId");
|
||||
|
||||
b.ToTable("Miners", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MiningService.Domain.AggregatesModel.MinerAggregate.MiningHistory", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<DateTime>("EarnedAt")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<decimal>("HourlyRateSnapshot")
|
||||
.HasPrecision(18, 4)
|
||||
.HasColumnType("numeric(18,4)");
|
||||
|
||||
b.Property<Guid>("MinerId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<decimal>("PointsEarned")
|
||||
.HasPrecision(18, 4)
|
||||
.HasColumnType("numeric(18,4)");
|
||||
|
||||
b.Property<Guid?>("SessionId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<string>("Source")
|
||||
.IsRequired()
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("character varying(50)");
|
||||
|
||||
b.Property<int>("StreakDaySnapshot")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("MinerId", "EarnedAt")
|
||||
.HasDatabaseName("IX_MiningHistories_MinerId_EarnedAt");
|
||||
|
||||
b.ToTable("MiningHistories", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MiningService.Domain.AggregatesModel.ReferralAggregate.Referral", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<DateTime?>("ActivatedAt")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<decimal>("BonusRate")
|
||||
.HasPrecision(5, 4)
|
||||
.HasColumnType("numeric(5,4)");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<bool>("IsActive")
|
||||
.HasColumnType("boolean");
|
||||
|
||||
b.Property<int>("Level")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.Property<string>("ReferralCode")
|
||||
.IsRequired()
|
||||
.HasMaxLength(10)
|
||||
.HasColumnType("character varying(10)");
|
||||
|
||||
b.Property<Guid>("ReferredId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<Guid>("ReferrerId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ReferredId")
|
||||
.IsUnique()
|
||||
.HasDatabaseName("IX_Referrals_ReferredId");
|
||||
|
||||
b.HasIndex("ReferrerId")
|
||||
.HasDatabaseName("IX_Referrals_ReferrerId");
|
||||
|
||||
b.ToTable("Referrals", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MiningService.Domain.AggregatesModel.CircleAggregate.CircleMember", b =>
|
||||
{
|
||||
b.HasOne("MiningService.Domain.AggregatesModel.CircleAggregate.Circle", null)
|
||||
.WithMany("Members")
|
||||
.HasForeignKey("CircleId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MiningService.Domain.AggregatesModel.MinerAggregate.Miner", b =>
|
||||
{
|
||||
b.OwnsOne("MiningService.Domain.AggregatesModel.MinerAggregate.MiningRate", "CurrentRate", b1 =>
|
||||
{
|
||||
b1.Property<Guid>("MinerId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b1.Property<decimal>("BaseRate")
|
||||
.HasPrecision(18, 4)
|
||||
.HasColumnType("numeric(18,4)");
|
||||
|
||||
b1.Property<decimal>("CircleBonus")
|
||||
.HasPrecision(5, 4)
|
||||
.HasColumnType("numeric(5,4)");
|
||||
|
||||
b1.Property<decimal>("ReferralBonus")
|
||||
.HasPrecision(5, 4)
|
||||
.HasColumnType("numeric(5,4)");
|
||||
|
||||
b1.Property<decimal>("RoleBonus")
|
||||
.HasPrecision(5, 4)
|
||||
.HasColumnType("numeric(5,4)");
|
||||
|
||||
b1.Property<decimal>("StreakBonus")
|
||||
.HasPrecision(5, 4)
|
||||
.HasColumnType("numeric(5,4)");
|
||||
|
||||
b1.HasKey("MinerId");
|
||||
|
||||
b1.ToTable("Miners");
|
||||
|
||||
b1.WithOwner()
|
||||
.HasForeignKey("MinerId");
|
||||
});
|
||||
|
||||
b.OwnsOne("MiningService.Domain.AggregatesModel.MinerAggregate.MiningSession", "ActiveSession", b1 =>
|
||||
{
|
||||
b1.Property<Guid>("MinerId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b1.Property<decimal>("AccumulatedPoints")
|
||||
.HasPrecision(18, 4)
|
||||
.HasColumnType("numeric(18,4)");
|
||||
|
||||
b1.Property<DateTime>("EndTime")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b1.Property<decimal>("HourlyRate")
|
||||
.HasPrecision(18, 4)
|
||||
.HasColumnType("numeric(18,4)");
|
||||
|
||||
b1.Property<Guid>("SessionId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b1.Property<DateTime>("StartTime")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b1.Property<string>("Status")
|
||||
.IsRequired()
|
||||
.HasMaxLength(20)
|
||||
.HasColumnType("character varying(20)");
|
||||
|
||||
b1.HasKey("MinerId");
|
||||
|
||||
b1.ToTable("Miners");
|
||||
|
||||
b1.WithOwner()
|
||||
.HasForeignKey("MinerId");
|
||||
});
|
||||
|
||||
b.OwnsOne("MiningService.Domain.AggregatesModel.MinerAggregate.MiningStreak", "Streak", b1 =>
|
||||
{
|
||||
b1.Property<Guid>("MinerId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b1.Property<int>("CurrentStreak")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b1.Property<int>("FreezeTokens")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b1.Property<bool>("IsGracePeriod")
|
||||
.HasColumnType("boolean");
|
||||
|
||||
b1.Property<DateTime>("LastMiningDate")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b1.Property<int>("LongestStreak")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b1.HasKey("MinerId");
|
||||
|
||||
b1.ToTable("Miners");
|
||||
|
||||
b1.WithOwner()
|
||||
.HasForeignKey("MinerId");
|
||||
});
|
||||
|
||||
b.Navigation("ActiveSession");
|
||||
|
||||
b.Navigation("CurrentRate")
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Streak")
|
||||
.IsRequired();
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MiningService.Domain.AggregatesModel.MinerAggregate.MiningHistory", b =>
|
||||
{
|
||||
b.HasOne("MiningService.Domain.AggregatesModel.MinerAggregate.Miner", null)
|
||||
.WithMany("MiningHistories")
|
||||
.HasForeignKey("MinerId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MiningService.Domain.AggregatesModel.CircleAggregate.Circle", b =>
|
||||
{
|
||||
b.Navigation("Members");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MiningService.Domain.AggregatesModel.MinerAggregate.Miner", b =>
|
||||
{
|
||||
b.Navigation("MiningHistories");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace MiningService.Infrastructure.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class InitialCreate : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Circles",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
OwnerId = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
Name = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
|
||||
TrustScore = table.Column<decimal>(type: "numeric(5,2)", precision: 5, scale: 2, nullable: false),
|
||||
BonusMultiplier = table.Column<decimal>(type: "numeric(5,4)", precision: 5, scale: 4, nullable: false),
|
||||
Status = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
|
||||
CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
|
||||
UpdatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Circles", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Miners",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
UserId = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
Role = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
|
||||
TotalMinedPoints = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false),
|
||||
CurrentRate_BaseRate = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false),
|
||||
CurrentRate_RoleBonus = table.Column<decimal>(type: "numeric(5,4)", precision: 5, scale: 4, nullable: false),
|
||||
CurrentRate_CircleBonus = table.Column<decimal>(type: "numeric(5,4)", precision: 5, scale: 4, nullable: false),
|
||||
CurrentRate_ReferralBonus = table.Column<decimal>(type: "numeric(5,4)", precision: 5, scale: 4, nullable: false),
|
||||
CurrentRate_StreakBonus = table.Column<decimal>(type: "numeric(5,4)", precision: 5, scale: 4, nullable: false),
|
||||
ActiveSession_SessionId = table.Column<Guid>(type: "uuid", nullable: true),
|
||||
ActiveSession_StartTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
|
||||
ActiveSession_EndTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
|
||||
ActiveSession_HourlyRate = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: true),
|
||||
ActiveSession_Status = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: true),
|
||||
ActiveSession_AccumulatedPoints = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: true),
|
||||
Streak_CurrentStreak = table.Column<int>(type: "integer", nullable: false),
|
||||
Streak_LongestStreak = table.Column<int>(type: "integer", nullable: false),
|
||||
Streak_LastMiningDate = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
|
||||
Streak_FreezeTokens = table.Column<int>(type: "integer", nullable: false),
|
||||
Streak_IsGracePeriod = table.Column<bool>(type: "boolean", nullable: false),
|
||||
ReferralCode = table.Column<string>(type: "character varying(10)", maxLength: 10, nullable: false),
|
||||
ReferredById = table.Column<Guid>(type: "uuid", nullable: true),
|
||||
CircleId = table.Column<Guid>(type: "uuid", nullable: true),
|
||||
Status = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
|
||||
CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
|
||||
UpdatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
|
||||
RowVersion = table.Column<byte[]>(type: "bytea", rowVersion: true, nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Miners", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Referrals",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
ReferrerId = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
ReferredId = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
ReferralCode = table.Column<string>(type: "character varying(10)", maxLength: 10, nullable: false),
|
||||
BonusRate = table.Column<decimal>(type: "numeric(5,4)", precision: 5, scale: 4, nullable: false),
|
||||
IsActive = table.Column<bool>(type: "boolean", nullable: false),
|
||||
Level = table.Column<int>(type: "integer", nullable: false),
|
||||
CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
|
||||
ActivatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Referrals", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "CircleMembers",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
CircleId = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
MinerId = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
JoinedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
|
||||
IsActive = table.Column<bool>(type: "boolean", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_CircleMembers", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_CircleMembers_Circles_CircleId",
|
||||
column: x => x.CircleId,
|
||||
principalTable: "Circles",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "MiningHistories",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
MinerId = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
PointsEarned = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false),
|
||||
Source = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
|
||||
SessionId = table.Column<Guid>(type: "uuid", nullable: true),
|
||||
EarnedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
|
||||
HourlyRateSnapshot = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false),
|
||||
StreakDaySnapshot = table.Column<int>(type: "integer", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_MiningHistories", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_MiningHistories_Miners_MinerId",
|
||||
column: x => x.MinerId,
|
||||
principalTable: "Miners",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_CircleMembers_CircleId_MinerId",
|
||||
table: "CircleMembers",
|
||||
columns: new[] { "CircleId", "MinerId" },
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_CircleMembers_MinerId",
|
||||
table: "CircleMembers",
|
||||
column: "MinerId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Circles_OwnerId",
|
||||
table: "Circles",
|
||||
column: "OwnerId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Miners_ReferralCode",
|
||||
table: "Miners",
|
||||
column: "ReferralCode",
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Miners_UserId",
|
||||
table: "Miners",
|
||||
column: "UserId",
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_MiningHistories_MinerId_EarnedAt",
|
||||
table: "MiningHistories",
|
||||
columns: new[] { "MinerId", "EarnedAt" });
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Referrals_ReferredId",
|
||||
table: "Referrals",
|
||||
column: "ReferredId",
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Referrals_ReferrerId",
|
||||
table: "Referrals",
|
||||
column: "ReferrerId");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "CircleMembers");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "MiningHistories");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Referrals");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Circles");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Miners");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,376 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using MiningService.Infrastructure;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace MiningService.Infrastructure.Migrations
|
||||
{
|
||||
[DbContext(typeof(MiningServiceContext))]
|
||||
partial class MiningServiceContextModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "10.0.2")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("MiningService.Domain.AggregatesModel.CircleAggregate.Circle", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<decimal>("BonusMultiplier")
|
||||
.HasPrecision(5, 4)
|
||||
.HasColumnType("numeric(5,4)");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("character varying(100)");
|
||||
|
||||
b.Property<Guid>("OwnerId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<string>("Status")
|
||||
.IsRequired()
|
||||
.HasMaxLength(20)
|
||||
.HasColumnType("character varying(20)");
|
||||
|
||||
b.Property<decimal>("TrustScore")
|
||||
.HasPrecision(5, 2)
|
||||
.HasColumnType("numeric(5,2)");
|
||||
|
||||
b.Property<DateTime>("UpdatedAt")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("OwnerId")
|
||||
.HasDatabaseName("IX_Circles_OwnerId");
|
||||
|
||||
b.ToTable("Circles", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MiningService.Domain.AggregatesModel.CircleAggregate.CircleMember", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<Guid>("CircleId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<bool>("IsActive")
|
||||
.HasColumnType("boolean");
|
||||
|
||||
b.Property<DateTime>("JoinedAt")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<Guid>("MinerId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("MinerId")
|
||||
.HasDatabaseName("IX_CircleMembers_MinerId");
|
||||
|
||||
b.HasIndex("CircleId", "MinerId")
|
||||
.IsUnique()
|
||||
.HasDatabaseName("IX_CircleMembers_CircleId_MinerId");
|
||||
|
||||
b.ToTable("CircleMembers", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MiningService.Domain.AggregatesModel.MinerAggregate.Miner", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<Guid?>("CircleId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<string>("ReferralCode")
|
||||
.IsRequired()
|
||||
.HasMaxLength(10)
|
||||
.HasColumnType("character varying(10)");
|
||||
|
||||
b.Property<Guid?>("ReferredById")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<string>("Role")
|
||||
.IsRequired()
|
||||
.HasMaxLength(20)
|
||||
.HasColumnType("character varying(20)");
|
||||
|
||||
b.Property<byte[]>("RowVersion")
|
||||
.IsConcurrencyToken()
|
||||
.IsRequired()
|
||||
.ValueGeneratedOnAddOrUpdate()
|
||||
.HasColumnType("bytea");
|
||||
|
||||
b.Property<string>("Status")
|
||||
.IsRequired()
|
||||
.HasMaxLength(20)
|
||||
.HasColumnType("character varying(20)");
|
||||
|
||||
b.Property<decimal>("TotalMinedPoints")
|
||||
.HasPrecision(18, 4)
|
||||
.HasColumnType("numeric(18,4)");
|
||||
|
||||
b.Property<DateTime>("UpdatedAt")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<Guid>("UserId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ReferralCode")
|
||||
.IsUnique()
|
||||
.HasDatabaseName("IX_Miners_ReferralCode");
|
||||
|
||||
b.HasIndex("UserId")
|
||||
.IsUnique()
|
||||
.HasDatabaseName("IX_Miners_UserId");
|
||||
|
||||
b.ToTable("Miners", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MiningService.Domain.AggregatesModel.MinerAggregate.MiningHistory", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<DateTime>("EarnedAt")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<decimal>("HourlyRateSnapshot")
|
||||
.HasPrecision(18, 4)
|
||||
.HasColumnType("numeric(18,4)");
|
||||
|
||||
b.Property<Guid>("MinerId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<decimal>("PointsEarned")
|
||||
.HasPrecision(18, 4)
|
||||
.HasColumnType("numeric(18,4)");
|
||||
|
||||
b.Property<Guid?>("SessionId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<string>("Source")
|
||||
.IsRequired()
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("character varying(50)");
|
||||
|
||||
b.Property<int>("StreakDaySnapshot")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("MinerId", "EarnedAt")
|
||||
.HasDatabaseName("IX_MiningHistories_MinerId_EarnedAt");
|
||||
|
||||
b.ToTable("MiningHistories", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MiningService.Domain.AggregatesModel.ReferralAggregate.Referral", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<DateTime?>("ActivatedAt")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<decimal>("BonusRate")
|
||||
.HasPrecision(5, 4)
|
||||
.HasColumnType("numeric(5,4)");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<bool>("IsActive")
|
||||
.HasColumnType("boolean");
|
||||
|
||||
b.Property<int>("Level")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.Property<string>("ReferralCode")
|
||||
.IsRequired()
|
||||
.HasMaxLength(10)
|
||||
.HasColumnType("character varying(10)");
|
||||
|
||||
b.Property<Guid>("ReferredId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<Guid>("ReferrerId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ReferredId")
|
||||
.IsUnique()
|
||||
.HasDatabaseName("IX_Referrals_ReferredId");
|
||||
|
||||
b.HasIndex("ReferrerId")
|
||||
.HasDatabaseName("IX_Referrals_ReferrerId");
|
||||
|
||||
b.ToTable("Referrals", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MiningService.Domain.AggregatesModel.CircleAggregate.CircleMember", b =>
|
||||
{
|
||||
b.HasOne("MiningService.Domain.AggregatesModel.CircleAggregate.Circle", null)
|
||||
.WithMany("Members")
|
||||
.HasForeignKey("CircleId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MiningService.Domain.AggregatesModel.MinerAggregate.Miner", b =>
|
||||
{
|
||||
b.OwnsOne("MiningService.Domain.AggregatesModel.MinerAggregate.MiningRate", "CurrentRate", b1 =>
|
||||
{
|
||||
b1.Property<Guid>("MinerId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b1.Property<decimal>("BaseRate")
|
||||
.HasPrecision(18, 4)
|
||||
.HasColumnType("numeric(18,4)");
|
||||
|
||||
b1.Property<decimal>("CircleBonus")
|
||||
.HasPrecision(5, 4)
|
||||
.HasColumnType("numeric(5,4)");
|
||||
|
||||
b1.Property<decimal>("ReferralBonus")
|
||||
.HasPrecision(5, 4)
|
||||
.HasColumnType("numeric(5,4)");
|
||||
|
||||
b1.Property<decimal>("RoleBonus")
|
||||
.HasPrecision(5, 4)
|
||||
.HasColumnType("numeric(5,4)");
|
||||
|
||||
b1.Property<decimal>("StreakBonus")
|
||||
.HasPrecision(5, 4)
|
||||
.HasColumnType("numeric(5,4)");
|
||||
|
||||
b1.HasKey("MinerId");
|
||||
|
||||
b1.ToTable("Miners");
|
||||
|
||||
b1.WithOwner()
|
||||
.HasForeignKey("MinerId");
|
||||
});
|
||||
|
||||
b.OwnsOne("MiningService.Domain.AggregatesModel.MinerAggregate.MiningSession", "ActiveSession", b1 =>
|
||||
{
|
||||
b1.Property<Guid>("MinerId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b1.Property<decimal>("AccumulatedPoints")
|
||||
.HasPrecision(18, 4)
|
||||
.HasColumnType("numeric(18,4)");
|
||||
|
||||
b1.Property<DateTime>("EndTime")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b1.Property<decimal>("HourlyRate")
|
||||
.HasPrecision(18, 4)
|
||||
.HasColumnType("numeric(18,4)");
|
||||
|
||||
b1.Property<Guid>("SessionId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b1.Property<DateTime>("StartTime")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b1.Property<string>("Status")
|
||||
.IsRequired()
|
||||
.HasMaxLength(20)
|
||||
.HasColumnType("character varying(20)");
|
||||
|
||||
b1.HasKey("MinerId");
|
||||
|
||||
b1.ToTable("Miners");
|
||||
|
||||
b1.WithOwner()
|
||||
.HasForeignKey("MinerId");
|
||||
});
|
||||
|
||||
b.OwnsOne("MiningService.Domain.AggregatesModel.MinerAggregate.MiningStreak", "Streak", b1 =>
|
||||
{
|
||||
b1.Property<Guid>("MinerId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b1.Property<int>("CurrentStreak")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b1.Property<int>("FreezeTokens")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b1.Property<bool>("IsGracePeriod")
|
||||
.HasColumnType("boolean");
|
||||
|
||||
b1.Property<DateTime>("LastMiningDate")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b1.Property<int>("LongestStreak")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b1.HasKey("MinerId");
|
||||
|
||||
b1.ToTable("Miners");
|
||||
|
||||
b1.WithOwner()
|
||||
.HasForeignKey("MinerId");
|
||||
});
|
||||
|
||||
b.Navigation("ActiveSession");
|
||||
|
||||
b.Navigation("CurrentRate")
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Streak")
|
||||
.IsRequired();
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MiningService.Domain.AggregatesModel.MinerAggregate.MiningHistory", b =>
|
||||
{
|
||||
b.HasOne("MiningService.Domain.AggregatesModel.MinerAggregate.Miner", null)
|
||||
.WithMany("MiningHistories")
|
||||
.HasForeignKey("MinerId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MiningService.Domain.AggregatesModel.CircleAggregate.Circle", b =>
|
||||
{
|
||||
b.Navigation("Members");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MiningService.Domain.AggregatesModel.MinerAggregate.Miner", b =>
|
||||
{
|
||||
b.Navigation("MiningHistories");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<AssemblyName>MiningService.Infrastructure</AssemblyName>
|
||||
<RootNamespace>MiningService.Infrastructure</RootNamespace>
|
||||
<Description>Infrastructure layer for data access and external services</Description>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<!-- EN: Entity Framework Core with PostgreSQL / VI: Entity Framework Core với PostgreSQL -->
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.0" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="10.0.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
|
||||
<!-- EN: MediatR for dispatching domain events / VI: MediatR để dispatch domain events -->
|
||||
<PackageReference Include="MediatR" Version="12.4.1" />
|
||||
|
||||
<!-- EN: Dapper for read-optimized queries / VI: Dapper cho queries tối ưu đọc -->
|
||||
<PackageReference Include="Dapper" Version="2.1.35" />
|
||||
|
||||
<!-- EN: Resilience with Polly / VI: Resilience với Polly -->
|
||||
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="9.0.0" />
|
||||
<PackageReference Include="Polly" Version="8.5.0" />
|
||||
|
||||
<!-- EN: Redis cache / VI: Redis cache -->
|
||||
<PackageReference Include="StackExchange.Redis" Version="2.8.16" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MiningService.Domain\MiningService.Domain.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,90 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
using MiningService.Domain.AggregatesModel.MinerAggregate;
|
||||
using MiningService.Domain.AggregatesModel.CircleAggregate;
|
||||
using MiningService.Domain.AggregatesModel.ReferralAggregate;
|
||||
using MiningService.Domain.SeedWork;
|
||||
|
||||
namespace MiningService.Infrastructure;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Database context for Mining Service.
|
||||
/// VI: Database context cho Mining Service.
|
||||
/// </summary>
|
||||
public class MiningServiceContext : DbContext, IUnitOfWork
|
||||
{
|
||||
private IDbContextTransaction? _currentTransaction;
|
||||
|
||||
public DbSet<Miner> Miners { get; set; } = null!;
|
||||
public DbSet<MiningHistory> MiningHistories { get; set; } = null!;
|
||||
public DbSet<Circle> Circles { get; set; } = null!;
|
||||
public DbSet<CircleMember> CircleMembers { get; set; } = null!;
|
||||
public DbSet<Referral> Referrals { get; set; } = null!;
|
||||
|
||||
public bool HasActiveTransaction => _currentTransaction != null;
|
||||
|
||||
public MiningServiceContext(DbContextOptions<MiningServiceContext> options)
|
||||
: base(options)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.ApplyConfigurationsFromAssembly(typeof(MiningServiceContext).Assembly);
|
||||
}
|
||||
|
||||
public async Task<bool> SaveEntitiesAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
await SaveChangesAsync(cancellationToken);
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<IDbContextTransaction?> BeginTransactionAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (_currentTransaction != null) return null;
|
||||
_currentTransaction = await Database.BeginTransactionAsync(cancellationToken);
|
||||
return _currentTransaction;
|
||||
}
|
||||
|
||||
public async Task CommitTransactionAsync(IDbContextTransaction transaction)
|
||||
{
|
||||
if (transaction != _currentTransaction)
|
||||
throw new InvalidOperationException($"Transaction {transaction.TransactionId} is not current");
|
||||
|
||||
try
|
||||
{
|
||||
await SaveChangesAsync();
|
||||
await transaction.CommitAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
RollbackTransaction();
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (_currentTransaction != null)
|
||||
{
|
||||
await _currentTransaction.DisposeAsync();
|
||||
_currentTransaction = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void RollbackTransaction()
|
||||
{
|
||||
try
|
||||
{
|
||||
_currentTransaction?.Rollback();
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (_currentTransaction != null)
|
||||
{
|
||||
_currentTransaction.Dispose();
|
||||
_currentTransaction = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MiningService.Domain.AggregatesModel.CircleAggregate;
|
||||
using MiningService.Domain.SeedWork;
|
||||
|
||||
namespace MiningService.Infrastructure.Repositories;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Repository implementation for Circle aggregate.
|
||||
/// VI: Triển khai repository cho Circle aggregate.
|
||||
/// </summary>
|
||||
public class CircleRepository : ICircleRepository
|
||||
{
|
||||
private readonly MiningServiceContext _context;
|
||||
|
||||
public IUnitOfWork UnitOfWork => _context;
|
||||
|
||||
public CircleRepository(MiningServiceContext context)
|
||||
{
|
||||
_context = context ?? throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
public async Task<Circle?> GetByIdAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _context.Circles
|
||||
.Include(c => c.Members)
|
||||
.FirstOrDefaultAsync(c => c.Id == id, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<Circle?> GetByOwnerIdAsync(Guid ownerId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _context.Circles
|
||||
.Include(c => c.Members)
|
||||
.FirstOrDefaultAsync(c => c.OwnerId == ownerId, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<Circle?> GetByMemberIdAsync(Guid minerId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _context.Circles
|
||||
.Include(c => c.Members)
|
||||
.FirstOrDefaultAsync(c => c.Members.Any(m => m.MinerId == minerId && m.IsActive), cancellationToken);
|
||||
}
|
||||
|
||||
public Circle Add(Circle circle)
|
||||
{
|
||||
return _context.Circles.Add(circle).Entity;
|
||||
}
|
||||
|
||||
public void Update(Circle circle)
|
||||
{
|
||||
_context.Entry(circle).State = EntityState.Modified;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MiningService.Domain.AggregatesModel.MinerAggregate;
|
||||
using MiningService.Domain.SeedWork;
|
||||
|
||||
namespace MiningService.Infrastructure.Repositories;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Repository implementation for Miner aggregate.
|
||||
/// VI: Triển khai repository cho Miner aggregate.
|
||||
/// </summary>
|
||||
public class MinerRepository : IMinerRepository
|
||||
{
|
||||
private readonly MiningServiceContext _context;
|
||||
|
||||
public IUnitOfWork UnitOfWork => _context;
|
||||
|
||||
public MinerRepository(MiningServiceContext context)
|
||||
{
|
||||
_context = context ?? throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
public async Task<Miner?> GetByIdAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _context.Miners
|
||||
.Include(m => m.MiningHistories.OrderByDescending(h => h.EarnedAt).Take(50))
|
||||
.FirstOrDefaultAsync(m => m.Id == id, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<Miner?> GetByUserIdAsync(Guid userId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _context.Miners
|
||||
.FirstOrDefaultAsync(m => m.UserId == userId, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<Miner?> GetByReferralCodeAsync(string referralCode, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _context.Miners
|
||||
.FirstOrDefaultAsync(m => m.ReferralCode == referralCode, cancellationToken);
|
||||
}
|
||||
|
||||
public Miner Add(Miner miner)
|
||||
{
|
||||
return _context.Miners.Add(miner).Entity;
|
||||
}
|
||||
|
||||
public void Update(Miner miner)
|
||||
{
|
||||
_context.Entry(miner).State = EntityState.Modified;
|
||||
}
|
||||
|
||||
public async Task<bool> ReferralCodeExistsAsync(string referralCode, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _context.Miners
|
||||
.AnyAsync(m => m.ReferralCode == referralCode, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<int> GetActiveReferralsCountAsync(Guid minerId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _context.Referrals
|
||||
.CountAsync(r => r.ReferrerId == minerId && r.IsActive, cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MiningService.Domain.AggregatesModel.ReferralAggregate;
|
||||
using MiningService.Domain.SeedWork;
|
||||
|
||||
namespace MiningService.Infrastructure.Repositories;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Repository implementation for Referral aggregate.
|
||||
/// VI: Triển khai repository cho Referral aggregate.
|
||||
/// </summary>
|
||||
public class ReferralRepository : IReferralRepository
|
||||
{
|
||||
private readonly MiningServiceContext _context;
|
||||
|
||||
public IUnitOfWork UnitOfWork => _context;
|
||||
|
||||
public ReferralRepository(MiningServiceContext context)
|
||||
{
|
||||
_context = context ?? throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
public async Task<Referral?> GetByIdAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _context.Referrals
|
||||
.FirstOrDefaultAsync(r => r.Id == id, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<List<Referral>> GetByReferrerIdAsync(Guid referrerId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _context.Referrals
|
||||
.Where(r => r.ReferrerId == referrerId)
|
||||
.OrderByDescending(r => r.CreatedAt)
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<int> GetActiveCountByReferrerIdAsync(Guid referrerId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _context.Referrals
|
||||
.CountAsync(r => r.ReferrerId == referrerId && r.IsActive, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<Referral?> GetByReferredIdAsync(Guid referredId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _context.Referrals
|
||||
.FirstOrDefaultAsync(r => r.ReferredId == referredId, cancellationToken);
|
||||
}
|
||||
|
||||
public Referral Add(Referral referral)
|
||||
{
|
||||
return _context.Referrals.Add(referral).Entity;
|
||||
}
|
||||
|
||||
public void Update(Referral referral)
|
||||
{
|
||||
_context.Entry(referral).State = EntityState.Modified;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using MiningService.Infrastructure;
|
||||
|
||||
namespace MiningService.FunctionalTests;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Custom WebApplicationFactory for functional tests.
|
||||
/// VI: WebApplicationFactory tùy chỉnh cho functional tests.
|
||||
/// </summary>
|
||||
public class CustomWebApplicationFactory : WebApplicationFactory<Program>
|
||||
{
|
||||
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
||||
{
|
||||
builder.UseEnvironment("Testing");
|
||||
|
||||
builder.ConfigureServices(services =>
|
||||
{
|
||||
// EN: Remove the existing DbContext registration
|
||||
// VI: Xóa đăng ký DbContext hiện tại
|
||||
var descriptor = services.SingleOrDefault(
|
||||
d => d.ServiceType == typeof(DbContextOptions<MiningServiceContext>));
|
||||
|
||||
if (descriptor != null)
|
||||
{
|
||||
services.Remove(descriptor);
|
||||
}
|
||||
|
||||
// EN: Remove DbContext service
|
||||
// VI: Xóa DbContext service
|
||||
var dbContextDescriptor = services.SingleOrDefault(
|
||||
d => d.ServiceType == typeof(MiningServiceContext));
|
||||
|
||||
if (dbContextDescriptor != null)
|
||||
{
|
||||
services.Remove(dbContextDescriptor);
|
||||
}
|
||||
|
||||
// EN: Add in-memory database for testing
|
||||
// VI: Thêm in-memory database để test
|
||||
services.AddDbContext<MiningServiceContext>(options =>
|
||||
{
|
||||
options.UseInMemoryDatabase("TestDatabase_" + Guid.NewGuid().ToString());
|
||||
});
|
||||
|
||||
// EN: Ensure database is created with seed data
|
||||
// VI: Đảm bảo database được tạo với seed data
|
||||
var sp = services.BuildServiceProvider();
|
||||
using var scope = sp.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<MiningServiceContext>();
|
||||
db.Database.EnsureCreated();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<AssemblyName>MiningService.FunctionalTests</AssemblyName>
|
||||
<RootNamespace>MiningService.FunctionalTests</RootNamespace>
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<!-- EN: Test framework / VI: Test framework -->
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
|
||||
<!-- EN: Integration testing / VI: Integration testing -->
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="10.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="10.0.0" />
|
||||
<PackageReference Include="FluentAssertions" Version="6.12.2" />
|
||||
|
||||
<!-- EN: Test containers for database / VI: Test containers cho database -->
|
||||
<PackageReference Include="Testcontainers.PostgreSql" Version="4.1.0" />
|
||||
|
||||
<!-- EN: Coverage / VI: Coverage -->
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.2">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\MiningService.API\MiningService.API.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,126 @@
|
||||
using MiningService.Domain.AggregatesModel.CircleAggregate;
|
||||
using MiningService.Domain.Exceptions;
|
||||
using Xunit;
|
||||
|
||||
namespace MiningService.UnitTests.Domain;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Unit tests for Circle aggregate.
|
||||
/// VI: Unit tests cho Circle aggregate.
|
||||
/// </summary>
|
||||
public class CircleAggregateTests
|
||||
{
|
||||
[Fact]
|
||||
public void Create_WithValidData_ShouldCreateCircle()
|
||||
{
|
||||
// Arrange
|
||||
var ownerId = Guid.NewGuid();
|
||||
var name = "Test Circle";
|
||||
|
||||
// Act
|
||||
var circle = Circle.Create(ownerId, name);
|
||||
|
||||
// Assert
|
||||
Assert.NotEqual(Guid.Empty, circle.Id);
|
||||
Assert.Equal(ownerId, circle.OwnerId);
|
||||
Assert.Equal(name, circle.Name);
|
||||
Assert.Equal(CircleStatus.Incomplete, circle.Status);
|
||||
Assert.Equal(1, circle.ActiveMemberCount); // Owner is first member
|
||||
Assert.False(circle.IsValid);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Create_WithEmptyName_ShouldThrow()
|
||||
{
|
||||
// Act & Assert
|
||||
Assert.Throws<CircleDomainException>(() => Circle.Create(Guid.NewGuid(), ""));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddMember_UntilValid_ShouldActivateCircle()
|
||||
{
|
||||
// Arrange
|
||||
var circle = Circle.Create(Guid.NewGuid(), "Test");
|
||||
|
||||
// Act
|
||||
circle.AddMember(Guid.NewGuid());
|
||||
circle.AddMember(Guid.NewGuid());
|
||||
|
||||
// Assert - 3 members (owner + 2) should be valid
|
||||
Assert.Equal(3, circle.ActiveMemberCount);
|
||||
Assert.Equal(CircleStatus.Active, circle.Status);
|
||||
Assert.True(circle.IsValid);
|
||||
Assert.Equal(0.25m, circle.BonusMultiplier);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddMember_BeyondMax_ShouldThrow()
|
||||
{
|
||||
// Arrange
|
||||
var circle = Circle.Create(Guid.NewGuid(), "Test");
|
||||
circle.AddMember(Guid.NewGuid());
|
||||
circle.AddMember(Guid.NewGuid());
|
||||
circle.AddMember(Guid.NewGuid());
|
||||
circle.AddMember(Guid.NewGuid()); // 5 total (max)
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<CircleDomainException>(() => circle.AddMember(Guid.NewGuid()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddMember_Duplicate_ShouldThrow()
|
||||
{
|
||||
// Arrange
|
||||
var circle = Circle.Create(Guid.NewGuid(), "Test");
|
||||
var memberId = Guid.NewGuid();
|
||||
circle.AddMember(memberId);
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<CircleDomainException>(() => circle.AddMember(memberId));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveMember_ShouldDeactivateMember()
|
||||
{
|
||||
// Arrange
|
||||
var circle = Circle.Create(Guid.NewGuid(), "Test");
|
||||
var memberId = Guid.NewGuid();
|
||||
circle.AddMember(memberId);
|
||||
circle.AddMember(Guid.NewGuid()); // 3 members, now valid
|
||||
|
||||
// Act
|
||||
circle.RemoveMember(memberId);
|
||||
|
||||
// Assert - 2 active members, no longer valid
|
||||
Assert.Equal(2, circle.ActiveMemberCount);
|
||||
Assert.Equal(CircleStatus.Incomplete, circle.Status);
|
||||
Assert.False(circle.IsValid);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveMember_Owner_ShouldThrow()
|
||||
{
|
||||
// Arrange
|
||||
var ownerId = Guid.NewGuid();
|
||||
var circle = Circle.Create(ownerId, "Test");
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<CircleDomainException>(() => circle.RemoveMember(ownerId));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Disband_ShouldDeactivateAll()
|
||||
{
|
||||
// Arrange
|
||||
var circle = Circle.Create(Guid.NewGuid(), "Test");
|
||||
circle.AddMember(Guid.NewGuid());
|
||||
circle.AddMember(Guid.NewGuid());
|
||||
|
||||
// Act
|
||||
circle.Disband();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(CircleStatus.Disbanded, circle.Status);
|
||||
Assert.Equal(0, circle.BonusMultiplier);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
using MiningService.Domain.AggregatesModel.MinerAggregate;
|
||||
using MiningService.Domain.Exceptions;
|
||||
using Xunit;
|
||||
|
||||
namespace MiningService.UnitTests.Domain;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Unit tests for Miner aggregate.
|
||||
/// VI: Unit tests cho Miner aggregate.
|
||||
/// </summary>
|
||||
public class MinerAggregateTests
|
||||
{
|
||||
[Fact]
|
||||
public void Create_WithValidUserId_ShouldCreateMiner()
|
||||
{
|
||||
// Arrange
|
||||
var userId = Guid.NewGuid();
|
||||
|
||||
// Act
|
||||
var miner = Miner.Create(userId);
|
||||
|
||||
// Assert
|
||||
Assert.NotEqual(Guid.Empty, miner.Id);
|
||||
Assert.Equal(userId, miner.UserId);
|
||||
Assert.Equal(MinerRole.Pioneer, miner.Role);
|
||||
Assert.Equal(0, miner.TotalMinedPoints);
|
||||
Assert.Equal(MinerStatus.Active, miner.Status);
|
||||
Assert.NotEmpty(miner.ReferralCode);
|
||||
Assert.Single(miner.DomainEvents); // MinerCreatedDomainEvent
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StartMiningSession_WhenActive_ShouldCreateSession()
|
||||
{
|
||||
// Arrange
|
||||
var miner = Miner.Create(Guid.NewGuid());
|
||||
miner.ClearDomainEvents();
|
||||
|
||||
// Act
|
||||
var session = miner.StartMiningSession();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(session);
|
||||
Assert.Equal(MiningSessionStatus.Active, session.Status);
|
||||
Assert.True(session.HourlyRate > 0);
|
||||
Assert.Single(miner.DomainEvents); // MiningSessionStartedDomainEvent
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StartMiningSession_WhenSuspended_ShouldThrow()
|
||||
{
|
||||
// Arrange
|
||||
var miner = Miner.Create(Guid.NewGuid());
|
||||
miner.Suspend();
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<MiningDomainException>(() => miner.StartMiningSession());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StartMiningSession_WhenAlreadyActive_ShouldThrow()
|
||||
{
|
||||
// Arrange
|
||||
var miner = Miner.Create(Guid.NewGuid());
|
||||
miner.StartMiningSession();
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<MiningDomainException>(() => miner.StartMiningSession());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Suspend_WhenActive_ShouldChangeStatus()
|
||||
{
|
||||
// Arrange
|
||||
var miner = Miner.Create(Guid.NewGuid());
|
||||
|
||||
// Act
|
||||
miner.Suspend();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(MinerStatus.Suspended, miner.Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Restore_WhenSuspended_ShouldActivate()
|
||||
{
|
||||
// Arrange
|
||||
var miner = Miner.Create(Guid.NewGuid());
|
||||
miner.Suspend();
|
||||
|
||||
// Act
|
||||
miner.Restore();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(MinerStatus.Active, miner.Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddBonusPoints_WithPositiveAmount_ShouldAddPoints()
|
||||
{
|
||||
// Arrange
|
||||
var miner = Miner.Create(Guid.NewGuid());
|
||||
var initialPoints = miner.TotalMinedPoints;
|
||||
|
||||
// Act
|
||||
miner.AddBonusPoints(100, "Test Bonus");
|
||||
|
||||
// Assert
|
||||
Assert.Equal(initialPoints + 100, miner.TotalMinedPoints);
|
||||
Assert.Single(miner.MiningHistories);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddBonusPoints_WithNegativeAmount_ShouldThrow()
|
||||
{
|
||||
// Arrange
|
||||
var miner = Miner.Create(Guid.NewGuid());
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<MiningDomainException>(() => miner.AddBonusPoints(-10, "Invalid"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JoinCircle_WhenNotInCircle_ShouldSetCircleId()
|
||||
{
|
||||
// Arrange
|
||||
var miner = Miner.Create(Guid.NewGuid());
|
||||
var circleId = Guid.NewGuid();
|
||||
|
||||
// Act
|
||||
miner.JoinCircle(circleId);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(circleId, miner.CircleId);
|
||||
Assert.Equal(MinerRole.Contributor, miner.Role); // Upgraded from Pioneer
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JoinCircle_WhenAlreadyInCircle_ShouldThrow()
|
||||
{
|
||||
// Arrange
|
||||
var miner = Miner.Create(Guid.NewGuid());
|
||||
miner.JoinCircle(Guid.NewGuid());
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<MiningDomainException>(() => miner.JoinCircle(Guid.NewGuid()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
using MiningService.Domain.AggregatesModel.MinerAggregate;
|
||||
using Xunit;
|
||||
|
||||
namespace MiningService.UnitTests.Domain;
|
||||
|
||||
/// <summary>
|
||||
/// EN: Unit tests for MiningStreak value object.
|
||||
/// VI: Unit tests cho MiningStreak value object.
|
||||
/// </summary>
|
||||
public class MiningStreakTests
|
||||
{
|
||||
[Fact]
|
||||
public void CreateNew_ShouldReturnZeroStreak()
|
||||
{
|
||||
// Act
|
||||
var streak = MiningStreak.CreateNew();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(0, streak.CurrentStreak);
|
||||
Assert.Equal(0, streak.LongestStreak);
|
||||
Assert.Equal(0, streak.FreezeTokens);
|
||||
Assert.False(streak.IsGracePeriod);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0, 0)] // Day 0: 0%
|
||||
[InlineData(1, 0)] // Day 1: 0%
|
||||
[InlineData(2, 0)] // Day 2: 0%
|
||||
[InlineData(3, 0.10)] // Day 3: +10%
|
||||
[InlineData(6, 0.10)] // Day 6: +10%
|
||||
[InlineData(7, 0.25)] // Day 7: +25%
|
||||
[InlineData(13, 0.25)] // Day 13: +25%
|
||||
[InlineData(14, 0.50)] // Day 14: +50%
|
||||
[InlineData(29, 0.50)] // Day 29: +50%
|
||||
[InlineData(30, 1.00)] // Day 30: +100%
|
||||
[InlineData(59, 1.00)] // Day 59: +100%
|
||||
[InlineData(60, 1.25)] // Day 60: +125%
|
||||
[InlineData(89, 1.25)] // Day 89: +125%
|
||||
[InlineData(90, 1.50)] // Day 90: +150%
|
||||
[InlineData(100, 1.50)] // Day 100: +150%
|
||||
public void BonusMultiplier_ShouldMatchTier(int currentStreak, decimal expectedBonus)
|
||||
{
|
||||
// Arrange
|
||||
var streak = new MiningStreak
|
||||
{
|
||||
CurrentStreak = currentStreak,
|
||||
LongestStreak = currentStreak,
|
||||
LastMiningDate = DateTime.UtcNow,
|
||||
FreezeTokens = 0,
|
||||
IsGracePeriod = false
|
||||
};
|
||||
|
||||
// Assert
|
||||
Assert.Equal(expectedBonus, streak.BonusMultiplier);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IncrementStreak_ShouldIncreaseBoth()
|
||||
{
|
||||
// Arrange
|
||||
var streak = MiningStreak.CreateNew();
|
||||
|
||||
// Act
|
||||
var newStreak = streak.IncrementStreak();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1, newStreak.CurrentStreak);
|
||||
Assert.Equal(1, newStreak.LongestStreak);
|
||||
Assert.False(newStreak.IsGracePeriod);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IncrementStreak_AtDay7_ShouldEarnFreezeToken()
|
||||
{
|
||||
// Arrange
|
||||
var streak = new MiningStreak
|
||||
{
|
||||
CurrentStreak = 6,
|
||||
LongestStreak = 6,
|
||||
LastMiningDate = DateTime.UtcNow.AddDays(-1),
|
||||
FreezeTokens = 0,
|
||||
IsGracePeriod = false
|
||||
};
|
||||
|
||||
// Act
|
||||
var newStreak = streak.IncrementStreak();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(7, newStreak.CurrentStreak);
|
||||
Assert.Equal(1, newStreak.FreezeTokens);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reset_ShouldZeroCurrentStreak()
|
||||
{
|
||||
// Arrange
|
||||
var streak = new MiningStreak
|
||||
{
|
||||
CurrentStreak = 30,
|
||||
LongestStreak = 50,
|
||||
LastMiningDate = DateTime.UtcNow,
|
||||
FreezeTokens = 3,
|
||||
IsGracePeriod = false
|
||||
};
|
||||
|
||||
// Act
|
||||
var resetStreak = streak.Reset();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(0, resetStreak.CurrentStreak);
|
||||
Assert.Equal(50, resetStreak.LongestStreak); // Longest preserved
|
||||
Assert.Equal(3, resetStreak.FreezeTokens); // Tokens preserved
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<AssemblyName>MiningService.UnitTests</AssemblyName>
|
||||
<RootNamespace>MiningService.UnitTests</RootNamespace>
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<!-- EN: Test framework / VI: Test framework -->
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
|
||||
<!-- EN: Assertions and mocking / VI: Assertions và mocking -->
|
||||
<PackageReference Include="FluentAssertions" Version="6.12.2" />
|
||||
<PackageReference Include="Moq" Version="4.20.72" />
|
||||
|
||||
<!-- EN: Coverage / VI: Coverage -->
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.2">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\MiningService.Domain\MiningService.Domain.csproj" />
|
||||
<ProjectReference Include="..\..\src\MiningService.API\MiningService.API.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user