44using Microsoft . AspNetCore . Authorization ;
55using WebApi . Entities ;
66using MediatR ;
7+ using System . Security . Claims ;
8+ using Microsoft . AspNetCore . Http ;
9+ using WebApi . Features . Employees ;
10+ using WebApi . Utils ;
711
812namespace WebApi . Features . Accounts
913{
@@ -12,24 +16,37 @@ namespace WebApi.Features.Accounts
1216 public class AccountsController : ControllerBase
1317 {
1418 private readonly IMediator _mediator ;
19+ private readonly IHttpContextAccessor _httpContext ;
1520
16- public AccountsController ( IMediator mediator )
21+ public AccountsController ( IMediator mediator , IHttpContextAccessor httpContext )
1722 {
1823 _mediator = mediator ;
24+ _httpContext = httpContext ;
1925 }
2026
2127 // POST: api/accounts/register
28+ /// <summary>
29+ /// Register new employee
30+ /// </summary>
31+ /// <remarks>
32+ /// Unique card no. and username filter will be applied
33+ /// </remarks>
34+ /// <param name="viewModel"></param>
2235 [ Authorize ( Roles = "Admin" ) ]
2336 [ HttpPost ( "register" ) ]
37+ [ ProducesResponseType ( typeof ( EmployeeViewModel ) , StatusCodes . Status201Created ) ]
38+ [ ProducesResponseType ( typeof ( ErrorHandler ) , StatusCodes . Status400BadRequest ) ]
2439 public async Task < IActionResult > Register ( RegisterViewModel viewModel )
2540 {
2641 // mediator from Features/Employees
2742 var isCardExist = await _mediator . Send ( new Employees . IsCardExists . Query ( Guid . Empty , viewModel . CardNo ) ) ;
28- if ( isCardExist ) return BadRequest ( "Card No. is already in use" ) ;
43+ if ( isCardExist )
44+ return BadRequest ( new ErrorHandler { Description = "Card No. is already in use" } ) ;
2945
3046 // mediator from Features/Employees
3147 var isUsernameExist = await _mediator . Send ( new Auth . IsUserExists . Query ( viewModel . UserName ) ) ;
32- if ( isUsernameExist ) return BadRequest ( $ "Username { viewModel . UserName } is already taken") ;
48+ if ( isUsernameExist )
49+ return BadRequest ( new ErrorHandler { Description = $ "Username { viewModel . UserName } is already taken" } ) ;
3350
3451 // Create user account
3552 var employeeInfo = await _mediator . Send ( new Register . Command ( viewModel ) ) ;
@@ -38,28 +55,44 @@ public async Task<IActionResult> Register(RegisterViewModel viewModel)
3855 }
3956
4057 // PUT: api/accounts/update-password
58+ /// <summary>
59+ /// Update an Employee password
60+ /// </summary>
61+ /// <param name="viewModel"></param>
4162 [ Authorize ( Roles = "Admin" ) ]
4263 [ HttpPut ( "update-password" ) ]
43- public async Task < IActionResult > UpdatePassword ( ChangePasswordViewModel viewModel )
64+ [ ProducesResponseType ( StatusCodes . Status200OK ) ]
65+ [ ProducesResponseType ( typeof ( ErrorHandler ) , StatusCodes . Status400BadRequest ) ]
66+ public async Task < IActionResult > UpdatePassword ( UpdatePasswordViewModel viewModel )
4467 {
4568 // Change a specific Employee account's password
4669 var result = await _mediator . Send ( new UpdatePassword . Command ( viewModel ) ) ;
47- if ( ! result ) return StatusCode ( 500 ) ;
70+ if ( ! result )
71+ return BadRequest ( new ErrorHandler { Description = "Unable to update password." } ) ;
4872
4973 return Ok ( ) ;
5074 }
5175
5276 // PUT: api/accounts/change-password
77+ /// <summary>
78+ /// Update your current password
79+ /// </summary>
80+ /// <param name="viewModel"></param>
5381 [ HttpPut ( "change-password" ) ]
82+ [ ProducesResponseType ( StatusCodes . Status200OK ) ]
83+ [ ProducesResponseType ( typeof ( ErrorHandler ) , StatusCodes . Status400BadRequest ) ]
5484 public async Task < IActionResult > ChangePassword ( ChangePasswordViewModel viewModel )
5585 {
5686 // Check if Old password is correct
57- var validatePassword = await _mediator . Send ( new Auth . ValidatePassword . Query ( viewModel . UserName , viewModel . OldPassword ) ) ;
58- if ( ! validatePassword ) return BadRequest ( "Incorrect password" ) ;
87+ var currentUser = _httpContext . HttpContext . User . FindFirstValue ( ClaimTypes . NameIdentifier ) ;
88+ var validatePassword = await _mediator . Send ( new Auth . ValidatePassword . Query ( currentUser , viewModel . OldPassword ) ) ;
89+ if ( ! validatePassword )
90+ return BadRequest ( new ErrorHandler { Description = "Incorrect password." } ) ;
5991
6092 // Change account password
6193 var result = await _mediator . Send ( new ChangePassword . Command ( viewModel ) ) ;
62- if ( ! result . Succeeded ) return BadRequest ( "Unable to change password" ) ;
94+ if ( ! result . Succeeded )
95+ return BadRequest ( new ErrorHandler { Description = "Unable to change password." } ) ;
6396
6497 return Ok ( ) ;
6598 }
0 commit comments