コード例 #1
0
        public ICollection <string> ValidateTrip(TripsAddFormModel model)
        {
            var errors = new List <string>();

            if (model.Seats < 2 || model.Seats > 6)
            {
                errors.Add($"Seat count not valid. It must be between 2 and 6.");
            }

            if (!Regex.IsMatch(model.DepartureTime, @"([0-9]{2})\.([0-9]{2})\.([0-9]{4})\ ([0-9]{2}):([0-9]{2})"))
            {
                errors.Add($"Email {model.DepartureTime} is not a valid date format.");
            }

            if (model.Description == null)
            {
                errors.Add("Description cannot be null.");
            }

            if (model.Description == null)
            {
                errors.Add("Description cannot be null.");
            }

            if (model.Description.Length > 80)
            {
                errors.Add("Description character limit reached. It cannot be above 80 characters long.");
            }


            // potential errors :wrong image url- but i won't implement it because from previous exams it has problems with images not ending in jpeg/jpg/png

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

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

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

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

            return(Redirect("/Trips/All"));
        }