public async Task <User> Update(User entity) { if (string.IsNullOrEmpty(entity.Password)) { var current = await Fetch(entity.Id); entity.Password = current.Password; } CosmosResponse <User> response = await store.UpdateAsync(entity); if (!response.IsSuccess && response.CosmosOperationStatus == CosmosOperationStatus.ResourceNotFound) { throw new NotFoundException(ErrorCode.NotFoundErrorGeneric, "Resource not found"); } return(response); }
public void Create_EbatchSheet_Successfully() { var createRequest = new CreateEbatchSheetCommand() { WorkOrderNumber = "WON_2020" }; var addResponse = new CosmosResponse <EbatchSheetEntity.EbatchSheet>(new EbatchSheetEntity.EbatchSheet(), new ResourceResponse <Document>()); _cosmosStore.Setup(f => f.AddAsync(It.IsAny <EbatchSheetEntity.EbatchSheet>(), null)).ReturnsAsync(addResponse); var createHandler = new CreateEbatchSheetCommandHandler(_cosmosStore.Object, _httpContext.Object, _createLogger.Object, _mailSender.Object); var result = createHandler.Handle(createRequest, CancellationToken.None); _cosmosStore.Verify(v => v.AddAsync(It.IsAny <EbatchSheetEntity.EbatchSheet>(), null)); }
/// <summary> /// Executes a SQL Query against the collection /// </summary> /// <param name="query">Query to execute</param> /// <param name="pagedResults">true to return only one page of the result set, false(Default) to return all results. </param> /// <param name="continuationToken">token to pass into the query iterator to resume from a specific page. Should be present when using pageResults = true</param> /// <param name="cancellationToken">cancellatinToken used to cancel an operation in progress.</param> /// <returns>Collection of results.</returns> public async Task <CosmosResponse <IEnumerable <T> > > ExecuteSQL <T>(string query, bool pagedResults = false, string continuationToken = null, CancellationToken cancellationToken = default(CancellationToken)) { DateTime start = DateTime.Now; CosmosResponse <IEnumerable <T> > cosmosResult = new CosmosResponse <IEnumerable <T> >(); var dataResult = new List <T>(); try { var iterator = Container.GetItemQueryIterator <T>(query, continuationToken); while (iterator.HasMoreResults) { var readNext = await iterator.ReadNextAsync(cancellationToken); dataResult.AddRange(readNext.Resource); cosmosResult.RequestCharge += readNext.RequestCharge; cosmosResult.ContinuationToken = readNext.ContinuationToken; cosmosResult.ActivityId = readNext.ActivityId; cosmosResult.ETag = readNext.ETag; if (pagedResults) //Just read the next page of the result set if we are paging results { break; } } cosmosResult.Result = dataResult; cosmosResult.ExecutionTime = DateTime.Now.Subtract(start); cosmosResult.StatusCode = System.Net.HttpStatusCode.OK; return(cosmosResult); } catch (CosmosException cex) { return(cex.ToCosmosResponse <IEnumerable <T> >()); } catch (Exception e) { return(new CosmosResponse <IEnumerable <T> > { Error = e, StatusCode = System.Net.HttpStatusCode.InternalServerError }); } }
public void Update_Any_State_By_User_Failed() { var currentEbatchSheet = new EbatchSheetEntity.EbatchSheet() { CurrentState = EbatchState.ProductionReview }; var updateRequest = new UpdateEbatchSheetCommand() { CurrentState = EbatchState.WarehouseReview }; var updateResponse = new CosmosResponse <EbatchSheetEntity.EbatchSheet>(currentEbatchSheet, new ResourceResponse <Document>()); _cosmosStore.Setup(f => f.FindAsync(It.IsAny <string>(), It.IsAny <string>())).ReturnsAsync(currentEbatchSheet); _cosmosStore.Setup(f => f.UpdateAsync(It.IsAny <EbatchSheetEntity.EbatchSheet>(), null)).ReturnsAsync(updateResponse); _httpContext.Setup(x => x.HttpContext.User.IsInRole(It.IsAny <string>())).Returns(false); var updateHandler = new UpdateEbatchSheetCommandHandler(_cosmosStore.Object, _httpContext.Object, _updateLogger.Object, _mailSender.Object); Assert.ThrowsAsync <InvalidStateChange>(() => updateHandler.Handle(updateRequest, CancellationToken.None)); }
public async Task AddValidObjectSuccess() { // Arrange var id = Guid.NewGuid().ToString(); var dummy = new Dummy { Id = id, Name = "Nick" }; _mockDocumentClient.Setup(x => x.CreateDocumentAsync(It.IsAny <string>(), dummy.GetCosmosDbFriendlyEntity() as Document, null, false)) .ReturnsAsync(new ResourceResponse <Document>()); var entityStore = new CosmosStore <Dummy>(_mockDocumentClient.Object, "databaseName", new CosmosDatabaseCreator(_mockDocumentClient.Object), new CosmosCollectionCreator(_mockDocumentClient.Object)); // Act var expectedResponse = new CosmosResponse <Dummy>(dummy, CosmosOperationStatus.Success); var result = await entityStore.AddAsync(dummy); //Assert Assert.Equal(expectedResponse.Entity, result.Entity); Assert.Equal(expectedResponse.IsSuccess, result.IsSuccess); }