Skip to content

Commit

Permalink
Merge pull request #13 from Star-Academy/refactor/refactor-account-apis
Browse files Browse the repository at this point in the history
refactor: refactor account related parts
  • Loading branch information
mobinbr authored Aug 19, 2024
2 parents 3cd7807 + c109f86 commit b25b194
Show file tree
Hide file tree
Showing 24 changed files with 258 additions and 240 deletions.
15 changes: 15 additions & 0 deletions src/Application/DTOs/Account/AccountCsvModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Application.DTOs.Account;

public class AccountCsvModel
{
public long AccountId { get; set; }
public long CardId { get; set; }
public string Iban { get; set; } = string.Empty;
public string AccountType { get; set; } = string.Empty;
public string BranchTelephone { get; set; } = string.Empty;
public string BranchAddress { get; set; } = string.Empty;
public string BranchName { get; set; } = string.Empty;
public string OwnerName { get; set; } = string.Empty;
public string OwnerLastName { get; set; } = string.Empty;
public string OwnerId { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Application.DTOs.AccountCsv;
namespace Application.DTOs.Account;

public class GetAllAccountsResponse
{
Expand Down
15 changes: 0 additions & 15 deletions src/Application/DTOs/AccountCsv/AccountCsvModel.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Application/Interfaces/IJwtGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Domain.Entities;

namespace Web.Interfaces;
namespace Application.Interfaces;

public interface IJwtGenerator
{
Expand Down
6 changes: 0 additions & 6 deletions src/Application/Interfaces/IRoleManager.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Domain.Entities;

namespace Application.Interfaces;
namespace Application.Interfaces.Repositories;

public interface IAccountRepository
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Application.Interfaces.Repositories;

public interface IRoleManagerRepository
{
Task<bool> RoleExistsAsync(string roleName);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Domain.Entities;

namespace Application.Interfaces;

public interface ITransactionRepository
{
Task CreateBulkAsync(List<Transaction> transactions);
Task<List<Transaction>> GetAllTransactions();
using Domain.Entities;

namespace Application.Interfaces.Repositories;

public interface ITransactionRepository
{
Task CreateBulkAsync(List<Transaction> transactions);
Task<List<Transaction>> GetAllTransactions();
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
using Application.DTOs;
using Domain.Entities;
using Microsoft.AspNetCore.Identity;

namespace Application.Interfaces;

public interface IUserManager
{
Task<IdentityResult> CreateAsync(AppUser user, string password);
Task<IdentityResult> SetRoleAsync(AppUser user, string role);
Task<IdentityResult> ChangeRoleAsync(AppUser user, string newRole);
Task<AppUser?> FindByNameAsync(string userName);
Task<AppUser?> FindByEmailAsync(string email);
Task<AppUser?> FindByIdAsync(string userId);
Task<IdentityResult> UpdateAsync(AppUser user);
Task<IdentityResult> ChangePasswordAsync(AppUser user, string currentPassword, string newPassword);
Task<string> GetRoleAsync(AppUser user);
Task<bool> CheckPasswordAsync(AppUser user, string password);
using Domain.Entities;
using Microsoft.AspNetCore.Identity;

namespace Application.Interfaces.Repositories;

public interface IUserManagerRepository
{
Task<IdentityResult> CreateAsync(AppUser user, string password);
Task<IdentityResult> SetRoleAsync(AppUser user, string role);
Task<IdentityResult> ChangeRoleAsync(AppUser user, string newRole);
Task<AppUser?> FindByNameAsync(string userName);
Task<AppUser?> FindByEmailAsync(string email);
Task<AppUser?> FindByIdAsync(string userId);
Task<IdentityResult> UpdateAsync(AppUser user);
Task<IdentityResult> ChangePasswordAsync(AppUser user, string currentPassword, string newPassword);
Task<string> GetRoleAsync(AppUser user);
Task<bool> CheckPasswordAsync(AppUser user, string password);
}
2 changes: 1 addition & 1 deletion src/Application/Interfaces/Services/IAccountService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Application.DTOs;
using Application.DTOs.AccountCsv;
using Application.DTOs.Account;
using Domain.Entities;

namespace Application.Interfaces.Services;
Expand Down
6 changes: 0 additions & 6 deletions src/Application/Interfaces/SharedService/ICsvService.cs

This file was deleted.

12 changes: 6 additions & 6 deletions src/Application/Mappers/AccountMapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Application.DTOs.AccountCsv;
using Application.DTOs.Account;
using Domain.Entities;

namespace Application.Mappers;
Expand All @@ -11,16 +11,16 @@ public static GetAllAccountsResponse ToGetAllAccountsResponse(this List<Account>
{
Accounts = accounts.Select(account => new AccountCsvModel
{
AccountID = account.AccountId,
CardID = account.CardId,
IBAN = account.Iban,
AccountId = account.AccountId,
CardId = account.CardId,
Iban = account.Iban,
AccountType = account.AccountType,
BranchTelephone = account.BranchTelephone,
BranchAdress = account.BranchAddress,
BranchAddress = account.BranchAddress,
BranchName = account.BranchName,
OwnerName = account.OwnerName,
OwnerLastName = account.OwnerLastName,
OwnerID = account.OwnerId
OwnerId = account.OwnerId
}).ToList()
};
}
Expand Down
13 changes: 7 additions & 6 deletions src/Application/Services/AccountService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Application.DTOs;
using Application.DTOs.AccountCsv;
using Application.DTOs.Account;
using Application.Interfaces;
using Application.Interfaces.Repositories;
using Application.Interfaces.Services;
using Application.Mappers;
using Application.Services.SharedService;
Expand All @@ -23,16 +24,16 @@ public async Task AddAccountsFromCsvAsync(string filePath)

var accounts = accountCsvModels.Select(csvModel => new Account
{
AccountId = csvModel.AccountID,
CardId = csvModel.CardID,
Iban = csvModel.IBAN,
AccountId = csvModel.AccountId,
CardId = csvModel.CardId,
Iban = csvModel.Iban,
AccountType = csvModel.AccountType,
BranchTelephone = csvModel.BranchTelephone,
BranchAddress = csvModel.BranchAdress,
BranchAddress = csvModel.BranchAddress,
BranchName = csvModel.BranchName,
OwnerName = csvModel.OwnerName,
OwnerLastName = csvModel.OwnerLastName,
OwnerId = csvModel.OwnerID
OwnerId = csvModel.OwnerId
}).ToList();

await _accountRepository.CreateBulkAsync(accounts);
Expand Down
34 changes: 17 additions & 17 deletions src/Application/Services/IdentityService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,43 @@
using Application.DTOs.Identity.ChangeRole;
using Application.ExtensionMethods;
using Application.Interfaces;
using Application.Interfaces.Repositories;
using Application.Interfaces.Services;
using Application.Mappers;
using Domain.Entities;
using Web.Interfaces;

namespace Application.Services;

public class IdentityService : IIdentityService
{
private readonly IUserManager _userManager;
private readonly IRoleManager _roleManager;
private readonly IUserManagerRepository _userManagerRepository;
private readonly IRoleManagerRepository _roleManagerRepository;
private readonly IJwtGenerator _jwtGenerator;
public IdentityService(IUserManager userManager,
IRoleManager roleManager,
public IdentityService(IUserManagerRepository userManagerRepository,
IRoleManagerRepository roleManagerRepository,
IJwtGenerator jwtGenerator)
{
_userManager = userManager;
_roleManager = roleManager;
_userManagerRepository = userManagerRepository;
_roleManagerRepository = roleManagerRepository;
_jwtGenerator = jwtGenerator;
}

public async Task<Result<CreateUserResponse>> SignUpUser(CreateUserRequest createUserRequest)
{
if (!await _roleManager.RoleExistsAsync(createUserRequest.Role))
if (!await _roleManagerRepository.RoleExistsAsync(createUserRequest.Role))
{
return Result<CreateUserResponse>.Fail("role does not exist");
}

var appUser = createUserRequest.ToAppUser();

var appUserResult = await _userManager.CreateAsync(appUser, createUserRequest.Password);
var appUserResult = await _userManagerRepository.CreateAsync(appUser, createUserRequest.Password);
if (!appUserResult.Succeeded)
{
return Result<CreateUserResponse>.Fail(appUserResult.Errors.FirstMessage());
}

var roleResult = await _userManager.SetRoleAsync(appUser, createUserRequest.Role);
var roleResult = await _userManagerRepository.SetRoleAsync(appUser, createUserRequest.Role);
if (!roleResult.Succeeded)
{
return Result<CreateUserResponse>.Fail(roleResult.Errors.FirstMessage());
Expand All @@ -54,11 +54,11 @@ public async Task<Result<LoginUserResponse>> Login(LoginUserRequest loginUserReq

if (!string.IsNullOrEmpty(loginUserRequest.UserName))
{
appUser = await _userManager.FindByNameAsync(loginUserRequest.UserName);
appUser = await _userManagerRepository.FindByNameAsync(loginUserRequest.UserName);
}
else if (!string.IsNullOrEmpty(loginUserRequest.Email))
{
appUser = await _userManager.FindByEmailAsync(loginUserRequest.Email);
appUser = await _userManagerRepository.FindByEmailAsync(loginUserRequest.Email);
}
else
{
Expand All @@ -67,27 +67,27 @@ public async Task<Result<LoginUserResponse>> Login(LoginUserRequest loginUserReq

if (appUser is null) return Result<LoginUserResponse>.Fail("Invalid username/email!");

var succeed = await _userManager.CheckPasswordAsync(appUser, loginUserRequest.Password);
var succeed = await _userManagerRepository.CheckPasswordAsync(appUser, loginUserRequest.Password);

if (!succeed) return Result<LoginUserResponse>.Fail("Username/Email not found and/or password incorrect");

var role = await _userManager.GetRoleAsync(appUser);
var role = await _userManagerRepository.GetRoleAsync(appUser);
var token = _jwtGenerator.GenerateToken(appUser, role);

return Result<LoginUserResponse>.Ok(appUser.ToLoginUserResponse(role, token));
}

public async Task<Result> ChangeRole(ChangeRoleRequest request)
{
if (!await _roleManager.RoleExistsAsync(request.Role))
if (!await _roleManagerRepository.RoleExistsAsync(request.Role))
{
return Result.Fail("role does not exist");
}
AppUser? appUser = await _userManager.FindByNameAsync(request.UserName);
AppUser? appUser = await _userManagerRepository.FindByNameAsync(request.UserName);

if (appUser is null) return Result<LoginUserResponse>.Fail("Invalid username");

var result = await _userManager.ChangeRoleAsync(appUser, request.Role);
var result = await _userManagerRepository.ChangeRoleAsync(appUser, request.Role);

return result.Succeeded ? Result.Ok() : Result.Fail(result.Errors.FirstMessage());
}
Expand Down
23 changes: 12 additions & 11 deletions src/Application/Services/ProfileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,31 @@
using Application.DTOs.Profile.ChangePassword;
using Application.ExtensionMethods;
using Application.Interfaces;
using Application.Interfaces.Repositories;
using Application.Interfaces.Services;
using Application.Mappers;

namespace Application.Services;

public class ProfileService : IProfileService
{
private readonly IUserManager _userManager;
private readonly IUserManagerRepository _userManagerRepository;

public ProfileService(IUserManager userManager)
public ProfileService(IUserManagerRepository userManagerRepository)
{
_userManager = userManager;
_userManagerRepository = userManagerRepository;
}

public async Task<Result<EditProfileInfoResponse>> EditProfileInfo(EditProfileInfoRequest infoRequest)
{
var user = await _userManager.FindByIdAsync(infoRequest.UserId);
var user = await _userManagerRepository.FindByIdAsync(infoRequest.UserId);
if (user == null)
return Result<EditProfileInfoResponse>.Fail("User not found!");


if (user.UserName != infoRequest.UserName)
{
var existingUser = await _userManager.FindByNameAsync(infoRequest.UserName);
var existingUser = await _userManagerRepository.FindByNameAsync(infoRequest.UserName);
if (existingUser != null)
return Result<EditProfileInfoResponse>.Fail("Username is already reserved by another user!");
}
Expand All @@ -35,7 +36,7 @@ public async Task<Result<EditProfileInfoResponse>> EditProfileInfo(EditProfileIn
user.FirstName = infoRequest.FirstName;
user.LastName = infoRequest.LastName;

var updateResult = await _userManager.UpdateAsync(user);
var updateResult = await _userManagerRepository.UpdateAsync(user);
if (!updateResult.Succeeded)
return Result<EditProfileInfoResponse>.Fail(updateResult.Errors.FirstMessage());

Expand All @@ -45,27 +46,27 @@ public async Task<Result<EditProfileInfoResponse>> EditProfileInfo(EditProfileIn

public async Task<Result<GetProfileInfoResponse>> GetProfileInfo(GetProfileInfoRequest getProfileInfoRequest)
{
var user = await _userManager.FindByIdAsync(getProfileInfoRequest.UserId);
var user = await _userManagerRepository.FindByIdAsync(getProfileInfoRequest.UserId);

if (user == null)
return Result<GetProfileInfoResponse>.Fail("User not found!");

var role = await _userManager.GetRoleAsync(user);
var role = await _userManagerRepository.GetRoleAsync(user);

return Result<GetProfileInfoResponse>.Ok(user.ToGetProfileInfoResponse(role));
}

public async Task<Result> ChangePassword(ChangePasswordRequest request)
{
var user = await _userManager.FindByIdAsync(request.UserId);
var user = await _userManagerRepository.FindByIdAsync(request.UserId);
if (user == null)
return Result.Fail("User not found!");

var isPasswordCorrect = await _userManager.CheckPasswordAsync(user, request.CurrentPassword);
var isPasswordCorrect = await _userManagerRepository.CheckPasswordAsync(user, request.CurrentPassword);
if (!isPasswordCorrect)
return Result.Fail("Incorrect current password!");

var passwordChangeResult = await _userManager.ChangePasswordAsync(user, request.CurrentPassword, request.NewPassword);
var passwordChangeResult = await _userManagerRepository.ChangePasswordAsync(user, request.CurrentPassword, request.NewPassword);
if (!passwordChangeResult.Succeeded)
return Result.Fail(passwordChangeResult.Errors.FirstMessage());

Expand Down
1 change: 1 addition & 0 deletions src/Application/Services/TransactionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Application.Services.SharedService;
using Domain.Entities;
using Application.DTOs.TransactionCsv;
using Application.Interfaces.Repositories;

namespace Application.Services;

Expand Down
Loading

0 comments on commit b25b194

Please sign in to comment.