public Trip AddTrip(Trip trip) { trip.User = _applicationUser; trip.Date = trip.Date.ToUniversalTime(); trip.AddressOrigin = trip.ResolveAddress(_dbContext, _applicationUser.Id, trip.AddressOrigin); trip.AddressDestination = trip.ResolveAddress(_dbContext, _applicationUser.Id, trip.AddressDestination); trip.Car = trip.ResolveCar(_dbContext, _applicationUser.Id, trip.Car); if (trip.AddressOrigin != null && trip.AddressDestination != null) { trip.DistanceInKm = _distanceCalculatorService.GetDistance(trip.AddressOrigin, trip.AddressDestination, Mode.Driving, Units.Metric); } _dbContext.Trips.Add(trip); _dbContext.SaveChanges(); return trip; }
public Trip UpdateTrip(Trip trip) { var existingTrip = _dbContext.Trips.SingleOrDefault(t => t.User.Id == _applicationUser.Id && t.Id == trip.Id); if (existingTrip == null) { throw new ObjectNotFoundException(); } existingTrip.Date = trip.Date.ToUniversalTime(); existingTrip.AddressOrigin = trip.ResolveAddress(_dbContext, _applicationUser.Id, trip.AddressOrigin); existingTrip.AddressDestination = trip.ResolveAddress(_dbContext, _applicationUser.Id, trip.AddressDestination); existingTrip.Car = trip.ResolveCar(_dbContext, _applicationUser.Id, trip.Car); existingTrip.Remarks = trip.Remarks; if (existingTrip.AddressOrigin != null && existingTrip.AddressDestination != null) { existingTrip.DistanceInKm = _distanceCalculatorService.GetDistance(existingTrip.AddressOrigin, existingTrip.AddressDestination, Mode.Driving, Units.Metric); } existingTrip.Remarks = trip.Remarks; _dbContext.SaveChanges(); return existingTrip; }
public void UpdateTrip_CanUpdate() { // Arrange var trip = new Trip { Id = 1 }; var mockTripService = new Mock<ITripService>(); mockTripService.Setup(x => x.UpdateTrip(trip)) .Returns(trip); var tripController = new TripsController(mockTripService.Object) { Request = new HttpRequestMessage(), Configuration = new HttpConfiguration() }; // Act var response = tripController.UpdateTrip(trip.Id, trip); // Assert Trip addedTrip; Assert.IsTrue(response.TryGetContentValue(out addedTrip)); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); }
public void UpdateTrip_CanUpdate() { // Arrange var addressOrigin = new Address { Id = 1, User = _currentUser }; var addressDestination = new Address { Id = 2, User = _currentUser }; var car = new Car { Id = 1, User = _currentUser }; var otherCar = new Car { Id = 2, User = _currentUser }; var otherAddress = new Address { Id = 3, User = _currentUser }; var trip = new Trip { Id = 1, User = _currentUser, Date = DateTime.SpecifyKind(new DateTime(2014, 08, 01), DateTimeKind.Utc), AddressOrigin = addressOrigin, AddressDestination = addressDestination, Car = car }; _fakeApplicationDbContext.Addresses.Add(addressOrigin); _fakeApplicationDbContext.Addresses.Add(addressDestination); _fakeApplicationDbContext.Addresses.Add(otherAddress); _fakeApplicationDbContext.Cars.Add(car); _fakeApplicationDbContext.Cars.Add(otherCar); _fakeApplicationDbContext.Trips.Add(trip); var tripService = new TripService(_fakeApplicationDbContext, _mockCurrentUserService.Object, _mockIDistanceCalculatorService.Object); var updatedTrip = new Trip { Id = 1, User = _currentUser, Date = DateTime.SpecifyKind(new DateTime(2014, 08, 02), DateTimeKind.Utc), AddressOrigin = addressOrigin, AddressDestination = otherAddress, Car = otherCar, Remarks = "" }; // Act updatedTrip = tripService.UpdateTrip(updatedTrip); // Assert Assert.IsNotNull(updatedTrip); Assert.IsNotNull(updatedTrip.AddressOrigin); Assert.IsNotNull(updatedTrip.AddressDestination); Assert.IsNotNull(updatedTrip.Car); Assert.AreEqual(new DateTime(2014, 08, 02), updatedTrip.Date); Assert.AreEqual(addressOrigin.Id, updatedTrip.AddressOrigin.Id); Assert.AreEqual(otherAddress.Id, updatedTrip.AddressDestination.Id); Assert.AreEqual(otherCar.Id, updatedTrip.Car.Id); Assert.AreEqual(updatedTrip.DistanceInKm, 42); Assert.AreEqual(updatedTrip.Id, 1); }
public void AddTrip_ShouldThrowValidationError() { // Arrange Exception caugthException = null; var trip = new Trip { User = _currentUser, Date = new DateTime(2014, 08, 01), AddressOrigin = null, AddressDestination = null, Car = null }; var tripService = new TripService(_fakeApplicationDbContext, _mockCurrentUserService.Object, _mockIDistanceCalculatorService.Object); // Act try { trip = tripService.AddTrip(trip); } catch (DbEntityValidationException ex) { caugthException = ex; } // Assert Assert.IsNull(trip); Assert.IsNotNull(caugthException); }
public void AddTrip_CanAdd() { // Arrange var addressOrigin = new Address { Id = 1, User = _currentUser}; var addressDestination = new Address { Id = 2, User = _currentUser }; var car = new Car { Id = 1, User = _currentUser }; _fakeApplicationDbContext.Addresses.Add(addressOrigin); _fakeApplicationDbContext.Addresses.Add(addressDestination); _fakeApplicationDbContext.Cars.Add(car); var trip = new Trip { User = _currentUser, Date = new DateTime(2014, 08, 01), AddressOrigin = addressOrigin, AddressDestination = addressDestination, Car = car }; var tripService = new TripService(_fakeApplicationDbContext, _mockCurrentUserService.Object, _mockIDistanceCalculatorService.Object); // Act trip = tripService.AddTrip(trip); // Assert Assert.IsNotNull(trip); Assert.IsNotNull(trip.AddressOrigin); Assert.IsNotNull(trip.AddressDestination); Assert.IsNotNull(trip.Car); Assert.AreEqual(addressOrigin.Id, trip.AddressOrigin.Id); Assert.AreEqual(addressDestination.Id, trip.AddressDestination.Id); Assert.AreEqual(car.Id, trip.Car.Id); Assert.AreEqual(trip.DistanceInKm, 42); }