public IHttpActionResult PostTrip(Trip trip)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            tripService.Add(trip);

            return(CreatedAtRoute("DefaultApi", new { id = trip.Id }, trip));
        }
Beispiel #2
0
        public void Add_Any_AddTrip()
        {
            //Arrange
            IList <Trip> trips = new List <Trip>()
            {
                new Trip()
            };

            Mock <ITripRepository> MockTripRepository = new Mock <ITripRepository>();

            TripRepositorySetupMoq.Add(MockTripRepository, trips);
            ITripService tripService = new TripService(MockTripRepository.Object);

            //Act
            tripService.Add(new Trip());

            //Assert
            Assert.AreEqual(trips.Count, 2);
        }
Beispiel #3
0
        public HttpResponse Add(TripAddInputModel model)
        {
            var validation = model.IsValid();

            if (!validation.isSuccessful)
            {
                return(Error(validation.errorMessage));
            }

            bool isDateValid = DateTime.TryParseExact(
                model.DepartureTime,
                "dd.MM.yyyy HH:mm",
                CultureInfo.InvariantCulture,
                DateTimeStyles.None,
                out _
                );

            if (!isDateValid)
            {
                return(Error("Date is not in valid format"));
            }

            _tripService.Add(model);
        }
Beispiel #4
0
 public Result Post([FromBody] Trip trip)
 {
     return(_tripService.Add(trip));
 }
        public void SplitBill_GetTotalUsers()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "Add_writes_to_database")
                          .Options;

            // Arrange

            // New User
            User user = new User()
            {
                UserId = Guid.NewGuid(), Name = "Gian", Email = "*****@*****.**", Total = 0
            };
            User user2 = new User()
            {
                UserId = Guid.NewGuid(), Name = "Rocio", Email = "*****@*****.**", Total = 0
            };
            User user3 = new User()
            {
                UserId = Guid.NewGuid(), Name = "Jose", Email = "*****@*****.**", Total = 0
            };

            // New Bill
            Bill bill = new Bill()
            {
                BillId = Guid.NewGuid(),
                Debit  = 1955.66,
                Users  = new List <User>()
                {
                    user, user2, user3
                }
            };

            // New Trip
            Trip trip = new Trip()
            {
                TripId   = Guid.NewGuid(),
                Location = "Paris",
                Bills    = new List <Bill>()
                {
                    bill
                }
            };

            // Add Trip
            using (var context = new ApplicationDbContext(options))
            {
                var service = new TripService(context);
                service.Add(trip);
            }

            // Act
            // Run the test against one instance of the context
            using (var context = new ApplicationDbContext(options))
            {
                var service = new BillService(context);
                service.SplitBill(bill);
            }

            // Assert
            double expected = 1955.66 / 3;

            // Use a separate instance of the context to verify correct data was saved to database
            using (var context = new ApplicationDbContext(options))
            {
                var service = new UserService(context);
                var total   = service.GetTotal(user);
                Assert.AreEqual(expected, total, "Split not divided correctly");
            }
        }