-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor entities and add group creation feature
Refactored several entities to use GUIDs instead of strings for IDs, including updates to the `Certificate`, `Group`, and `Skill` classes. Added new properties and methods to support domain-driven design, such as the `CreateGroupCommandHandler` and `GroupCreated` domain event. Updated `ServiceCollectionExtensions` to register `IUnitOfWork` as a transient service. Introduced a new migration file to handle database schema changes. Added a new API endpoint for group creation in `GroupApi.cs`.
- Loading branch information
1 parent
47ca9ae
commit 813c610
Showing
13 changed files
with
1,028 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
MBS_COMMAND.Application/UserCases/Commands/Groups/CreateGroupCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using MBS_COMMAND.Contract.Abstractions.Messages; | ||
using MBS_COMMAND.Contract.Abstractions.Shared; | ||
using MBS_COMMAND.Contract.Services.Groups; | ||
using MBS_COMMAND.Domain.Abstractions.Repositories; | ||
using MBS_COMMAND.Domain.Entities; | ||
|
||
namespace MBS_COMMAND.Application.UserCases.Commands.Groups; | ||
|
||
public sealed class CreateGroupCommandHandler : ICommandHandler<Command.CreateGroupCommand> | ||
{ | ||
private readonly IRepositoryBase<Group, Guid> _repositoryBase; | ||
|
||
public CreateGroupCommandHandler(IRepositoryBase<Group, Guid> repositoryBase) | ||
{ | ||
_repositoryBase = repositoryBase; | ||
} | ||
|
||
public async Task<Result> Handle(Command.CreateGroupCommand request, CancellationToken cancellationToken) | ||
{ | ||
var G = Group.Create(request.Name, request.Stacks, request.MentorId); | ||
_repositoryBase.Add(G); | ||
return Result.Success(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using MBS_COMMAND.Contract.Abstractions.Messages; | ||
|
||
namespace MBS_COMMAND.Contract.Services.Groups; | ||
|
||
public static class Command | ||
{ | ||
public record CreateGroupCommand(string Name,Guid MentorId,string Stacks) : ICommand; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using MBS_COMMAND.Contract.Abstractions.Messages; | ||
|
||
namespace MBS_COMMAND.Contract.Services.Groups; | ||
|
||
public static class DomainEvents | ||
{ | ||
public record GroupCreated(Guid IdEvent, Guid Id, string Name, Guid? MentorId, string Stacks) : IDomainEvent, ICommand; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,39 @@ | ||
using MBS_AUTHORIZATION.Domain.Entities; | ||
using MBS_COMMAND.Domain.Abstractions.Aggregates; | ||
using MBS_COMMAND.Domain.Abstractions.Entities; | ||
|
||
namespace MBS_COMMAND.Domain.Entities; | ||
|
||
public class Group : Entity<Guid>, IAuditableEntity | ||
public class Group : AggregateRoot<Guid>, IAuditableEntity | ||
{ | ||
public string Name { get; set; } | ||
public Guid? MentorId { get; set; } | ||
public string Name { get; private set; } | ||
public Guid? MentorId { get; private set; } | ||
public virtual User? Mentor { get; set; } | ||
|
||
public string? LeaderId { get; set; } | ||
public Guid? LeaderId { get; set; } | ||
public virtual User? Leader { get; set; } | ||
public string Stack { get; set; } | ||
public Guid? ProjectId { get; set; } | ||
public string Stack { get; private set; } | ||
public Guid? ProjectId { get; private set; } | ||
public virtual Project? Project { get; set; } | ||
public DateTimeOffset CreatedOnUtc { get ; set ; } | ||
public DateTimeOffset? ModifiedOnUtc { get ; set ; } | ||
public DateTimeOffset CreatedOnUtc { get; set; } | ||
public DateTimeOffset? ModifiedOnUtc { get; set; } | ||
|
||
|
||
|
||
public Group(string name, string stack, Guid? mentorId) | ||
{ | ||
Name = name; | ||
Stack = stack; | ||
MentorId = mentorId; | ||
} | ||
|
||
public static Group Create(string name, string stack, Guid? mentorId) | ||
{ | ||
var G = new Group(name, stack, mentorId); | ||
G.RaiseDomainEvent(new Contract.Services.Groups.DomainEvents.GroupCreated(Guid.NewGuid(), G.Id, G.Name, G.MentorId, G.Stack)); | ||
return G; | ||
} | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
| ||
using Microsoft.EntityFrameworkCore.ChangeTracking; | ||
using Microsoft.EntityFrameworkCore; | ||
using Newtonsoft.Json; | ||
using MBS_COMMAND.Domain.Abstractions; | ||
using MBS_COMMAND.Persistence.Outbox; | ||
using MBS_COMMAND.Domain.Abstractions.Entities; | ||
|
||
namespace MBS_COMMAND.Persistence; | ||
|
||
public class EFUnitOfWork : IUnitOfWork | ||
{ | ||
private readonly ApplicationDbContext _dbContext; | ||
|
||
public EFUnitOfWork(ApplicationDbContext dbContext) | ||
=> _dbContext = dbContext; | ||
|
||
public async Task SaveChangesAsync(CancellationToken cancellationToken = default) | ||
{ | ||
//ConvertDomainEventsToOutboxMessages(); | ||
//UpdateAuditableEntities(); | ||
await _dbContext.SaveChangesAsync(); | ||
} | ||
|
||
async ValueTask IAsyncDisposable.DisposeAsync() | ||
=> await _dbContext.DisposeAsync(); | ||
|
||
private void ConvertDomainEventsToOutboxMessages() | ||
{ | ||
var outboxMessages = _dbContext.ChangeTracker | ||
.Entries<Domain.Abstractions.Aggregates.AggregateRoot<Guid>>() | ||
.Select(x => x.Entity) | ||
.SelectMany(aggregateRoot => | ||
{ | ||
var domainEvents = aggregateRoot.GetDomainEvents(); | ||
|
||
aggregateRoot.ClearDomainEvents(); | ||
|
||
return domainEvents; | ||
}) | ||
.Select(domainEvent => new OutboxMessage | ||
{ | ||
Id = Guid.NewGuid(), | ||
OccurredOnUtc = DateTime.UtcNow, | ||
Type = domainEvent.GetType().Name, | ||
Content = JsonConvert.SerializeObject( | ||
domainEvent, | ||
new JsonSerializerSettings | ||
{ | ||
TypeNameHandling = TypeNameHandling.All | ||
}) | ||
}) | ||
.ToList(); | ||
|
||
_dbContext.Set<OutboxMessage>().AddRange(outboxMessages); | ||
} | ||
|
||
private void UpdateAuditableEntities() | ||
{ | ||
IEnumerable<EntityEntry<IAuditableEntity>> entries = | ||
_dbContext | ||
.ChangeTracker | ||
.Entries<IAuditableEntity>(); | ||
|
||
foreach (EntityEntry<IAuditableEntity> entityEntry in entries) | ||
{ | ||
if (entityEntry.State == EntityState.Added) | ||
{ | ||
entityEntry.Property(a => a.CreatedOnUtc).CurrentValue = DateTime.UtcNow; | ||
} | ||
|
||
if (entityEntry.State == EntityState.Modified) | ||
{ | ||
entityEntry.Property(a => a.ModifiedOnUtc).CurrentValue = DateTime.UtcNow; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.