Last active
November 11, 2019 00:07
-
-
Save mike1477/c354c9de53034982bb293e9f86b9470f to your computer and use it in GitHub Desktop.
Video 48 |Create update API and upload profile image to azure storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PUT api/employers/update/5 | |
[HttpPut("update/{employerid}")] | |
[Authorize(Policy = "EmployerPolicy")] | |
public async Task<IActionResult> Put(string employerid, [FromForm] UpdateEmployerViewModel model) | |
{ | |
if (employerid != User.FindFirst(ClaimTypes.NameIdentifier).Value) | |
{ | |
return Unauthorized(); | |
} | |
var employerFromDb = await _userManager.FindByIdAsync(employerid); | |
//edit profile | |
if (model.ProfileImage != null) | |
{ | |
if (employerFromDb.ProfileImageUrl != null) | |
{ | |
await _cloudStorage.DeleteImage(employerFromDb.ProfileImageUrl); | |
} | |
var addedImageUrl = await _cloudStorage.UploadAsync(model.ProfileImage); | |
employerFromDb.ProfileImageUrl = addedImageUrl; | |
} | |
var result = await _userManager.UpdateAsync(employerFromDb); | |
var updatedEmployer = await _userManager.FindByIdAsync(employerid); | |
if (result.Succeeded) | |
{ | |
return Ok(new | |
{ | |
result = result, | |
updatedEmployer | |
}); | |
} | |
return BadRequest(result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment