public async Task <IResult <IApprenticeship> > AddApprenticeshipAsync(IApprenticeship apprenticeship) { _logger.LogMethodEnter(); Throw.IfNull(apprenticeship, nameof(apprenticeship)); try { _logger.LogInformationObject("Apprenticeship add object.", apprenticeship); _logger.LogInformationObject("Apprenticeship add URI", _addCourseUri); var courseJson = JsonConvert.SerializeObject(apprenticeship); var content = new StringContent(courseJson, Encoding.UTF8, "application/json"); var response = await _httpClient.PostAsync(_addCourseUri, content); _logger.LogHttpResponseMessage("Apprenticeship add service http response", response); if (response.IsSuccessStatusCode) { var json = await response.Content.ReadAsStringAsync(); _logger.LogInformationObject("Apprenticeship add service json response", json); var apprenticeshipResult = JsonConvert.DeserializeObject <Apprenticeship>(json); return(Result.Ok <IApprenticeship>(apprenticeshipResult)); } else if ((int)response.StatusCode == 429) { return(Result.Fail <IApprenticeship>( "Apprenticeship add service unsuccessful http response - TooManyRequests")); } else { return(Result.Fail <IApprenticeship>( "Apprenticeship add service unsuccessful http response - ResponseStatusCode: " + response.StatusCode)); } } catch (HttpRequestException hre) { _logger.LogException("Apprenticeship add service http request error", hre); return(Result.Fail <IApprenticeship>("Apprenticeship add service http request error.")); } catch (Exception e) { _logger.LogException("Apprenticeship add service unknown error.", e); return(Result.Fail <IApprenticeship>("Apprenticeship add service unknown error.")); } finally { _logger.LogMethodExit(); } }
public async Task <IApprenticeship> AddApprenticeship(IApprenticeship apprenticeship) { Throw.IfNull(apprenticeship, nameof(apprenticeship)); Apprenticeship persisted; var client = _cosmosDbHelper.GetClient(); await _cosmosDbHelper.CreateDatabaseIfNotExistsAsync(client); var doc = await _cosmosDbHelper.CreateDocumentAsync(client, _settings.ApprenticeshipCollectionId, apprenticeship); persisted = _cosmosDbHelper.DocumentTo <Apprenticeship>(doc); return(persisted); }
public async Task <IApprenticeship> Update(IApprenticeship apprenticeship) { Throw.IfNull(apprenticeship, nameof(apprenticeship)); Apprenticeship updated = null; var client = _cosmosDbHelper.GetClient(); await _cosmosDbHelper.CreateDatabaseIfNotExistsAsync(client); await _cosmosDbHelper.CreateDocumentCollectionIfNotExistsAsync(client, _settings.ApprenticeshipCollectionId); var updatedDocument = await _cosmosDbHelper.UpdateDocumentAsync(client, _settings.ApprenticeshipCollectionId, apprenticeship); updated = _cosmosDbHelper.DocumentTo <Apprenticeship>(updatedDocument); return(updated); }