Skip to content

Commit

Permalink
Issue #59: Update login function.
Browse files Browse the repository at this point in the history
  • Loading branch information
akazad13 committed Dec 22, 2024
1 parent 4bbf736 commit d2c5a54
Show file tree
Hide file tree
Showing 18 changed files with 1,169 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/Shopizy.Api/Controllers/PaymentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class PaymentController(ISender mediator, IMapper mapper) : ApiController
[SwaggerResponse(StatusCodes.Status500InternalServerError, null, typeof(ErrorResult))]
public async Task<IActionResult> CreateSaleAsync(CardNotPresentSaleRequest request)
{
if (request.PaymentMethod.ToLower() == "Card")
if (request.PaymentMethod.ToLower() == "card")
{
var command = _mapper.Map<CardNotPresentSaleCommand>(request);
var result = await _mediator.Send(command);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

namespace Shopizy.Application.Authentication.Commands.Register;

public record RegisterCommand(string FirstName, string LastName, string Phone, string Password)
public record RegisterCommand(string FirstName, string LastName, string Email, string Password)
: IRequest<ErrorOr<Success>>;
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public async Task<ErrorOr<Success>> Handle(
CancellationToken cancellationToken
)
{
if ((await _userRepository.GetUserByPhoneAsync(command.Phone)) is not null)
if ((await _userRepository.GetUserByEmailAsync(command.Email)) is not null)
{
return CustomErrors.User.DuplicatePhone;
return CustomErrors.User.DuplicateEmail;
}

if (
Expand Down Expand Up @@ -54,7 +54,7 @@ CancellationToken cancellationToken
var user = User.Create(
command.FirstName,
command.LastName,
command.Phone,
command.Email,
hashedPassword,
permissionIds
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public RegisterCommandValidator()
{
RuleFor(register => register.FirstName).NotNull().NotEmpty().MaximumLength(50);
RuleFor(register => register.LastName).NotNull().NotEmpty().MaximumLength(50);
RuleFor(register => register.Phone).NotNull().NotEmpty().MaximumLength(15);
RuleFor(register => register.Email).NotNull().NotEmpty().MaximumLength(50).EmailAddress();
RuleFor(register => register.Password).NotNull().NotEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace Shopizy.Application.Authentication.Common;

public record AuthResult(Guid Id, string FirstName, string LastName, string Phone, string Token);
public record AuthResult(Guid Id, string FirstName, string LastName, string Email, string Token);
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

namespace Shopizy.Application.Authentication.Queries.login;

public record LoginQuery(string Phone, string Password) : IRequest<ErrorOr<AuthResult>>;
public record LoginQuery(string Email, string Password) : IRequest<ErrorOr<AuthResult>>;
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ CancellationToken cancellationToken
{
cancellationToken.ThrowIfCancellationRequested();

var user = await _userRepository.GetUserByPhoneAsync(query.Phone);
var user = await _userRepository.GetUserByEmailAsync(query.Email);
if (user is null)
{
return CustomErrors.User.UserNotFoundWhileLogin;
Expand All @@ -52,6 +52,6 @@ CancellationToken cancellationToken

var token = _jwtTokenGenerator.GenerateToken(user.Id, roles, assignedPermissions);

return new AuthResult(user.Id.Value, user.FirstName, user.LastName, user.Phone, token);
return new AuthResult(user.Id.Value, user.FirstName, user.LastName, user.Email, token);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Shopizy.Application.Common.Interfaces.Persistence;

public interface IUserRepository
{
Task<User?> GetUserByPhoneAsync(string phone);
Task<User?> GetUserByEmailAsync(string email);
Task<User?> GetUserById(UserId id);
Task AddAsync(User user);
void Update(User user);
Expand Down
2 changes: 1 addition & 1 deletion src/Shopizy.Contracts/Authentication/LoginRequest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace Shopizy.Contracts.Authentication;

public record LoginRequest(string Phone, string Password);
public record LoginRequest(string Email, string Password);
2 changes: 1 addition & 1 deletion src/Shopizy.Contracts/Authentication/RegisterRequest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace Shopizy.Contracts.Authentication;

public record RegisterRequest(string FirstName, string LastName, string Phone, string Password);
public record RegisterRequest(string FirstName, string LastName, string Email, string Password);
2 changes: 1 addition & 1 deletion src/Shopizy.Contracts/Cart/CartResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ CartProductResponse Product
public record CartProductResponse(
string Name,
string Description,
string Price,
decimal Price,
decimal Discount,
string Brand,
int StockQuantity,
Expand Down
4 changes: 2 additions & 2 deletions src/Shopizy.Domain/Common/CustomErrors/CustomErrors.User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public static partial class CustomErrors
{
public static class User
{
public static Error DuplicatePhone =>
Error.Conflict(code: "User.DuplicatePhone", description: "Phone is already in use.");
public static Error DuplicateEmail =>
Error.Conflict(code: "User.DuplicateEmail", description: "Email is already in use.");
public static Error UserNotFoundWhileLogin =>
Error.Unauthorized(
code: "User.UserNotFound",
Expand Down
20 changes: 9 additions & 11 deletions src/Shopizy.Domain/Users/User.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
using Shopizy.Domain.Common.Models;
using Shopizy.Domain.Orders;
using Shopizy.Domain.Orders.ValueObjects;
using Shopizy.Domain.Permissions.ValueObjects;
using Shopizy.Domain.ProductReviews;
using Shopizy.Domain.ProductReviews.ValueObjects;
using Shopizy.Domain.Users.ValueObjects;

namespace Shopizy.Domain.Users;

public sealed class User : AggregateRoot<UserId, Guid>
{
private readonly IList<Order> _orders = [];
private readonly IList<ProductReview> _productReviews = [];
private readonly IList<OrderId> _orderIds = [];
private readonly IList<ProductReviewId> _productReviewIds = [];
private readonly IList<PermissionId> _permissionIds = [];
public string FirstName { get; private set; }
public string LastName { get; private set; }

public string Email { get; private set; }
public string? ProfileImageUrl { get; }
public string Phone { get; private set; }
Expand All @@ -24,19 +22,19 @@ public sealed class User : AggregateRoot<UserId, Guid>
public DateTime CreatedOn { get; private set; }
public DateTime? ModifiedOn { get; private set; }

public IReadOnlyList<Order> Orders => _orders.AsReadOnly();
public IReadOnlyList<ProductReview> ProductReviews => _productReviews.AsReadOnly();
public IReadOnlyList<OrderId> OrderIds => _orderIds.AsReadOnly();
public IReadOnlyList<ProductReviewId> ProductReviewIds => _productReviewIds.AsReadOnly();
public IReadOnlyList<PermissionId> PermissionIds => _permissionIds.AsReadOnly();

public static User Create(
string firstName,
string lastName,
string phone,
string email,
string? password,
IList<PermissionId> permissionIds
)
{
return new(UserId.CreateUnique(), firstName, lastName, phone, password, permissionIds);
return new(UserId.CreateUnique(), firstName, lastName, email, password, permissionIds);
}

private User() { }
Expand All @@ -45,15 +43,15 @@ private User(
UserId userId,
string firstName,
string lastName,
string phone,
string email,
string? password,
IList<PermissionId> permissionIds
)
: base(userId)
{
FirstName = firstName;
LastName = lastName;
Phone = phone;
Phone = email;
Password = password;
_permissionIds = permissionIds.ToList();
CreatedOn = DateTime.UtcNow;
Expand Down
Loading

0 comments on commit d2c5a54

Please sign in to comment.