66using MediatR ;
77using System . Security . Claims ;
88using Microsoft . AspNetCore . Http ;
9+ using WebApi . Features . Employees ;
10+ using WebApi . Utils ;
911
1012namespace WebApi . Features . Accounts
1113{
@@ -23,19 +25,28 @@ public AccountsController(IMediator mediator, IHttpContextAccessor httpContext)
2325 }
2426
2527 // 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>
2635 [ Authorize ( Roles = "Admin" ) ]
2736 [ HttpPost ( "register" ) ]
37+ [ ProducesResponseType ( typeof ( EmployeeViewModel ) , StatusCodes . Status201Created ) ]
38+ [ ProducesResponseType ( typeof ( ErrorHandler ) , StatusCodes . Status400BadRequest ) ]
2839 public async Task < IActionResult > Register ( RegisterViewModel viewModel )
2940 {
3041 // mediator from Features/Employees
3142 var isCardExist = await _mediator . Send ( new Employees . IsCardExists . Query ( Guid . Empty , viewModel . CardNo ) ) ;
3243 if ( isCardExist )
33- return BadRequest ( "Card No. is already in use" ) ;
44+ return BadRequest ( new ErrorHandler { Description = "Card No. is already in use" } ) ;
3445
3546 // mediator from Features/Employees
3647 var isUsernameExist = await _mediator . Send ( new Auth . IsUserExists . Query ( viewModel . UserName ) ) ;
3748 if ( isUsernameExist )
38- return BadRequest ( $ "Username { viewModel . UserName } is already taken") ;
49+ return BadRequest ( new ErrorHandler { Description = $ "Username { viewModel . UserName } is already taken" } ) ;
3950
4051 // Create user account
4152 var employeeInfo = await _mediator . Send ( new Register . Command ( viewModel ) ) ;
@@ -44,32 +55,44 @@ public async Task<IActionResult> Register(RegisterViewModel viewModel)
4455 }
4556
4657 // PUT: api/accounts/update-password
58+ /// <summary>
59+ /// Update an Employee password
60+ /// </summary>
61+ /// <param name="viewModel"></param>
4762 [ Authorize ( Roles = "Admin" ) ]
4863 [ HttpPut ( "update-password" ) ]
64+ [ ProducesResponseType ( StatusCodes . Status200OK ) ]
65+ [ ProducesResponseType ( typeof ( ErrorHandler ) , StatusCodes . Status400BadRequest ) ]
4966 public async Task < IActionResult > UpdatePassword ( UpdatePasswordViewModel viewModel )
5067 {
5168 // Change a specific Employee account's password
5269 var result = await _mediator . Send ( new UpdatePassword . Command ( viewModel ) ) ;
5370 if ( ! result )
54- return BadRequest ( ) ;
71+ return BadRequest ( new ErrorHandler { Description = "Unable to update password." } ) ;
5572
5673 return Ok ( ) ;
5774 }
5875
5976 // PUT: api/accounts/change-password
77+ /// <summary>
78+ /// Update your current password
79+ /// </summary>
80+ /// <param name="viewModel"></param>
6081 [ HttpPut ( "change-password" ) ]
82+ [ ProducesResponseType ( StatusCodes . Status200OK ) ]
83+ [ ProducesResponseType ( typeof ( ErrorHandler ) , StatusCodes . Status400BadRequest ) ]
6184 public async Task < IActionResult > ChangePassword ( ChangePasswordViewModel viewModel )
6285 {
6386 // Check if Old password is correct
6487 var currentUser = _httpContext . HttpContext . User . FindFirstValue ( ClaimTypes . NameIdentifier ) ;
6588 var validatePassword = await _mediator . Send ( new Auth . ValidatePassword . Query ( currentUser , viewModel . OldPassword ) ) ;
6689 if ( ! validatePassword )
67- return BadRequest ( "Incorrect password" ) ;
90+ return BadRequest ( new ErrorHandler { Description = "Incorrect password." } ) ;
6891
6992 // Change account password
7093 var result = await _mediator . Send ( new ChangePassword . Command ( viewModel ) ) ;
7194 if ( ! result . Succeeded )
72- return BadRequest ( "Unable to change password" ) ;
95+ return BadRequest ( new ErrorHandler { Description = "Unable to change password." } ) ;
7396
7497 return Ok ( ) ;
7598 }
0 commit comments