public async Task UpdateProfile(string username, Profile profile) { var body = new UpdateProfileRequestBody { Firstname = profile.Firstname, Lastname = profile.Lastname }; string json = JsonConvert.SerializeObject(body); HttpResponseMessage responseMessage = await _httpClient.PutAsync($"api/profile/{username}", new StringContent(json, Encoding.UTF8, "application/json")); await EnsureSuccessOrThrowProfileException(responseMessage); }
public async Task <IActionResult> Put(string username, [FromBody] UpdateProfileRequestBody updateProfileRequestBody) { using (_logger.BeginScope("{Username}", username)) { try { var profile = new Profile { Username = username, Firstname = updateProfileRequestBody.Firstname, Lastname = updateProfileRequestBody.Lastname, ProfilePictureId = updateProfileRequestBody.ProfilePictureId }; if (!ValidateProfile(profile, out string error)) { _logger.LogWarning(error); return(BadRequest(error)); } var stopWatch = Stopwatch.StartNew(); await _profileStore.UpdateProfile(profile); _telemetryClient.TrackMetric("ProfileStore.UpdateProfile.Time", stopWatch.ElapsedMilliseconds); _telemetryClient.TrackEvent("ProfileUpdated"); return(Ok(profile)); } catch (ProfileNotFoundException e) { _logger.LogError(e, $"Profile {username} does not exists in storage"); return(NotFound($"The profile with username {username} was not found")); } catch (StorageErrorException e) { _logger.LogError(e, $"Failed to update profile {username} in storage"); return(StatusCode(503, "The service is unavailable, please retry in few minutes")); } catch (StorageConflictException e) { _logger.LogError(e, $"Failed to update profile {username} in storage"); return(StatusCode(503, "The service is unavailable, please retry in few minutes")); } catch (Exception e) { _logger.LogError(e, $"Unknown exception occured while updating profile {username} in storage"); return(StatusCode(500, "An internal server error occured, please reachout to support if this error persists")); } } }
public async Task UpdateProfileReturns500WhenExceptionIsNotKnown() { UpdateProfileRequestBody passedProfile = new UpdateProfileRequestBody(); passedProfile.Firstname = _testProfile.Firstname; passedProfile.Lastname = _testProfile.Lastname; var profilesStoreMock = new Mock <IProfileStore>(); profilesStoreMock.Setup(store => store.UpdateProfile(_testProfile)).ThrowsAsync(new Exception("Test Exception")); var loggerStub = new ProfilesControllerLoggerStub(); var controller = new ProfileController(profilesStoreMock.Object, loggerStub, new TelemetryClient()); IActionResult result = await controller.Put(_testProfile.Username, passedProfile); AssertUtils.HasStatusCode(HttpStatusCode.InternalServerError, result); Assert.Contains(LogLevel.Error, loggerStub.LogEntries.Select(entry => entry.Level)); }
public async Task UpdateProfile(string username, UpdateProfileRequestBody updateProfileRequestBody) { using (_logger.BeginScope("{Username}", username)) { var userProfile = new UserProfile { Username = username, FirstName = updateProfileRequestBody.FirstName, LastName = updateProfileRequestBody.LastName, ProfilePictureId = updateProfileRequestBody.ProfilePictureId }; if (!ValidateProfile(userProfile, out string error)) { throw new BadRequestException(error); } var stopWatch = Stopwatch.StartNew(); await _profileStore.UpdateProfile(userProfile); _telemetryClient.TrackMetric("ProfileStore.UpdateProfile.Time", stopWatch.ElapsedMilliseconds); _telemetryClient.TrackEvent("ProfileUpdated"); } }
public async Task <IActionResult> Put(string username, [FromBody] UpdateProfileRequestBody updateProfileRequestBody) { await _profileService.UpdateProfile(username, updateProfileRequestBody); return(Ok()); }