public IHttpActionResult PostTrip(AddTripBindingModel model)
        {
            if (model == null)
            {
                return(this.BadRequest("Model cannot be null (no data in request)"));
            }

            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            var departureCity = this.Data.Cities.Find(model.DepartureCityId);
            var arrivalCity   = this.Data.Cities.Find(model.ArrivalCityId);

            if (departureCity == null)
            {
                return(this.BadRequest("Departure city does not exist!"));
            }

            if (arrivalCity == null)
            {
                return(this.BadRequest("Arrival city does not exist!"));
            }

            var trip = new Trip()
            {
                Title           = model.Title,
                ArrivalCityId   = model.ArrivalCityId,
                Description     = model.Description,
                AvailableSeats  = model.AvaibleSeats,
                DepartureCityId = model.DepartureCityId,
                DepartureDate   = model.DepartureDate,
                DriverId        = this.User.Identity.GetUserId()
            };

            this.Data.Trips.Add(trip);
            this.Data.SaveChanges();

            var data = this.Data.Trips
                       .All().Where(t => t.Id == trip.Id)
                       .Select(TripViewModel.Create)
                       .FirstOrDefault();

            return(this.Ok(data));
        }
        public IHttpActionResult UpdateTrip(int id, AddTripBindingModel model)
        {
            var trip = this.Data.Trips.Find(id);

            if (trip == null)
            {
                return(this.BadRequest(string.Format("There is no trip with Id {0}", id)));
            }

            var loggedUserId = this.User.Identity.GetUserId();

            if (trip.DriverId != loggedUserId)
            {
                return(this.Unauthorized());
            }

            if (model == null)
            {
                return(this.BadRequest("Model cannot be null (no data in request)"));
            }

            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            trip.ArrivalCityId   = model.ArrivalCityId;
            trip.DepartureCityId = model.DepartureCityId;
            trip.DepartureDate   = model.DepartureDate;
            trip.AvailableSeats  = model.AvaibleSeats;
            trip.Description     = model.Description;
            trip.Title           = model.Title;

            this.Data.SaveChanges();

            var data = this.Data.Trips
                       .All().Where(t => t.Id == trip.Id)
                       .Select(TripViewModel.Create)
                       .FirstOrDefault();

            return(this.Ok(data));
        }