public async Task <MyWebApp.Domain.Collection> InsertAsync(CollectionUpdateModel collection) { var result = await this.Context.AddAsync(this.Mapper.Map <Collection>(collection)); await this.Context.SaveChangesAsync(); return(this.Mapper.Map <MyWebApp.Domain.Collection>(result.Entity)); }
public async Task <IActionResult> Update(string id, CollectionUpdateModel model) { if (!ModelState.IsValid) { return(null); } return(Ok(await _manager.Create(model))); }
public async Task <ApiResponse> Create(CollectionUpdateModel model) { var user = await CurrentUser(); var social = await _userSocialsStore.GetTwitter(user.Id); if (social == null) { Warning("No user twitter account"); return(Failed("No twitter account")); } var entity = _mapper.Map <TwitterSourceCollection>(model); entity.UserId = _currentUser.Id; entity = await _twitterCollectionsStore.Add(entity); if (model.SourcesIds != null && model.SourcesIds.Any()) { OAuthTwitter(_client, social); var sourcesList = new List <TwitterSource>(); int skipFactor = 0; while (true) { var usersToSearch = model.SourcesIds.Skip(50 * skipFactor).Take(50); var uploadedUsers = User.UserFactory.GetUsersFromIds(usersToSearch); foreach (var uploaded in uploadedUsers) { var u = _mapper.Map <TwitterSource>(uploaded); u.Id = null; u.TwitterId = uploaded.Id; u.AccountCreatedAt = uploaded.CreatedAt; u.CollectionId = entity.Id; sourcesList.Add(u); } if (usersToSearch.Count() < 50) { break; } skipFactor++; } await _twitterSourcesStore.Add(sourcesList); } return(Ok()); }
public async Task <MyWebApp.Domain.Collection> UpdateAsync(CollectionUpdateModel collection) { var existing = await this.Get(collection); var result = this.Mapper.Map(collection, existing); this.Context.Update(result); await this.Context.SaveChangesAsync(); return(this.Mapper.Map <MyWebApp.Domain.Collection>(result)); }
public async Task CreateAsync_CollectionValidationSucceed_CreatesCollection() { // Arrange var collection = new CollectionUpdateModel(); var expected = new Collection(); var collectionDAL = new Mock <ICollectionDAL>(); collectionDAL.Setup(x => x.InsertAsync(collection)).ReturnsAsync(expected); var collectionService = new CollectionService(collectionDAL.Object); // Act var result = await collectionService.CreateAsync(collection); // Assert result.Should().Be(expected); }
public async Task <IActionResult> UpdateCollectionAsync(long id, [FromBody] CollectionUpdateModel model) { var collection = await _collectionRepository.GetCollectionAsync(id); if (collection == null) { return(NotFound("Invalid collection Id")); } if (!ModelState.IsValid) { return(new ValidateObjectResult(ModelState)); } collection.UpdateCollection(model.CollectionName); await _collectionRepository.UpdateCollectionAsync(); return(NoContent()); }
public async Task <IActionResult> Update([FromBody] CollectionUpdateModel collection) { var userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value; if (String.IsNullOrEmpty(userId)) { return(NotFound()); } var updateCollectionC = new UpdateCollectionC(collection, userId); var actionResponse = await _mediator.Send(updateCollectionC); if (actionResponse.IsSucceed) { return(Ok()); } return(BadRequest(actionResponse.Message)); }
public async Task TestB_UserNotOwnCollection() { var mocker = new MockDataV3(); mocker.Reset(); _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1laWRlbnRpZmllciI6IjlhNGUxZDc5LWQ2NGUtNGVjNC04NWU1LTUzYmRlZjUwNDNmNCIsIm5iZiI6MTYxOTc2OTYzOCwiZXhwIjoxNjE5ODU2MDM4LCJpc3MiOiJhIn0.tohmUFgbnXqaMoehSX9i-p_F6vpdoziu9Jz5XgM1N1k"); var inputDTO = new CollectionUpdateModel() { Id = Guid.Parse("82c3a0d1-a73c-41e2-a8f3-ef525e5f0ffa"), Name = "name" }; var response = await _client.PutAsync("api/collection/update", new StringContent(JsonConvert.SerializeObject(inputDTO), Encoding.UTF8, "application/json")); Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); using (var db = MockDatabaseFactory.Build()) { Assert.Equal("User 2 Name 2", db.Collections.FirstOrDefault(e => e.Id == Guid.Parse("82c3a0d1-a73c-41e2-a8f3-ef525e5f0ffa")).Name); } }
public UpdateCollectionC(CollectionUpdateModel collection, string userId) { Collection = collection; UserId = userId; }
public async Task <ApiResponse> Update(CollectionUpdateModel model) { var entity = await _twitterCollectionsStore.Get(model.Id); if (entity == null) { return(Failed("Not found")); } if (entity.UserId != (await CurrentUser())?.Id) { return(Failed("Access denied")); } var social = _userSocialsStore.GetTwitter((await CurrentUser()).Id); if (social == null) { return(Failed("Twitter account not found")); } entity.Title = model.Title; entity.Comments = model.Comments; if ((await _twitterCollectionsStore.Update(entity)) != 1) { _logger.LogError("Update collection failed", entity); return(Failed("Server unavaliable")); } var sources = await _twitterSourcesStore.GetBy(x => x.CollectionId == entity.Id); var toDelete = new List <TwitterSource>(sources ?? new List <TwitterSource>()); var toInsert = new List <long>(); foreach (var newSource in model.SourcesIds) { var exists = toDelete.FirstOrDefault(x => x.TwitterId == newSource); if (exists != null) { toDelete.Remove(exists); } else { toInsert.Add(newSource); } } if (toDelete.Any()) { if ((await _twitterSourcesStore.DeleteSoft(toDelete)) != toDelete.Count) { _logger.LogError("Error deleting sources", toDelete); return(Failed("Service unavaliable")); } } if (toInsert.Any()) { var list = new List <TwitterSource>(); int skipFactor = 0; while (true) { var usersToSearch = model.SourcesIds.Skip(50 * skipFactor).Take(50); var uploadedUsers = User.UserFactory.GetUsersFromIds(usersToSearch); foreach (var uploaded in uploadedUsers) { var u = _mapper.Map <TwitterSource>(uploaded); u.Id = null; u.TwitterId = uploaded.Id; u.AccountCreatedAt = uploaded.CreatedAt; u.CollectionId = entity.Id; list.Add(u); } if (usersToSearch.Count() < 50) { break; } skipFactor++; } await _twitterSourcesStore.Add(list); } return(Ok()); }
public async Task <Collection> UpdateAsync(CollectionUpdateModel collection) { return(await this.CollectionDAL.UpdateAsync(collection)); }