コード例 #1
0
        public HttpResponse Add(CreateTripFormModel model)
        {
            var modelErrors = this.validator.ValidateTrip(model);

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

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

            var trip = new Trip
            {
                StartPoint    = model.StartPoint,
                EndPoint      = model.EndPoint,
                DepartureTime = departure,
                ImagePath     = model.ImagePath,
                Seats         = model.Seats,
                Description   = model.Description
            };

            trip.UserTrips.Add(new UserTrip
            {
                UserId = this.User.Id,
            });

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

            return(Redirect("/Trips/All"));
        }
コード例 #2
0
        public ICollection <string> ValidateTrip(CreateTripFormModel model)
        {
            var errors = new List <string>();

            if (model.Seats < SeatsMin || model.Seats > SeatsMax)
            {
                errors.Add("Seats should be between 2 and 6.");
            }

            if (model.Description.Length > DescriptionMax)
            {
                errors.Add("Description can not be longer than 80 symbols.");
            }

            return(errors);
        }
コード例 #3
0
        public HttpResponse Add(CreateTripFormModel model)
        {
            var modelErrors = this.validator.ValidateTrip(model);

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

            if (!this.User.IsAuthenticated)
            {
                var errors = new List <string>()
                {
                    "Users",
                    "must",
                    "login",
                    "to",
                    "add",
                    "a",
                    "trip"
                };

                return(Error(modelErrors));
            }

            var trip = new Trip
            {
                StartPoint    = model.StartPoint,
                EndPoint      = model.EndPoint,
                DepartureTime = model.DepartureTime,
                Seats         = model.Seats,
                Description   = model.Description,
                ImagePath     = model.ImagePath,
            };

            this.data.Trips.Add(trip);

            this.data.SaveChanges();

            return(Redirect("/Trips/All"));
        }
コード例 #4
0
ファイル: Validator.cs プロジェクト: n1claren/SoftUni
        public ICollection <string> ValidateTrip(CreateTripFormModel model)
        {
            var errors = new List <string>();

            if (model.Seats < 2)
            {
                errors.Add("Minimum seats required are 2.");
            }

            if (model.Seats > 6)
            {
                errors.Add("Maximum seats allowed are 6");
            }

            if (model.Description.Length > 80)
            {
                errors.Add("Trip description cannot exceed 80 symbols.");
            }

            return(errors);
        }