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

            if (model.StartPoint.Length == 0)
            {
                errors.Add($"StartPoint must not be  empty!");
            }
            if (model.EndPoint.Length == 0)
            {
                errors.Add($"EndPoint must not be  empty!");
            }
            if (model.DepartureTime.Length == 0)
            {
                errors.Add($"DepartureTime must not be  empty!");
            }

            try
            {
                var date = DateTime.ParseExact(model.DepartureTime, DepartureTimeFormat, CultureInfo.InvariantCulture);
            }
            catch (Exception)
            {
                errors.Add($"Datetime is invalid.DateTime format must be {DepartureTimeFormat} !");
            }

            if (model.Seats < SeatsMinLength || model.Seats > SeatsMaxLength)
            {
                errors.Add($"Seats must be between {SeatsMinLength} and {SeatsMaxLength}");
            }
            if (model.Description.Length > DescriptionMaxLength)
            {
                errors.Add($"Description length must be max {DescriptionMaxLength}!");
            }
            if (model.Description.Length == 0)
            {
                errors.Add($"Description must not be  empty!");
            }

            return(errors);
        }
Beispiel #2
0
        public HttpResponse Add(AddTripsModel model)
        {
            var errors = this.validator.ValidateTrip(model);

            if (errors.Any())
            {
                return(Redirect("/Trips/Add"));
            }

            var trip = new Trip {
                EndPoint      = model.EndPoint,
                Description   = model.Description,
                ImagePath     = model.ImagePath,
                Seats         = model.Seats,
                StartPoint    = model.StartPoint,
                DepartureTime = DateTime.ParseExact(model.DepartureTime, DepartureTimeFormat, CultureInfo.InvariantCulture),
            };

            this.data.Trips.Add(trip);
            this.data.SaveChanges();
            return(this.Redirect("/Trips/All"));
        }