Academatica.Api/Academatica.Api.Users/Controllers/UsersController.cs
/// <summary>
/// Endpoint used to change the user's first name.
/// </summary>
/// <param name="id">User ID.</param>
/// <param name="changeFirstNameRequestDTO">Body - new first name.</param>
Endpoint used to change the user's first name.
Endpoint used to change the user ' s first name .
[HttpPatch]
        [Route("{id}/firstname")]
        public async Task<IActionResult> SetUserFirstName(Guid id, [FromBody] UserChangeFirstNameRequestDto changeFirstNameRequestDTO)
        {
            if (ModelState.IsValid)
            {
                var userId = User.FindFirst("sub")?.Value;
                if (userId != id.ToString())
                {
                    return Forbid();
                }
                var user = _academaticaDbContext.Users.Where(x => x.Id == id).FirstOrDefault();
                if (user == null)
                {
                    return NotFound("Invalid user ID.");
                }
                var firstName = changeFirstNameRequestDTO.FirstName;
                user.FirstName = firstName;
                await _academaticaDbContext.SaveChangesAsync();
                return Ok();
            }
            else
            {
                var message = string.Join(" | ", ModelState.Values
                                    .SelectMany(v => v.Errors)
                                    .Select(e => e.ErrorMessage));
                return BadRequest(message);
            }
        }
[ HttpPatch ] [ Route ( " " ) ] public async Task < IActionResult > SetUserFirstName ( Guid id , [ FromBody ] UserChangeFirstNameRequestDto changeFirstNameRequestDTO ) { if ( ModelState . IsValid ) { var userId = User . FindFirst ( " " ) ? . Value ; if ( userId != id . ToString ( ) ) { return Forbid ( ) ; } var user = _academaticaDbContext . Users . Where ( x => x . Id == id ) . FirstOrDefault ( ) ; if ( user == null ) { return NotFound ( " " ) ; } var firstName = changeFirstNameRequestDTO . FirstName ; user . FirstName = firstName ; await _academaticaDbContext . SaveChangesAsync ( ) ; return Ok ( ) ; } else { var message = string . Join ( " " , ModelState . Values . SelectMany ( v => v . Errors ) . Select ( e => e . ErrorMessage ) ) ; return BadRequest ( message ) ; } }
Endpoint used to change the user's first name.
Endpoint used to change the user ' s first name .
id
Guid
None
UserChangeFirstNameRequestDto
[FromBody]
changeFirstNameRequestDTO
new first name.
new first name .