public IHttpActionResult PutCategoryDocument(int id, CategoryDocument categoryDocument) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != categoryDocument.id) { return(BadRequest()); } db.Entry(categoryDocument).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!CategoryDocumentExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
/// <summary> /// Adds a new cagtegory. /// </summary> /// <param name="categoryDocument">The document representation of the category.</param> /// <returns>The id of the category.</returns> public async Task <string> AddCategoryAsync(CategoryDocument categoryDocument) { var documentUri = UriFactory.CreateDocumentCollectionUri(DatabaseName, CollectionName); Document doc = await DocumentClient.CreateDocumentAsync(documentUri, categoryDocument).ConfigureAwait(false); return(doc.Id); }
public static CategoryDto AsDto(this CategoryDocument document) => new CategoryDto { Id = document.Id, Name = document.Name, Description = document.Description, CreatedAt = document.CreatedAt };
/// <summary> /// Adds a new cagtegory. /// </summary> /// <param name="categoryDocument">The document representation of the category.</param> /// <returns>The id of the category.</returns> public Task <string> AddCategoryAsync(CategoryDocument categoryDocument) { if (string.IsNullOrEmpty(categoryDocument.Id)) { categoryDocument.Id = Guid.NewGuid().ToString(); } this.CategoryDocuments.Add(categoryDocument); return(Task.FromResult(categoryDocument.Id)); }
public Task UpdateCategoryAsync(CategoryDocument categoryDocument) { var documentToUpdate = CategoryDocuments.SingleOrDefault(d => d.Id == categoryDocument.Id && d.UserId == categoryDocument.UserId); if (documentToUpdate == null) { throw new InvalidOperationException("UpdateTextAsync called for document that does not exist."); } documentToUpdate.Name = categoryDocument.Name; return(Task.CompletedTask); }
public IHttpActionResult GetCategoryDocument(int id) { CategoryDocument categoryDocument = db.CategoryDocuments.Find(id); if (categoryDocument == null) { return(NotFound()); } return(Ok(categoryDocument)); }
public async Task <Result <IPagedList <CategoryMinimalDto> > > Handle(GetCategoriesQuery request, CancellationToken cancellationToken) { var categories = await _collection.CategoriesCollection.Find(Filter()) .SortByDescending(CategoryDocument.GetOrderBy(request.OrderBy)) .Skip(PaginationHelper.Skip(request.PageNumber, request.PageSize)) .Limit(request.PageSize) .Project(Projections.MinimalCategoryProjection()) .ToListAsync(cancellationToken); return(Result.Ok(categories.ToPagedList(request.PageNumber, request.PageSize))); }
public IHttpActionResult PostCategoryDocument(CategoryDocument categoryDocument) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.CategoryDocuments.Add(categoryDocument); db.SaveChanges(); return(CreatedAtRoute("DefaultApi", new { id = categoryDocument.id }, categoryDocument)); }
public Task UpdateCategoryAsync(CategoryDocument categoryDocument) { var documentUri = UriFactory.CreateDocumentUri(DatabaseName, CollectionName, categoryDocument.Id); var concurrencyCondition = new AccessCondition { Condition = categoryDocument.ETag, Type = AccessConditionType.IfMatch }; return(DocumentClient.ReplaceDocumentAsync(documentUri, categoryDocument, new RequestOptions { AccessCondition = concurrencyCondition })); }
/// <summary> /// Updates a category. /// </summary> /// <param name="categoryDocument">The category details to update.</param> /// <returns>The results of the category update.</returns> public async Task <UpdateCategoryResult> UpdateCategoryAsync(CategoryDocument categoryDocument) { var documentToUpdate = await Task.Run( () => this.CategoryDocuments.SingleOrDefault(d => d.Id == categoryDocument.Id && d.UserId == categoryDocument.UserId)) .ConfigureAwait(false); if (documentToUpdate == null) { return(UpdateCategoryResult.NotFound); } documentToUpdate.Name = categoryDocument.Name; return(UpdateCategoryResult.Success); }
public IHttpActionResult DeleteCategoryDocument(int id) { CategoryDocument categoryDocument = db.CategoryDocuments.Find(id); if (categoryDocument == null) { return(NotFound()); } db.CategoryDocuments.Remove(categoryDocument); db.SaveChanges(); return(Ok(categoryDocument)); }
public async Task ProcessAddItemEventAsyncUpdatesItemWhenAlreadyExists() { // arrange var fakeCategoriesRepository = new FakeCategoriesRepository(); var category = new CategoryDocument { Id = "fakecategoryid", Name = "fakename", UserId = "fakeuserid" }; category.Items.Add(new CategoryItem { Id = "fakeitemid", Preview = "oldpreview" }); fakeCategoriesRepository.CategoryDocuments.Add(category); var service = new CategoriesService( fakeCategoriesRepository, new Mock <IImageSearchService>().Object, new Mock <ISynonymService>().Object, new Mock <IEventGridPublisherService>().Object); var eventToProcess = new EventGridEvent { Subject = "fakeuserid/fakeitemid", EventType = AudioEvents.AudioCreated, Data = new AudioCreatedEventData { Category = "fakecategoryid", TranscriptPreview = "newpreview" } }; // act await service.ProcessAddItemEventAsync(eventToProcess, "fakeuserid").ConfigureAwait(false); // assert var itemsCollection = fakeCategoriesRepository.CategoryDocuments.Single().Items; Assert.AreEqual(1, itemsCollection.Count); Assert.IsTrue( itemsCollection.Contains( new CategoryItem { Id = "fakeitemid", Preview = "newpreview", Type = ItemType.Audio }, new CategoryItemComparer())); }
public async Task ProcessUpdateItemEventAsyncPublishesCategoryItemsUpdatedEventToEventGrid() { // arrange var fakeCategoriesRepository = new FakeCategoriesRepository(); var category = new CategoryDocument { Id = "fakecategoryid", Name = "fakename", UserId = "fakeuserid", }; category.Items.Add(new CategoryItem { Id = "fakeitemid", Type = ItemType.Text, Preview = "oldpreview" }); fakeCategoriesRepository.CategoryDocuments.Add(category); var mockEventGridPublisherService = new Mock <IEventGridPublisherService>(); var service = new CategoriesService( fakeCategoriesRepository, new Mock <IImageSearchService>().Object, new Mock <ISynonymService>().Object, mockEventGridPublisherService.Object); var eventToProcess = new EventGridEvent { Subject = "fakeuserid/fakeitemid", EventType = TextEvents.TextUpdated, Data = new TextUpdatedEventData { Preview = "newpreview" } }; // act await service.ProcessUpdateItemEventAsync(eventToProcess, "fakeuserid").ConfigureAwait(false); // assert mockEventGridPublisherService.Verify( m => m.PostEventGridEventAsync( CategoryEvents.CategoryItemsUpdated, "fakeuserid/fakecategoryid", It.IsAny <CategoryItemsUpdatedEventData>()), Times.Once); }
public async Task <string> AddCategoryAsync(string name, string userId) { // create the document in Cosmos DB var categoryDocument = new CategoryDocument { Name = name, UserId = userId }; var categoryId = await CategoriesRepository.AddCategoryAsync(categoryDocument); // post a CategoryCreated event to Event Grid var eventData = new CategoryCreatedEventData { Name = name }; var subject = $"{userId}/{categoryId}"; await EventGridPublisher.PostEventGridEventAsync(EventTypes.Categories.CategoryCreated, subject, eventData); return(categoryId); }
public async Task ProcessUpdateItemEventAsyncUpdatesTextItem() { // arrange var fakeCategoriesRepository = new FakeCategoriesRepository(); var category = new CategoryDocument { Id = "fakecategoryid", Name = "fakename", UserId = "fakeuserid", }; category.Items.Add(new CategoryItem { Id = "fakeitemid", Type = ItemType.Text, Preview = "oldpreview" }); fakeCategoriesRepository.CategoryDocuments.Add(category); var service = new CategoriesService( fakeCategoriesRepository, new Mock <IImageSearchService>().Object, new Mock <ISynonymService>().Object, new Mock <IEventGridPublisherService>().Object); var eventToProcess = new EventGridEvent { Subject = "fakeuserid/fakeitemid", EventType = TextEvents.TextUpdated, Data = new TextUpdatedEventData { Preview = "newpreview" } }; // act await service.ProcessUpdateItemEventAsync(eventToProcess, "fakeuserid").ConfigureAwait(false); // assert var itemsCollection = fakeCategoriesRepository.CategoryDocuments.Single().Items; Assert.AreEqual("newpreview", itemsCollection.Single().Preview); }
/// <summary> /// Updates a category. /// </summary> /// <param name="categoryDocument">The category details to update.</param> /// <returns>The results of the category update.</returns> public async Task <UpdateCategoryResult> UpdateCategoryAsync(CategoryDocument categoryDocument) { if (categoryDocument == null) { throw new ArgumentNullException(nameof(categoryDocument)); } var documentUri = UriFactory.CreateDocumentUri(DatabaseName, CollectionName, categoryDocument.Id); var concurrencyCondition = new AccessCondition { Condition = categoryDocument.ETag, Type = AccessConditionType.IfMatch, }; var document = await DocumentClient.ReplaceDocumentAsync(documentUri, categoryDocument, new RequestOptions { AccessCondition = concurrencyCondition }).ConfigureAwait(false); if (document.StatusCode == System.Net.HttpStatusCode.OK) { return(UpdateCategoryResult.Success); } return(UpdateCategoryResult.NotFound); }
public async Task ProcessDeleteItemEventAsyncDeletesAudioItem() { // arrange var fakeCategoriesRepository = new FakeCategoriesRepository(); var category = new CategoryDocument { Id = "fakecategoryid", Name = "fakename", UserId = "fakeuserid", }; category.Items.Add(new CategoryItem { Id = "fakeitemid", Type = ItemType.Audio }); fakeCategoriesRepository.CategoryDocuments.Add(category); var service = new CategoriesService( fakeCategoriesRepository, new Mock <IImageSearchService>().Object, new Mock <ISynonymService>().Object, new Mock <IEventGridPublisherService>().Object); var eventToProcess = new EventGridEvent { Subject = "fakeuserid/fakeitemid", EventType = AudioEvents.AudioDeleted, Data = new AudioDeletedEventData() }; // act await service.ProcessDeleteItemEventAsync(eventToProcess, "fakeuserid").ConfigureAwait(false); // assert Assert.IsTrue(fakeCategoriesRepository.CategoryDocuments.Single().Items.Count == 0); }
private async Task CreateCategories() { Console.Out.WriteLine("Creating Categories"); var categories = new Dictionary <string, CategoryDocument>(); var collectionId = CollectionIdHelper.GetId(typeof(CategoryDocument)); await CreateCollectionIfNotExistsAsync(DocumentClient, collectionId); foreach (var document in Documents) { var primaryCategory = document.Categories[0]; CategoryDocument categoryDocument; if (categories.ContainsKey(primaryCategory.Id)) { categoryDocument = categories[primaryCategory.Id]; } else { categoryDocument = new CategoryDocument(primaryCategory.Id, primaryCategory.Name); categories.Add(primaryCategory.Id, categoryDocument); } var secondaryCategory = document.Categories[1]; categoryDocument.AddSubCategory(new SubCategory(secondaryCategory.Id, secondaryCategory.Name)); } foreach (var category in categories) { await DocumentClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(DatabaseId, collectionId), category.Value); Console.Out.WriteLine($"Created {category.Key} {category.Value.Name}"); } }
public async Task AddCategoryDocument(CategoryDocument categoryDocument) { dmsDbContext.Add <CategoryDocument>(categoryDocument); await dmsDbContext.SaveChangesAsync(); }