Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
akbaramd committed Dec 29, 2024
1 parent f7bbe36 commit 298d50f
Show file tree
Hide file tree
Showing 126 changed files with 2,118 additions and 346 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,23 @@ public static PropertyBuilder<TBussinedId> HasBusinessIdConversion<TBussinedId,
convertFromProviderExpression
);
}
public static PropertyBuilder<TBussinedId> HasEnumeration<TBussinedId>(
this PropertyBuilder<TBussinedId> propertyBuilder)
where TBussinedId : Enumeration
{
Expression<Func<TBussinedId, int>> convertToProviderExpression = c => c.Id;

// Convert TKey to BusinessId using the FromValue method of the BusinessId class
Expression<Func<int, TBussinedId>> convertFromProviderExpression = v => Enumeration.FromId<TBussinedId>(v)!;

return propertyBuilder.HasConversion(
convertToProviderExpression,
convertFromProviderExpression
);
}
public static PropertyBuilder<TBussinedId> HasBusinessIdConversion<TBussinedId>(
this PropertyBuilder<TBussinedId> propertyBuilder)
where TBussinedId : GuidBusinessId<TBussinedId>, new()
where TBussinedId : GuidBusinessId<TBussinedId>?, new()
{
return propertyBuilder.HasBusinessIdConversion<TBussinedId, Guid>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Payeh.SharedKernel.Domain;
using Payeh.SharedKernel.Domain.Repositories;
using Payeh.SharedKernel.EntityFrameworkCore.UnitofWork;
using Payeh.SharedKernel.EntityFrameworkCore.UnitOfWork;

namespace Payeh.SharedKernel.EntityFrameworkCore.Domain;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Linq;
using Payeh.SharedKernel.EntityFrameworkCore.Domain;
using Payeh.SharedKernel.EntityFrameworkCore.UnitofWork;
using Payeh.SharedKernel.EntityFrameworkCore.UnitOfWork;
using Payeh.SharedKernel.UnitOfWork;

namespace Payeh.SharedKernel.EntityFrameworkCore;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging.Abstractions;
using Payeh.SharedKernel.EntityFrameworkCore.UnitofWork;
using Payeh.SharedKernel.UnitOfWork;
using Payeh.SharedKernel.UnitOfWork.Null;

namespace Payeh.SharedKernel.EntityFrameworkCore.UnitOfWork;
namespace Payeh.SharedKernel.EntityFrameworkCore.UnitofWork;

/// <summary>
/// Provides a DbContext instance tied to the current Unit of Work.
Expand Down
2 changes: 2 additions & 0 deletions New/Payeh.SharedKernel/UnitOfWork/IUnitOfWorkOptions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

using System.Data;

namespace Payeh.SharedKernel.UnitOfWork;

public interface IUnitOfWorkOptions
{
public bool IsUnitOfWorkEnabled { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Payeh.SharedKernel.UnitOfWork.Null;

namespace Payeh.SharedKernel.UnitOfWork;
namespace Payeh.SharedKernel.UnitOfWork.Null;

/// <summary>
/// A null implementation of the Unit of Work Manager that returns a NullUnitOfWork.
Expand Down
2 changes: 2 additions & 0 deletions New/Payeh.SharedKernel/UnitOfWork/UnitOfWorkOptions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

using System.Data;

namespace Payeh.SharedKernel.UnitOfWork;

public class UnitOfWorkOptions : IUnitOfWorkOptions
{
public bool IsUnitOfWorkEnabled { get; set; } = true;
Expand Down

This file was deleted.

This file was deleted.

15 changes: 0 additions & 15 deletions Nezam.EES.Service.Identity/Properties/launchSettings.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Nezam.EES.Service.Identity.Application.Dto.Roles;
using Nezam.EES.Service.Identity.Domains.Users;
using Nezam.EEs.Shared.Domain.Identity.User;
using Nezam.EEs.Shared.Domain.Identity.User.ValueObjects;
Expand All @@ -10,6 +11,7 @@ public class UserDto
public UserNameId UserName { get; set; }
public UserEmailValue? Email { get; set; }
public UserProfileValue Profile { get; set; } // Optional profile info
public List<RoleDto> Roles { get; set; } // Optional profile info

// Static method for mapping UserEntity to UserDto
public static UserDto FromEntity(UserEntity user)
Expand All @@ -19,7 +21,8 @@ public static UserDto FromEntity(UserEntity user)
UserId = user.UserId,
UserName = user.UserName, // Assuming UserName is a value object
Email = user.Email, // Assuming Email is a value object
Profile = user.Profile
Profile = user.Profile,
Roles = user.Roles.Select(x => RoleDto.FromEntity(x)).ToList()
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public override async Task HandleAsync(CreateRoleRequest req, CancellationToken
{
// شروع تراکنش

// ایجاد نقش جدید با استفاده از Domain Service
// ایجاد نقش جدید با استفاده از Domains Service
var roleResult = await _roleDomainService.CreateRoleAsync(new RoleEntity(req.Id, req.Title));

if (roleResult.IsFailure)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Consul;
using FastEndpoints;
using Nezam.EES.Service.Identity.Domains.Roles.DomainServices;
using Nezam.EES.Service.Identity.Domains.Users;
using Nezam.EES.Service.Identity.Domains.Users.DomainServices;
using Nezam.EEs.Shared.Domain.Identity.Roles;
using Nezam.EEs.Shared.Domain.Identity.User;
using Payeh.SharedKernel.UnitOfWork;

namespace Nezam.EES.Service.Identity.Application.UseCases.Users.AssignRoles;

public class AssignRolesEndpoint : Endpoint<AssignRolesRecord>
{
private readonly IUserDomainService _userDomainService;
private readonly IRoleDomainService _roleDomainService;
private readonly IUnitOfWorkManager _unitOfWork;

public AssignRolesEndpoint(IUserDomainService userDomainService, IRoleDomainService roleDomainService, IUnitOfWorkManager unitOfWork)
{
_userDomainService = userDomainService;
_roleDomainService = roleDomainService;
_unitOfWork = unitOfWork;
}

public override void Configure()
{
Put("/api/users/{id:guid}/assign-roles");
}

public override async Task HandleAsync(AssignRolesRecord req, CancellationToken ct)
{
var userIdGuid = Route<Guid>("id", true);
var userId = UserId.NewId(userIdGuid);

using var uow = _unitOfWork.Begin();
try
{
var existingUserResult = await _userDomainService.GetUserByIdAsync(userId);

if (existingUserResult.IsFailure)
{
ThrowError("User not found.");
}

var existingUser = existingUserResult.Data;

// Retrieve roles by IDs from the request
var roleIds = req.Roles.Select(RoleId.NewId).ToArray();




var updateResult = await _userDomainService.AssignRoleAsync(existingUser,roleIds);

if (updateResult.IsFailure)
{
await SendErrorsAsync(cancellation: ct);
return;
}

await uow.CommitAsync(ct);

await SendOkAsync(ct);
}
catch (Exception ex)
{
await uow.RollbackAsync(ct);
ThrowError($"Error while assigning roles to user: {ex.Message}");
}
}
}

public record AssignRolesRecord
{
public List<string> Roles { get; set; } = new();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using FastEndpoints;
using Nezam.EES.Service.Identity.Domains.Users;
using Nezam.EES.Service.Identity.Domains.Users.DomainServices;
using Nezam.EEs.Shared.Domain.Identity.User;
using Nezam.EEs.Shared.Domain.Identity.User.ValueObjects;
using Payeh.SharedKernel.UnitOfWork;

namespace Nezam.EES.Service.Identity.Application.UseCases.Users.CreateUser;

public class CreateUserEndpoint : Endpoint<CreateUserRecord>
{
private readonly IUserDomainService _userDomainService;
private readonly IUnitOfWorkManager _unitOfWork;

public CreateUserEndpoint(IUserDomainService roleDomainService, IUnitOfWorkManager unitOfWork)
{
_userDomainService = roleDomainService;
_unitOfWork = unitOfWork;
}

public override void Configure()
{
Post("/api/users");

}

public override async Task HandleAsync(CreateUserRecord req, CancellationToken ct)
{
if (string.IsNullOrWhiteSpace(req.UserName))
{
ThrowError("UserName cannot be empty.");
}

using var uow = _unitOfWork.Begin();
try
{

var user = new UserEntity(UserId.NewId(), UserNameId.NewId(req.UserName),new UserPasswordValue(req.Password),new UserProfileValue(req.FirstName,req.LastName),new UserEmailValue(req.Email));
var createResult = await _userDomainService.Create(user);

if (createResult.IsFailure)
{
await SendErrorsAsync(cancellation: ct);
return;
}
// ذخیره تغییرات
await uow.CommitAsync(ct);

await SendOkAsync(ct);
}
catch (Exception ex)
{
await uow.RollbackAsync(ct);
ThrowError($"Error while updating role: {ex.Message}");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Nezam.EES.Service.Identity.Application.UseCases.Users.CreateUser;

public record CreateUserRecord(string UserName,string Password,string FirstName,string LastName,string Email);
Loading

0 comments on commit 298d50f

Please sign in to comment.