public async Task UpdateObjectThatExist() { var runner = MongoDbRunner.Start(); var database = TestHelpers.GetDefaultDatabase(runner.ConnectionString); var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "1.json"); runner.Import("okn", "objects", path, true); var config = new MapperConfiguration(cfg => cfg.AddProfile(typeof(MappingProfile))); var mapper = config.CreateMapper(); var repo = new ObjectsRepository(mapper, new DbContext(database)); var command = new UpdateObjectCommand("5af2796e32522f798f822a41") { Name = "TEST1" }; await repo.UpdateObject(command, CancellationToken.None); ; // var entities = _database.GetCollection<ObjectEntity>("objects").Find(Builders<ObjectEntity>.Filter // .Where(x => x.ObjectId == command.ObjectId)).ToList(); // Assert.Single(entities); // Assert.Equal(command.Name, entities[0].Name); // var versions = _database.GetCollection<ObjectEntity>("objects_versions").Find(Builders<ObjectEntity>.Filter // .Where(x => x.ObjectId == command.ObjectId)).ToList(); // Assert.Single(versions); // Assert.Equal("TEST", versions[0].Name); }
public async Task <IActionResult> Update([FromRoute] string objectId, [FromBody] UpdateObjectViewModel request) { if (request == null) { return(BadRequest()); } var objectQuery = new ObjectQuery(objectId); var current = await _objectsRepository.GetObject(objectQuery, CancellationToken.None); if (current == null) { return(NotFound()); } var currentUser = HttpContext.User; var updateCommand = new UpdateObjectCommand(objectId) { Name = request.Name, Description = request.Description, Latitude = request.Latitude, Longitude = request.Longitude, Type = request.Type }; if (request.MainPhoto != null) { updateCommand.MainPhoto = new FileInfo { FileId = request.MainPhoto.FileId, Description = request.MainPhoto.Description }; } if (request.Photos != null) { updateCommand.Photos = request.Photos.Select(x => new FileInfo { FileId = x.FileId, Description = x.Description }).ToList(); } updateCommand.SetCreator( long.Parse(currentUser.FindFirstValue(ClaimTypes.NameIdentifier)).ToString(), currentUser.FindFirstValue(ClaimTypes.Name), currentUser.FindFirstValue(ClaimTypes.Email)); await _objectsRepository.UpdateObject(updateCommand, CancellationToken.None); return(Ok()); }
public async Task UpdateObjectThatNotExist() { var runner = MongoDbRunner.Start(); var database = TestHelpers.GetDefaultDatabase(runner.ConnectionString); var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "1.json"); runner.Import("okn", "objects", path, true); var repo = new ObjectsRepository(null, new DbContext(database)); var command = new UpdateObjectCommand("5af27196e32522f798f822a41"); await Assert.ThrowsAsync <ObjectNotExistException>(() => repo.UpdateObject(command, CancellationToken.None)); }