Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/expansion #23

Merged
merged 2 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Application.DTOs.Transaction;

public class GetTransactionsByAccountIdResponse
{
public long AccountId { get; set; }
public List<TransactionCsvModel> TransactionWithSources = new();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Application.DTOs.Identity.ChangeRole;
public class ChangeRoleRequest
{
public string UserName { get; set; } = string.Empty;
public string Role { get; set; } = string.Empty;
namespace Application.DTOs.Identity.ChangeRole;

public class ChangeRoleRequest
{
public string UserName { get; set; } = string.Empty;
public string Role { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
namespace Application.DTOs.Identity.CreateUser;
public class CreateUserRequest
{
public string FirstName { get; set; } = String.Empty;
public string LastName { get; set; } = String.Empty;
public string Email { get; set; } = String.Empty;
public string UserName { get; set; } = String.Empty;
public string Password { get; set; } = String.Empty;
public string Role { get; set; } = String.Empty;
namespace Application.DTOs.Identity.CreateUser;

public class CreateUserRequest
{
public string FirstName { get; set; } = String.Empty;
public string LastName { get; set; } = String.Empty;
public string Email { get; set; } = String.Empty;
public string UserName { get; set; } = String.Empty;
public string Password { get; set; } = String.Empty;
public string Role { get; set; } = String.Empty;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
namespace Application.DTOs.Identity.CreateUser;
public class CreateUserResponse
{
public string FirstName { get; set; } = String.Empty;
public string LastName { get; set; } = String.Empty;
public string Email { get; set; } = String.Empty;
public string UserName { get; set; } = String.Empty;
public string Role { get; set; } = String.Empty;
namespace Application.DTOs.Identity.CreateUser;

public class CreateUserResponse
{
public string FirstName { get; set; } = String.Empty;
public string LastName { get; set; } = String.Empty;
public string Email { get; set; } = String.Empty;
public string UserName { get; set; } = String.Empty;
public string Role { get; set; } = String.Empty;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace Application.DTOs.Identity.LoginUser;
public class LoginUserRequest
{
public string? UserName { get; set; }
public string? Email { get; set; }
public string Password { get; set; } = String.Empty;
namespace Application.DTOs.Identity.LoginUser;

public class LoginUserRequest
{
public string? UserName { get; set; }
public string? Email { get; set; }
public string Password { get; set; } = String.Empty;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
namespace Application.DTOs.Identity.LoginUser;
public class LoginUserResponse
{
public string FirstName { get; set; } = String.Empty;
public string LastName { get; set; } = String.Empty;
public string Email { get; set; } = String.Empty;
public string UserName { get; set; } = String.Empty;
public string Role { get; set; } = String.Empty;
public string Token { get; set; } = String.Empty;
namespace Application.DTOs.Identity.LoginUser;

public class LoginUserResponse
{
public string FirstName { get; set; } = String.Empty;
public string LastName { get; set; } = String.Empty;
public string Email { get; set; } = String.Empty;
public string UserName { get; set; } = String.Empty;
public string Role { get; set; } = String.Empty;
public string Token { get; set; } = String.Empty;
}
2 changes: 0 additions & 2 deletions src/Application/Interfaces/ITokenService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@ namespace Application.Interfaces;
public interface ITokenService
{
string GenerateToken(AppUser user, string role);
Task<bool> IsTokenInvalidatedAsync(string token);
Task AddInvalidatedTokenAsync(string token);
}
3 changes: 2 additions & 1 deletion src/Application/Interfaces/Services/ITransactionService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Application.DTOs;
using Application.DTOs.Transaction;
using Domain.Entities;

namespace Application.Interfaces.Services;
Expand All @@ -7,5 +8,5 @@ public interface ITransactionService
{
Task<Result> AddTransactionsFromCsvAsync(string filePath);
Task<Result<List<Transaction>>> GetAllTransactionsAsync();
Task<List<Transaction>> GetTransactionsByAccountIdAsync(long accountId);
Task<Result<List<GetTransactionsByAccountIdResponse>>> GetTransactionsByAccountIdAsync(long accountId);
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using Application.DTOs;
using Application.DTOs.Identity.ChangeRole;
using Application.DTOs.Identity.CreateUser;
using Application.DTOs.Identity.GetUser;
using Application.DTOs.Identity.LoginUser;

namespace Application.Interfaces.Services;

public interface IIdentityService
{
Task<Result<CreateUserResponse>> SignUpUser(CreateUserRequest createIdentityDto);
Task<Result<LoginUserResponse>> Login(LoginUserRequest loginDto);
Task<Result> ChangeRole(ChangeRoleRequest changeRoleRequest);
Task<List<GetUserResponse>> GetUsersAsync();
Task<Result> Logout(string token);
using Application.DTOs;
using Application.DTOs.Identity.ChangeRole;
using Application.DTOs.Identity.CreateUser;
using Application.DTOs.Identity.GetUser;
using Application.DTOs.Identity.LoginUser;

namespace Application.Interfaces.Services;

public interface IUserService
{
Task<Result<CreateUserResponse>> SignUpUser(CreateUserRequest createIdentityDto);
Task<Result<LoginUserResponse>> Login(LoginUserRequest loginDto);
Task<Result> ChangeRole(ChangeRoleRequest changeRoleRequest);
Task<List<GetUserResponse>> GetUsersAsync();
}
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
using Application.DTOs.Identity.CreateUser;
using Application.DTOs.Identity.GetUser;
using Application.DTOs.Identity.LoginUser;
using Domain.Entities;
namespace Application.Mappers;
public static class IdentityMapper
{
public static AppUser ToAppUser(this CreateUserRequest createUserRequest)
{
return new AppUser
{
FirstName = createUserRequest.FirstName,
LastName = createUserRequest.LastName,
Email = createUserRequest.Email,
UserName = createUserRequest.UserName
};
}
public static CreateUserResponse ToCreateUserResponse(this AppUser appUser, string role)
{
return new CreateUserResponse
{
FirstName = appUser.FirstName,
LastName = appUser.LastName,
Email = appUser.Email,
UserName = appUser.UserName,
Role = role
};
}
public static LoginUserResponse ToLoginUserResponse(this AppUser appUser, string role, string token)
{
return new LoginUserResponse
{
FirstName = appUser.FirstName,
LastName = appUser.LastName,
Email = appUser.Email,
UserName = appUser.UserName,
Role = role,
Token = token
};
}
public static GetUserResponse ToGetUserResponse(this AppUser appUser, string role)
{
return new GetUserResponse
{
FirstName = appUser.FirstName,
LastName = appUser.LastName,
Email = appUser.Email,
UserName = appUser.UserName,
Role = role
};
}
using Application.DTOs.Identity.CreateUser;
using Application.DTOs.Identity.GetUser;
using Application.DTOs.Identity.LoginUser;
using Domain.Entities;

namespace Application.Mappers;

public static class UserMapper
{
public static AppUser ToAppUser(this CreateUserRequest createUserRequest)
{
return new AppUser
{
FirstName = createUserRequest.FirstName,
LastName = createUserRequest.LastName,
Email = createUserRequest.Email,
UserName = createUserRequest.UserName
};
}

public static CreateUserResponse ToCreateUserResponse(this AppUser appUser, string role)
{
return new CreateUserResponse
{
FirstName = appUser.FirstName,
LastName = appUser.LastName,
Email = appUser.Email,

Check warning on line 27 in src/Application/Mappers/UserMapper.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Possible null reference assignment.
UserName = appUser.UserName,

Check warning on line 28 in src/Application/Mappers/UserMapper.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Possible null reference assignment.
Role = role
};
}

public static LoginUserResponse ToLoginUserResponse(this AppUser appUser, string role, string token)
{
return new LoginUserResponse
{
FirstName = appUser.FirstName,
LastName = appUser.LastName,
Email = appUser.Email,

Check warning on line 39 in src/Application/Mappers/UserMapper.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Possible null reference assignment.
UserName = appUser.UserName,

Check warning on line 40 in src/Application/Mappers/UserMapper.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Possible null reference assignment.
Role = role,
Token = token
};
}

public static GetUserResponse ToGetUserResponse(this AppUser appUser, string role)
{
return new GetUserResponse
{
FirstName = appUser.FirstName,
LastName = appUser.LastName,
Email = appUser.Email,

Check warning on line 52 in src/Application/Mappers/UserMapper.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Possible null reference assignment.
UserName = appUser.UserName,

Check warning on line 53 in src/Application/Mappers/UserMapper.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Possible null reference assignment.
Role = role
};
}
}
44 changes: 40 additions & 4 deletions src/Application/Services/DomainService/TransactionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,46 @@ public async Task<Result<List<Transaction>>> GetAllTransactionsAsync()
}
}

public async Task<List<Transaction>> GetTransactionsByAccountIdAsync(long accountId)
public async Task<Result<List<GetTransactionsByAccountIdResponse>>> GetTransactionsByAccountIdAsync(long accountId)
{
var source = await _transactionRepository.GetBySourceAccountId(accountId);
var destination = await _transactionRepository.GetByDestinationAccountId(accountId);
return source.Concat(destination).ToList();
try
{
var result = new List<GetTransactionsByAccountIdResponse>();

var transactionsSourceAccountId = await _transactionRepository.GetBySourceAccountId(accountId);

var transactionsDestinationAccountId = await _transactionRepository.GetByDestinationAccountId(accountId);

var allTransactions = transactionsSourceAccountId.Concat(transactionsDestinationAccountId).ToList();

var groupedTransactions = allTransactions
.GroupBy(t => t.SourceAccountId == accountId ? t.DestinationAccountId : t.SourceAccountId)
.ToList();

foreach (var group in groupedTransactions)
{
var response = new GetTransactionsByAccountIdResponse
{
AccountId = group.Key,
TransactionWithSources = group.Select(t => new TransactionCsvModel
{
TransactionID = t.TransactionId,
SourceAcount = t.SourceAccountId,
DestiantionAccount = t.DestinationAccountId,
Amount = t.Amount,
Date = t.Date,
Type = t.Type,
}).ToList()
};

result.Add(response);
}

return Result<List<GetTransactionsByAccountIdResponse>>.Ok(result);
}
catch (Exception ex)
{
return Result<List<GetTransactionsByAccountIdResponse>>.Fail($"An error occurred: {ex.Message}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@

namespace Application.Services.DomainService;

public class IdentityService : IIdentityService
public class UserService : IUserService
{
private readonly IUserManagerRepository _userManagerRepository;
private readonly IRoleManagerRepository _roleManagerRepository;
private readonly ITokenService _tokenService;
public IdentityService(IUserManagerRepository userManagerRepository,
public UserService(IUserManagerRepository userManagerRepository,
IRoleManagerRepository roleManagerRepository,
ITokenService tokenService)
{
Expand Down Expand Up @@ -108,10 +108,4 @@ public async Task<List<GetUserResponse>> GetUsersAsync()

return userWithRoles;
}

public async Task<Result> Logout(string token)
{
await _tokenService.AddInvalidatedTokenAsync(token);
return Result.Ok();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@
namespace Web.Controllers;

[ApiController]
[Route("[controller]/[action]")]
public class AccountController : ControllerBase
[Route("accounts")]
public class AccountsController : ControllerBase
{
private readonly IAccountService _accountService;

public AccountController(IAccountService accountService)
public AccountsController(IAccountService accountService)
{
_accountService = accountService;
}

[HttpPost]
[HttpPost("upload")]
[Authorize]
[RequiresAnyRole(Claims.Role, AppRoles.Admin, AppRoles.DataAdmin)]
[ProducesResponseType(200)]
[ProducesResponseType(400)]
public async Task<IActionResult> ImportAccounts([FromForm] IFormFile file)
public async Task<IActionResult> UploadAccounts([FromForm] IFormFile file)
{
if (file.Length == 0)
return BadRequest("No file uploaded.");
Expand Down
Loading
Loading