public ICollection <string> ValidateTrip(TripFormModel model)
        {
            var errors = new List <string>();

            if (string.IsNullOrEmpty(model.StartPoint))
            {
                errors.Add("Start point is required!");
            }

            if (string.IsNullOrEmpty(model.EndPoint))
            {
                errors.Add("End point is required!");
            }

            if (string.IsNullOrEmpty(model.Description) || model.Description.Length > DescriptionMaxLength)
            {
                errors.Add("Description has max lenth 80 and it is required!");
            }

            if (model.Seats < TripSeatsMinNumber || model.Seats > TripSeatsmaxNumber)
            {
                errors.Add($"Trip seats must be between {TripSeatsMinNumber} and {TripSeatsmaxNumber} seats.");
            }

            if (!DateTime.TryParseExact(model.DepartureTime, "dd.MM.yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out _))
            {
                errors.Add("Datetime format is incorrect!");
            }

            return(errors);
        }
        public HttpResponse Add(TripFormModel model)
        {
            var modelErrors = this.validator.ValidateTrip(model);

            if (modelErrors.Any())
            {
                return(this.Error(modelErrors));
            }

            var trip = new Trip
            {
                StartPoint    = model.StartPoint,
                EndPoint      = model.EndPoint,
                DepartureTime = DateTime.ParseExact(model.DepartureTime, "dd.MM.yyyy HH:mm", CultureInfo.InvariantCulture),
                ImagePath     = model.ImagePath,
                Seats         = model.Seats,
                Description   = model.Description
            };

            this.data.Trips.Add(trip);

            this.data.SaveChanges();

            return(Redirect("/Trips/All"));
        }
Beispiel #3
0
        public async Task <IActionResult> Create(TripFormModel model)
        {
            var userId = this.userManager.GetUserId(this.User);

            await this.trips.CreateAsync(
                model.Title,
                model.StartLocation,
                model.EndLocation,
                model.StartDate,
                model.ExactAddress,
                model.Description,
                model.CarModel,
                model.PricePerPassenger,
                model.TotalSeats, userId);

            this.TempData.AddSuccessMessage(string.Format(WebConstants.TempDataCreateCommentText, ModelName));

            return(this.RedirectToAction(nameof(this.Index)));
        }
Beispiel #4
0
        public HttpResponse Add(TripFormModel model)
        {
            var errors = this.validator.ValidateTrip(model);

            if (errors.Any())
            {
                return(this.Error(errors));
            }

            this.tripService.Add(
                model.StartPoint,
                model.EndPoint,
                DateTime.Parse(model.DepartureTime, CultureInfo.InstalledUICulture),
                model.ImagePath,
                model.Seats,
                model.Description);

            return(this.Redirect("/Trips/All"));
        }
Beispiel #5
0
        public ActionResult GetTrain(TripFormModel model)
        {
            List <SuperTripViewModel> result = new List <SuperTripViewModel>();

            //model.DateOneWay = DateTime.ParseExact(model.DateOneWay.ToString(), "dd/MM/yyyy", null);

            /*string[] keys = Request.Form.AllKeys;
             * for (int i = 0; i < keys.Length; i++)
             * {
             *  System.Diagnostics.Debug.WriteLine(keys[i] + ": " + Request.Form[keys[i]] + "<br>");
             * }*/
            if (ModelState.IsValid)
            {
                //System.Diagnostics.Debug.WriteLine("ici");
                //call API

                result = GetTripFromApi(model.DepartOrArrivalOneWay, model.DepartureStation, model.ArrivalStation, model.DateOneWay, model.HourOneWay);
                foreach (var trip in result)
                {
                    trip.isReturn = false;
                }

                if (model.TripWay == false)
                {
                    List <SuperTripViewModel> tmp = GetTripFromApi(model.DepartOrArrivalReturn, model.ArrivalStation, model.DepartureStation, (DateTime)model.DateReturn, model.HourReturn);
                    foreach (var trip in tmp)
                    {
                        trip.isReturn = true;
                        result.Add(trip);
                    }
                }
            }

            Session["listProposition"] = result;

            TripFormAndSuperTripViewModel modelReturn = new TripFormAndSuperTripViewModel();

            modelReturn.tripFormModel      = model;
            modelReturn.superTripViewModel = result;


            return(View("Index", modelReturn));
        }