Skip to content

Commit

Permalink
domain objects encapsulated properties
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidEggenberger committed Dec 3, 2024
1 parent de8febd commit 92c320b
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public TenantSubscriptionUpdatedIntegrationEventHandler(IServiceProvider service
public async Task HandleAsync(TenantSubscriptionPlanUpdatedIntegrationEvent integrationEvent, CancellationToken cancellation)
{
var tenant = await module.TenantIdentityDbContext.Tenants.FirstAsync(tenant => tenant.Id == integrationEvent.TenantId);
tenant.SubscriptionPlanType = integrationEvent.SubscriptionPlanType;
tenant.UpdateSubscriptionPlan(integrationEvent.SubscriptionPlanType);

await module.TenantIdentityDbContext.SaveChangesAsync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ namespace Modules.TenantIdentity.Features.DomainFeatures.Tenants.Domain
{
public class Tenant : Entity
{
public Tenant() { }
private Tenant() { }

public string Name { get; set; }
public SubscriptionPlanType SubscriptionPlanType { get; set; }
public string Name { get; private set; }
public SubscriptionPlanType SubscriptionPlanType { get; private set; }
public IReadOnlyCollection<TenantMembership> Memberships => memberships.AsReadOnly();
private List<TenantMembership> memberships = new List<TenantMembership>();
public IReadOnlyCollection<TenantInvitation> Invitations => invitations.AsReadOnly();
Expand Down Expand Up @@ -103,6 +103,11 @@ public bool CheckIfMember(Guid userId)
return memberships.Any(membership => membership.UserId == userId);
}

public void UpdateSubscriptionPlan(SubscriptionPlanType subscriptionPlanType)
{
SubscriptionPlanType = subscriptionPlanType;
}

public void ThrowIfUserCantDeleteTenant()
{
ThrowIfCallerIsNotInRole(TenantRole.Admin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace Modules.TenantIdentity.Features.DomainFeatures.Tenants.Domain
{
public class TenantInvitation : Entity
{
public Guid TenantId { get; set; }
public Tenant Tenant { get; set; }
public string Email { get; set; }
public TenantRole Role { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public TenantMembership(Guid userId, TenantRole role)
}

public Guid UserId { get; set; }
public Guid TenantId { get; set; }
public Tenant Tenant { get; set; }
public TenantRole Role { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class CreateNewUser : Command
public ApplicationUser User { get; set; }
public ExternalLoginInfo LoginInfo { get; set; }
}

public class CreateNewUserCommandHandler : ICommandHandler<CreateNewUser>
{
private readonly UserManager<ApplicationUser> userManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

namespace Modules.TenantIdentity.Features.DomainFeatures.Users.Application.Commands
{
public class SetSelectedTenantForUser : Command
public class SelectTenant : Command
{
public Guid SelectedTenantId { get; set; }
}

public class SetSelectedTenantForUserHandler : ServerExecutionBase<TenantIdentityModule>, ICommandHandler<SetSelectedTenantForUser>
public class SelectedTenantCommandHandler : ServerExecutionBase<TenantIdentityModule>, ICommandHandler<SelectTenant>
{
public SetSelectedTenantForUserHandler(IServiceProvider serviceProvider) : base(serviceProvider) { }
public SelectedTenantCommandHandler(IServiceProvider serviceProvider) : base(serviceProvider) { }

public async Task HandleAsync(SetSelectedTenantForUser command, CancellationToken cancellationToken)
public async Task HandleAsync(SelectTenant command, CancellationToken cancellationToken)
{
var user = await module.TenantIdentityDbContext.GetUserByIdAsync(command.ExecutingUserId);

Expand All @@ -22,7 +22,7 @@ public async Task HandleAsync(SetSelectedTenantForUser command, CancellationToke
return;
}

user.SelectedTenantId = command.SelectedTenantId;
user.SelectTenant(command.SelectedTenantId);

await module.TenantIdentityDbContext.SaveChangesAsync(cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,34 @@ namespace Modules.TenantIdentity.Features.DomainFeatures.Users
{
public class ApplicationUser : IdentityUser<Guid>, IApplicationUser
{
public string PictureUri { get; set; }
private ApplicationUser() { }

public string PictureUri { get; private set; }
public bool IsOnline => CountOfOpenTabs > 0;
public int CountOfOpenTabs { get; set; }
public Guid SelectedTenantId { get; set; }
public int CountOfOpenTabs { get; private set; }
public Guid SelectedTenantId { get; private set; }
public IList<TenantMembership> TenantMemberships { get; set; }

public static ApplicationUser Create(string userName, string mail, string pictureUri)
{
return new ApplicationUser()
{
UserName = userName,
Email = mail,
PictureUri = pictureUri
};
}

public void SetPictureUri(string pictureUri)
{
PictureUri = pictureUri;
}

public void SelectTenant(Guid tenantId)
{
SelectedTenantId = tenantId;
}

public void IncrementOpenTabCount()
{
CountOfOpenTabs++;
Expand All @@ -29,9 +51,9 @@ public void DecrementOpenTabCount()
CountOfOpenTabs--;
}

public virtual ICollection<IdentityUserLogin<Guid>> Logins { get; set; }
public virtual ICollection<IdentityUserClaim<Guid>> Claims { get; set; }
public virtual ICollection<IdentityUserToken<Guid>> Tokens { get; set; }
public virtual ICollection<IdentityUserLogin<Guid>> Logins { get; private set; }
public virtual ICollection<IdentityUserClaim<Guid>> Claims { get; private set; }
public virtual ICollection<IdentityUserToken<Guid>> Tokens { get; private set; }
}

public class ApplicationUserEFConfiguration : IEntityTypeConfiguration<ApplicationUser>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null)

if (info is not null && user is null)
{
ApplicationUser _user = new ApplicationUser
{
UserName = info.Principal.Identity.Name,
Email = info.Principal.GetClaimValue(ClaimConstants.EmailClaimType),
PictureUri = info.Principal.GetClaimValue(ClaimConstants.PictureClaimType)
};
var name = info.Principal.Identity.Name;
var mail = info.Principal.GetClaimValue(ClaimConstants.EmailClaimType);
var pictureUri = info.Principal.GetClaimValue(ClaimConstants.PictureClaimType);

ApplicationUser _user = ApplicationUser.Create(name, mail, pictureUri);

var createUserCommand = new CreateNewUser { User = _user };
await commandDispatcher.DispatchAsync(createUserCommand);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async Task<ActionResult> SelectTenant([FromRoute] Guid tenantId, [FromQue

if (tenantMemberships.Select(t => t.TenantId).Contains(tenantId))
{
await commandDispatcher.DispatchAsync(new SetSelectedTenantForUser { ExecutingUserId = user.Id, SelectedTenantId = tenantId });
await commandDispatcher.DispatchAsync(new SelectTenant { ExecutingUserId = user.Id, SelectedTenantId = tenantId });
await signInManager.RefreshSignInAsync(user);
}
else
Expand Down

0 comments on commit 92c320b

Please sign in to comment.