Skip to content

Commit

Permalink
refactor: refactor returns
Browse files Browse the repository at this point in the history
  • Loading branch information
mobinbr committed Aug 28, 2024
1 parent 0866bed commit 83c910b
Show file tree
Hide file tree
Showing 19 changed files with 127 additions and 99 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace Application.DTOs.Profile.ChangePassword;

public class ChangePasswordRequest
{
public string UserId { get; set; } = string.Empty;
public string CurrentPassword { get; set; } = string.Empty;
public string NewPassword { get; set; } = string.Empty;
namespace Application.DTOs.Profile;

public class ChangePasswordRequest
{
public string UserId { get; set; } = string.Empty;
public string CurrentPassword { get; set; } = string.Empty;
public string NewPassword { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Application.DTOs.Identity.ChangeRole;
namespace Application.DTOs.User;

public class ChangeRoleRequest
{
Expand Down
2 changes: 1 addition & 1 deletion src/Application/DTOs/User/CreateUser/CreateUserResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ public class CreateUserResponse
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,4 +1,4 @@
namespace Application.DTOs.Identity.GetUser;
namespace Application.DTOs.User;

public class GetUserResponse
{
Expand Down
2 changes: 1 addition & 1 deletion src/Application/Mappers/UserMapper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Application.DTOs.Identity.CreateUser;
using Application.DTOs.Identity.GetUser;
using Application.DTOs.Identity.LoginUser;
using Application.DTOs.User;
using Domain.Entities;

namespace Application.Mappers;
Expand Down
12 changes: 6 additions & 6 deletions src/Web/Identity/Claims.cs → src/Web/AccessControl/Claims.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Web.Identity;

public static class Claims
{
public const string UserId = nameof(UserId);
public const string Role = nameof(Role);
namespace Web.AccessControl;

public static class Claims
{
public const string UserId = nameof(UserId);
public const string Role = nameof(Role);
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace Web.Identity;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class RequiresAnyRoleAttribute : Attribute, IAuthorizationFilter
{
private readonly string _claimName;
private readonly string[] _roles;

public RequiresAnyRoleAttribute(string claimName, params string[] roles)
{
_claimName = claimName;
_roles = roles;
}

public void OnAuthorization(AuthorizationFilterContext context)
{
var user = context.HttpContext.User;

if (user.Identity is { IsAuthenticated: false })
{
context.Result = new UnauthorizedResult();
return;
}

var hasRequiredRole = _roles.Any(role => user.HasClaim(_claimName, role));

if (!hasRequiredRole)
{
context.Result = new ForbidResult();
}
}
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace Web.AccessControl;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class RequiresAnyRoleAttribute : Attribute, IAuthorizationFilter
{
private readonly string _claimName;
private readonly string[] _roles;

public RequiresAnyRoleAttribute(string claimName, params string[] roles)
{
_claimName = claimName;
_roles = roles;
}

public void OnAuthorization(AuthorizationFilterContext context)
{
var user = context.HttpContext.User;

if (user.Identity is { IsAuthenticated: false })
{
context.Result = new UnauthorizedResult();
return;
}

var hasRequiredRole = _roles.Any(role => user.HasClaim(_claimName, role));

if (!hasRequiredRole)
{
context.Result = new ForbidResult();
}
}
}
19 changes: 12 additions & 7 deletions src/Web/Controllers/AccountsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
using Domain.Entities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Web.AccessControl;
using Web.Helper;
using Web.Identity;
using Web.Mappers;

namespace Web.Controllers;
Expand Down Expand Up @@ -40,10 +40,11 @@ public async Task<IActionResult> UploadAccounts([FromForm] IFormFile file)
var result = await _accountService.AddAccountsFromCsvAsync(filePath);
if (!result.Succeed)
{
return BadRequest(result.Message);
var errorResponse = Errors.New(nameof(UploadAccounts), result.Message);
return BadRequest(errorResponse);
}

return Ok();
return Ok("Accounts uploaded successfully!");
}

[HttpGet("{accountId}")]
Expand All @@ -54,12 +55,15 @@ public async Task<IActionResult> UploadAccounts([FromForm] IFormFile file)
public async Task<IActionResult> GetAccountById(long accountId)
{
var account = await _accountService.GetAccountByIdAsync(accountId);
if (account == null)
if (!account.Succeed)
{
return NotFound();
var errorResponse = Errors.New(nameof(GetAccountById), account.Message);
return NotFound(errorResponse);
}

return Ok(account.ToAccountDto());
var response = account.Value!;

return Ok(response.ToAccountDto());
}

[HttpGet]
Expand All @@ -72,7 +76,8 @@ public async Task<IActionResult> GetAllAccounts()
var allAccounts = await _accountService.GetAllAccountsAsync();
if (!allAccounts.Succeed)
{
return BadRequest(Errors.New(nameof(GetAllAccounts), allAccounts.Message));
var errorResponse = Errors.New(nameof(GetAllAccounts), allAccounts.Message);
return BadRequest(errorResponse);
}

var response = allAccounts.Value!;
Expand Down
11 changes: 7 additions & 4 deletions src/Web/Controllers/ProfileController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using Application.Interfaces.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Web.AccessControl;
using Web.DTOs.Profile;
using Web.Helper;
using Web.Identity;
using Web.Mappers;

namespace Web.Controllers;
Expand Down Expand Up @@ -35,7 +35,8 @@ public async Task<IActionResult> EditProfileInfo([FromBody] EditProfileInfoDto e

if (!result.Succeed)
{
return BadRequest(Errors.New(nameof(EditProfileInfo), result.Message));
var errorResponse = Errors.New(nameof(EditProfileInfo), result.Message);
return BadRequest(errorResponse);
}

return Ok("Profile info updated successfully!");
Expand All @@ -55,7 +56,8 @@ public async Task<IActionResult> GetProfileInfo()

if (!result.Succeed)
{
return NotFound(Errors.New(nameof(GetProfileInfo), "User not found!"));
var errorResponse = Errors.New(nameof(GetProfileInfo), result.Message);
return NotFound(errorResponse);
}

var user = result.Value!;
Expand All @@ -77,7 +79,8 @@ public async Task<IActionResult> ChangePassword([FromBody] ChangePasswordDto cha

if (!result.Succeed)
{
return BadRequest(Errors.New(nameof(ChangePassword), result.Message));
var errorResponse = Errors.New(nameof(ChangePassword), result.Message);
return BadRequest(errorResponse);
}

return Ok("Password changed successfully!");
Expand Down
13 changes: 8 additions & 5 deletions src/Web/Controllers/TransactionsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using Domain.Constants;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Web.AccessControl;
using Web.Helper;
using Web.Identity;
using Web.Mappers;

namespace Web.Controllers;
Expand Down Expand Up @@ -42,10 +42,11 @@ public async Task<IActionResult> UploadTransactions([FromForm] IFormFile file)

if (!result.Succeed)
{
return BadRequest(result.Message);
var errorResponse = Errors.New(nameof(UploadTransactions), result.Message);
return BadRequest(errorResponse);
}

return Ok();
return Ok("Transactions uploaded successfully!");
}

[HttpGet()]
Expand All @@ -60,7 +61,8 @@ public async Task<IActionResult> GetAllTransactions()
var allTransactions = await _transactionService.GetAllTransactionsAsync();
if (!allTransactions.Succeed)
{
return BadRequest(Errors.New(nameof(GetAllTransactions), allTransactions.Message));
var errorResponse = Errors.New(nameof(GetAllTransactions), allTransactions.Message);
return BadRequest(errorResponse);
}

var response = allTransactions.Value!;
Expand All @@ -79,7 +81,8 @@ public async Task<IActionResult> GetTransactionsByAccountId(long accountId)

if (!transactions.Succeed)
{
return BadRequest(Errors.New(nameof(GetAllTransactions), transactions.Message));
var errorResponse = Errors.New(nameof(GetAllTransactions), transactions.Message);
return BadRequest(errorResponse);
}

var response = transactions.Value!;
Expand Down
34 changes: 24 additions & 10 deletions src/Web/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
using Domain.Constants;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Web.DTOs.Identity;
using Web.AccessControl;
using Web.DTOs.User;
using Web.DTOs.User.Login;
using Web.DTOs.User.Signup;
using Web.Helper;
using Web.Identity;
using Web.Mappers;

namespace Web.Controllers;

[ApiController]
[Route("identity")]
[Route("users")]
public class UsersController : ControllerBase
{
private readonly IUserService _userService;
Expand All @@ -28,11 +30,13 @@ public UsersController(IUserService userService)
[ProducesResponseType(403)]
public async Task<IActionResult> Signup([FromBody] SignupDto signupDto)
{
var result = await _userService.SignUpUser(signupDto.ToCreateUserRequest());
var result = await _userService.SignUp(signupDto.ToCreateUserRequest());

if (!result.Succeed)
{
return BadRequest(Errors.New(nameof(Signup), result.Message));
var errorResponse = Errors.New(nameof(Signup), result.Message);
return BadRequest(errorResponse);
// return StatusCode(500, Errors.New("Server Error", $"An unexpected error occurred: {ex.Message}"));
}

var response = result.Value!;
Expand All @@ -49,7 +53,8 @@ public async Task<IActionResult> Login([FromBody] LoginDto loginDto)

if (!result.Succeed)
{
return Unauthorized(Errors.New(nameof(Login), result.Message));
var errorResponse = Errors.New(nameof(Login), result.Message);
return Unauthorized(errorResponse);
}

var response = result.Value!;
Expand All @@ -70,7 +75,8 @@ public async Task<IActionResult> ChangeRole([FromBody] ChangeRoleDto changeRoleD

if (!result.Succeed)
{
return BadRequest(Errors.New(nameof(ChangeRole), result.Message));
var errorResponse = Errors.New(nameof(ChangeRole), result.Message);
return BadRequest(errorResponse);
}

return Ok("Role changed successfully!");
Expand All @@ -82,10 +88,18 @@ public async Task<IActionResult> ChangeRole([FromBody] ChangeRoleDto changeRoleD
[ProducesResponseType(200)]
[ProducesResponseType(401)]
[ProducesResponseType(403)]
public async Task<IActionResult> GetUsersAsync()
public async Task<IActionResult> GetAllUsers()
{
var appUsersWithRoles = await _userService.GetUsersAsync();
var usersWithRolesResult = await _userService.GetAllUsersAsync();

return Ok(appUsersWithRoles);
if (!usersWithRolesResult.Succeed)
{
var errorResponse = Errors.New(nameof(ChangeRole), usersWithRolesResult.Message);
return BadRequest(errorResponse);
}

var response = usersWithRolesResult.Value!;

return Ok(response);
}
}
2 changes: 1 addition & 1 deletion src/Web/DTOs/User/ChangeRoleDto.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.ComponentModel.DataAnnotations;

namespace Web.DTOs.Identity;
namespace Web.DTOs.User;

public class ChangeRoleDto
{
Expand Down
3 changes: 1 addition & 2 deletions src/Web/DTOs/User/Login/LoginDto.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.ComponentModel.DataAnnotations;
using System.Runtime.InteropServices.JavaScript;

namespace Web.DTOs.Identity;
namespace Web.DTOs.User.Login;

public class LoginDto
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Web.DTOs.Identity;
namespace Web.DTOs.User.Login;

public class UserLoggedInDto
public class LoginResponseDto
{
public string FirstName { get; set; } = String.Empty;
public string LastName { get; set; } = String.Empty;
Expand Down
2 changes: 1 addition & 1 deletion src/Web/DTOs/User/Signup/SignupDto.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.ComponentModel.DataAnnotations;

namespace Web.DTOs.Identity;
namespace Web.DTOs.User.Signup;

public class SignupDto
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Web.DTOs.Identity;
namespace Web.DTOs.User.Signup;

public class UserSignedUpDto
public class SignupResponseDto
{
public string FirstName { get; set; } = String.Empty;
public string LastName { get; set; } = String.Empty;
Expand Down
2 changes: 1 addition & 1 deletion src/Web/Mappers/ProfileMapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Application.DTOs.Profile.ChangePassword;
using Application.DTOs.Profile;
using Application.DTOs.Profile.EditProfile;
using Application.DTOs.Profile.GetProfileInfo;
using Web.DTOs.Profile;
Expand Down
Loading

0 comments on commit 83c910b

Please sign in to comment.