diff --git a/src/CMSMicroservice.Application/UserCQ/Commands/SetPasswordForUser/SetPasswordForUserCommand.cs b/src/CMSMicroservice.Application/UserCQ/Commands/SetPasswordForUser/SetPasswordForUserCommand.cs new file mode 100644 index 0000000..0c07b42 --- /dev/null +++ b/src/CMSMicroservice.Application/UserCQ/Commands/SetPasswordForUser/SetPasswordForUserCommand.cs @@ -0,0 +1,13 @@ +namespace CMSMicroservice.Application.UserCQ.Commands.SetPasswordForUser; +public record SetPasswordForUserCommand : IRequest +{ + //شناسه کاربر + public long UserId { get; init; } + //کلمه عبور فعلی + public string? CurrentPassword { get; init; } + //کلمه عبور + public string NewPassword { get; init; } + //تایید کلمه عبور + public string ConfirmPassword { get; init; } + +} \ No newline at end of file diff --git a/src/CMSMicroservice.Application/UserCQ/Commands/SetPasswordForUser/SetPasswordForUserCommandHandler.cs b/src/CMSMicroservice.Application/UserCQ/Commands/SetPasswordForUser/SetPasswordForUserCommandHandler.cs new file mode 100644 index 0000000..72ca63e --- /dev/null +++ b/src/CMSMicroservice.Application/UserCQ/Commands/SetPasswordForUser/SetPasswordForUserCommandHandler.cs @@ -0,0 +1,17 @@ +using CMSMicroservice.Domain.Events; +namespace CMSMicroservice.Application.UserCQ.Commands.SetPasswordForUser; +public class SetPasswordForUserCommandHandler : IRequestHandler +{ + private readonly IApplicationDbContext _context; + + public SetPasswordForUserCommandHandler(IApplicationDbContext context) + { + _context = context; + } + + public async Task Handle(SetPasswordForUserCommand request, CancellationToken cancellationToken) + { + //TODO: Implement your business logic + return new Unit(); + } +} diff --git a/src/CMSMicroservice.Application/UserCQ/Commands/SetPasswordForUser/SetPasswordForUserCommandValidator.cs b/src/CMSMicroservice.Application/UserCQ/Commands/SetPasswordForUser/SetPasswordForUserCommandValidator.cs new file mode 100644 index 0000000..4847cec --- /dev/null +++ b/src/CMSMicroservice.Application/UserCQ/Commands/SetPasswordForUser/SetPasswordForUserCommandValidator.cs @@ -0,0 +1,20 @@ +namespace CMSMicroservice.Application.UserCQ.Commands.SetPasswordForUser; +public class SetPasswordForUserCommandValidator : AbstractValidator +{ + public SetPasswordForUserCommandValidator() + { + RuleFor(model => model.UserId) + .NotNull(); + RuleFor(model => model.NewPassword) + .NotEmpty(); + RuleFor(model => model.ConfirmPassword) + .NotEmpty(); + } + public Func>> ValidateValue => async (model, propertyName) => + { + var result = await ValidateAsync(ValidationContext.CreateWithOptions((SetPasswordForUserCommand)model, x => x.IncludeProperties(propertyName))); + if (result.IsValid) + return Array.Empty(); + return result.Errors.Select(e => e.ErrorMessage); + }; +} diff --git a/src/CMSMicroservice.Application/UserCQ/EventHandlers/SetPasswordForUserEventHandlers/SetPasswordForUserEventHandler.cs b/src/CMSMicroservice.Application/UserCQ/EventHandlers/SetPasswordForUserEventHandlers/SetPasswordForUserEventHandler.cs new file mode 100644 index 0000000..062d465 --- /dev/null +++ b/src/CMSMicroservice.Application/UserCQ/EventHandlers/SetPasswordForUserEventHandlers/SetPasswordForUserEventHandler.cs @@ -0,0 +1,22 @@ +using CMSMicroservice.Domain.Events; +using Microsoft.Extensions.Logging; + +namespace CMSMicroservice.Application.UserCQ.EventHandlers; + +public class SetPasswordForUserEventHandler : INotificationHandler +{ + private readonly ILogger< + SetPasswordForUserEventHandler> _logger; + + public SetPasswordForUserEventHandler(ILogger logger) + { + _logger = logger; + } + + public Task Handle(SetPasswordForUserEvent notification, CancellationToken cancellationToken) + { + _logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name); + + return Task.CompletedTask; + } +} diff --git a/src/CMSMicroservice.Domain/Events/UserEvents/SetPasswordForUserEvent.cs b/src/CMSMicroservice.Domain/Events/UserEvents/SetPasswordForUserEvent.cs new file mode 100644 index 0000000..ea8c7da --- /dev/null +++ b/src/CMSMicroservice.Domain/Events/UserEvents/SetPasswordForUserEvent.cs @@ -0,0 +1,8 @@ +namespace CMSMicroservice.Domain.Events; +public class SetPasswordForUserEvent : BaseEvent +{ + public SetPasswordForUserEvent(User item) + { + } + public User Item { get; } +} diff --git a/src/CMSMicroservice.Protobuf/Protos/user.proto b/src/CMSMicroservice.Protobuf/Protos/user.proto index 4ae9071..88bf61f 100644 --- a/src/CMSMicroservice.Protobuf/Protos/user.proto +++ b/src/CMSMicroservice.Protobuf/Protos/user.proto @@ -55,6 +55,12 @@ service UserContract }; }; + rpc SetPasswordForUser(SetPasswordForUserRequest) returns (google.protobuf.Empty){ + option (google.api.http) = { + post: "/SetPasswordForUser" + body: "*" + }; + }; } message CreateNewUserRequest { @@ -174,3 +180,10 @@ message AdminGetJwtTokenResponse { string token = 1; } +message SetPasswordForUserRequest +{ + int64 user_id = 1; + google.protobuf.StringValue current_password = 2; + string new_password = 3; + string confirm_password = 4; +} diff --git a/src/CMSMicroservice.Protobuf/Validator/User/SetPasswordForUserRequestValidator.cs b/src/CMSMicroservice.Protobuf/Validator/User/SetPasswordForUserRequestValidator.cs new file mode 100644 index 0000000..c8d7ac0 --- /dev/null +++ b/src/CMSMicroservice.Protobuf/Validator/User/SetPasswordForUserRequestValidator.cs @@ -0,0 +1,23 @@ +using FluentValidation; +using CMSMicroservice.Protobuf.Protos.User; +namespace CMSMicroservice.Protobuf.Validator.User; + +public class SetPasswordForUserRequestValidator : AbstractValidator +{ + public SetPasswordForUserRequestValidator() + { + RuleFor(model => model.UserId) + .NotNull(); + RuleFor(model => model.NewPassword) + .NotEmpty(); + RuleFor(model => model.ConfirmPassword) + .NotEmpty(); + } + public Func>> ValidateValue => async (model, propertyName) => + { + var result = await ValidateAsync(ValidationContext.CreateWithOptions((SetPasswordForUserRequest)model, x => x.IncludeProperties(propertyName))); + if (result.IsValid) + return Array.Empty(); + return result.Errors.Select(e => e.ErrorMessage); + }; +} diff --git a/src/CMSMicroservice.WebApi/Services/UserService.cs b/src/CMSMicroservice.WebApi/Services/UserService.cs index f5a8d05..13abc6b 100644 --- a/src/CMSMicroservice.WebApi/Services/UserService.cs +++ b/src/CMSMicroservice.WebApi/Services/UserService.cs @@ -7,6 +7,7 @@ using CMSMicroservice.Application.UserCQ.Queries.GetUser; using CMSMicroservice.Application.UserCQ.Queries.GetAllUserByFilter; using CMSMicroservice.Application.UserCQ.Queries.GetJwtToken; using CMSMicroservice.Application.UserCQ.Queries.AdminGetJwtToken; +using CMSMicroservice.Application.UserCQ.Commands.SetPasswordForUser; namespace CMSMicroservice.WebApi.Services; public class UserService : UserContract.UserContractBase { @@ -44,4 +45,8 @@ public class UserService : UserContract.UserContractBase { return await _dispatchRequestToCQRS.Handle(request, context); } + public override async Task SetPasswordForUser(SetPasswordForUserRequest request, ServerCallContext context) + { + return await _dispatchRequestToCQRS.Handle(request, context); + } }