public async Task <IActionResult> Add(InputTripViewModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(model));
            }

            if (this._citiesService.IsCityInCountry(model.DepartureLocationId, model.DepartureCountryId) ||
                this._citiesService.IsCityInCountry(model.DestinationLocationId, model.DestinationCountryId))
            {
                this.ModelState.AddModelError("", InputModelsConstants.CityInCountryError);
                return(this.View(model));
            }

            if (this._transportsService.IsTransportOfCorrectType(model.TransportId, model.TransportType))
            {
                this.ModelState.AddModelError("", InputModelsConstants.TransportNotCorrectType);
                return(this.View(model));
            }

            if (this._citiesService.IsCityForTransportType(model.DepartureLocationId, model.TransportType) ||
                this._citiesService.IsCityForTransportType(model.DestinationLocationId, model.TransportType))
            {
                this.ModelState.AddModelError("", InputModelsConstants.CityDoesNotSupportsType);
                return(this.View(model));
            }
            var result = await this._tripsService.Add(model);

            this.TempData[GlobalConstants.SuccessMessageKey] = "Trip" + GlobalConstants.SuccessfullyAddedMessage;
            return(this.Redirect("Add"));
        }
Example #2
0
        public async Task <int> Add(InputTripViewModel model)
        {
            var destinationLocation = this._citiesService.GetById(model.DestinationLocationId);
            var departureLocation   = this._citiesService.GetById(model.DepartureLocationId);
            var transport           = this._transportsService.GetById(model.TransportId);
            var trip = new Trip
            {
                Departure     = departureLocation,
                Destination   = destinationLocation,
                DepartureTime = model.DepartureTime,
                ArrivalTime   = model.ArrivalTime,
                Transport     = transport
            };

            await this._tripsRepository.AddAsync(trip);

            var result = await this._tripsRepository.SaveChangesAsync();

            return(result);
        }