Skip to content

Commit

Permalink
refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
akhadov committed Jun 24, 2024
1 parent c091758 commit 131ea1f
Show file tree
Hide file tree
Showing 33 changed files with 199 additions and 406 deletions.
13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ services:
ports:
- 5432:5432

idp:
image: quay.io/keycloak/keycloak:latest
container_name: identity
command: start-dev --import-realm
environment:
- KEYCLOAK_ADMIN=admin
- KEYCLOAK_ADMIN_PASSWORD=admin
volumes:
- ./.containers/identity:/opt/keycloak/data
- ./.files/uzbekcuisines-realm-export.json:/opt/keycloak/data/import/realm.json
ports:
- 18080:8080

seq:
image: datalust/seq:latest
container_name: seq
Expand Down
6 changes: 0 additions & 6 deletions src/Application/Users/Create/CreateUserCommand.cs

This file was deleted.

37 changes: 0 additions & 37 deletions src/Application/Users/Create/CreateUserCommandHandler.cs

This file was deleted.

16 changes: 0 additions & 16 deletions src/Application/Users/Create/CreateUserCommandValidator.cs

This file was deleted.

3 changes: 0 additions & 3 deletions src/Application/Users/Create/CreateUserRequest.cs

This file was deleted.

5 changes: 0 additions & 5 deletions src/Application/Users/GetByEmail/GetUserByEmailQuery.cs

This file was deleted.

39 changes: 0 additions & 39 deletions src/Application/Users/GetByEmail/GetUserByEmailQueryHandler.cs

This file was deleted.

12 changes: 0 additions & 12 deletions src/Application/Users/GetByEmail/UserResponse.cs

This file was deleted.

10 changes: 0 additions & 10 deletions src/Application/Users/GetById/GetUserByIdQuery.cs

This file was deleted.

39 changes: 0 additions & 39 deletions src/Application/Users/GetById/GetUserByIdQueryHandler.cs

This file was deleted.

12 changes: 0 additions & 12 deletions src/Application/Users/GetById/UserResponse.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Data.Common;
using Application.Abstractions.Authentication;
using Application.Abstractions.Data;
using Application.Abstractions.Messaging;
using Dapper;
using SharedKernel;

namespace Application.Users.GetLoggedInUser;
internal sealed class GetLoggedInUserQueryHandler(
IDbConnectionFactory dbConnectionFactory,
IUserContext userContext)
: IQueryHandler<GetLoggedInUserQuery, UserResponse>
{
public async Task<Result<UserResponse>> Handle(
GetLoggedInUserQuery request,
CancellationToken cancellationToken)
{
await using DbConnection connection = await dbConnectionFactory.OpenConnectionAsync();

const string sql = """
SELECT
id AS Id,
first_name AS FirstName,
last_name AS LastName,
email AS Email
FROM users
WHERE identity_id = @IdentityId
""";

UserResponse user = await connection.QuerySingleAsync<UserResponse>(
sql,
new
{
userContext.IdentityId
});

return user;
}
}
35 changes: 11 additions & 24 deletions src/Application/Users/RegisterUser/RegisterUserCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,27 @@

namespace Application.Users.RegisterUser;
internal sealed class RegisterUserCommandHandler(
IAuthenticationService authenticationService,
//IAuthenticationService authenticationService,
IUserRepository userRepository,
IUnitOfWork unitOfWork) : ICommandHandler<RegisterUserCommand, Guid>
{
public async Task<Result<Guid>> Handle(
RegisterUserCommand request,
CancellationToken cancellationToken)
{
Result<Email> emailResult = Email.Create(request.Email);
var user = User.Create(
new FirstName(request.FirstName),
new LastName(request.LastName),
new Email(request.Email));

if (emailResult.IsFailure)
{
return Result.Failure<Guid>(emailResult.Error);
}
//string identityId = await authenticationService.RegisterAsync(
// user,
// request.Password,
// cancellationToken);

Email email = emailResult.Value;
if (!await userRepository.IsEmailUniqueAsync(email))
{
return Result.Failure<Guid>(UserErrors.EmailNotUnique);
}
//user.SetIdentityId(identityId);

var firstName = new FirstName(request.FirstName);
var lastName = new LastName(request.LastName);

var user = User.Create(email, firstName, lastName);

string identityId = await authenticationService.RegisterAsync(
user,
request.Password,
cancellationToken);

user.SetIdentityId(identityId);

userRepository.Insert(user);
userRepository.Add(user);

await unitOfWork.SaveChangesAsync(cancellationToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@ internal sealed class RegisterUserCommandValidator : AbstractValidator<RegisterU
{
public RegisterUserCommandValidator()
{
RuleFor(c => c.FirstName)
.NotEmpty().WithErrorCode(UserErrorCodes.CreateUser.MissingFirstName);
RuleFor(c => c.FirstName).NotEmpty();

RuleFor(c => c.LastName)
.NotEmpty().WithErrorCode(UserErrorCodes.CreateUser.MissingLastName);
RuleFor(c => c.LastName).NotEmpty();

RuleFor(c => c.Email)
.NotEmpty().WithErrorCode(UserErrorCodes.CreateUser.MissingEmail)
.EmailAddress().WithErrorCode(UserErrorCodes.CreateUser.InvalidEmail);
RuleFor(c => c.Email).EmailAddress();

RuleFor(c => c.Password).NotEmpty().MinimumLength(5);
}
Expand Down
2 changes: 0 additions & 2 deletions src/Application/Users/UserErrorCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@ public static class CreateUser
{
public const string MissingFirstName = nameof(MissingFirstName);
public const string MissingLastName = nameof(MissingLastName);
public const string MissingEmail = nameof(MissingEmail);
public const string InvalidEmail = nameof(InvalidEmail);
}
}
12 changes: 0 additions & 12 deletions src/Application/Users/UserNotFoundException.cs

This file was deleted.

27 changes: 2 additions & 25 deletions src/Domain/Users/Email.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,2 @@
using SharedKernel;

namespace Domain.Users;

public sealed record Email
{
private Email(string value) => Value = value;

public string Value { get; }

public static Result<Email> Create(string? email)
{
if (string.IsNullOrEmpty(email))
{
return Result.Failure<Email>(EmailErrors.Empty);
}

//if (email.Split('@').Length != 2)
//{
// return Result.Failure<Email>(EmailErrors.InvalidFormat);
//}

return new Email(email);
}
}
namespace Domain.Users;
public sealed record Email(string Value);
11 changes: 0 additions & 11 deletions src/Domain/Users/EmailErrors.cs

This file was deleted.

15 changes: 2 additions & 13 deletions src/Domain/Users/FirstName.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
using SharedKernel;
namespace Domain.Users;

namespace Domain.Users;
public sealed record FirstName(string Value);

public sealed record FirstName
{
public FirstName(string? value)
{
Ensure.NotNullOrEmpty(value);

Value = value;
}

public string Value { get; }
}
Loading

0 comments on commit 131ea1f

Please sign in to comment.