public HttpResponse Add(AddTripModel model) { var errorsModel = this.validator.ValidateTrip(model); if (this.context.Trips.Any(t => t.EndPoint == model.EndPoint && t.StartPoint == model.StartPoint)) { errorsModel.Add("This trip is already entered."); } if (errorsModel.Any()) { return(Error(errorsModel)); } var trip = new Trip { EndPoint = model.EndPoint, StartPoint = model.StartPoint, DepartureTime = DateTime.ParseExact(model.DepartureTime, "dd.MM.yyyy HH:mm", CultureInfo.InvariantCulture), Description = model.Description, ImagePath = model.ImagePath, Seats = model.Seats }; this.context.Trips.Add(trip); this.context.SaveChanges(); return(Redirect("/Trips/All")); }
public ICollection <string> ValidateTrip(AddTripModel model) { var errors = new List <string>(); if (string.IsNullOrWhiteSpace(model.EndPoint) || string.IsNullOrWhiteSpace(model.StartPoint) || string.IsNullOrWhiteSpace(model.DepartureTime.ToString()) || string.IsNullOrWhiteSpace(model.Description)) { errors.Add("There is an empty field in the form."); } if (!DateTime.TryParseExact(model.DepartureTime, "dd.MM.yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out _)) { errors.Add("DepartureTime is in invalid format."); } if (string.IsNullOrWhiteSpace(model.ImagePath) || !Uri.IsWellFormedUriString(model.ImagePath, UriKind.Absolute)) { errors.Add($"Image '{model.ImagePath}' is not valid. It must be a valid URL."); } if (model.Description.Length > TripMaxDescription) { errors.Add("Description is too long."); } return(errors); }
public string Add(AddTripModel model) { 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, }; db.Trips.Add(trip); db.SaveChanges(); return(trip.Id); }
public ICollection <string> ValidateTrip(AddTripModel model) { var errors = new List <string>(); if (string.IsNullOrWhiteSpace(model.EndPoint) || string.IsNullOrWhiteSpace(model.StartPoint) || string.IsNullOrWhiteSpace(model.DepartureTime.ToString()) || string.IsNullOrWhiteSpace(model.ImagePath) || string.IsNullOrWhiteSpace(model.Description)) { errors.Add("Some field in form is empty."); } if (model.DepartureTime.ToString() == model.DepartureTime.ToString("dd.MM.yyyy HH: mm")) { errors.Add("DepartureTime is in invalid format."); } return(errors); }
public HttpResponse Add(AddTripModel input) { if (!this.IsUserLoggedIn()) { return(this.Redirect("/Users/Login")); } if (input.Description.Length < 80) { return(this.View()); } if (input.Seats < 2 || input.Seats > 6) { return(this.View()); } var tripId = this.tripsService.Add(input); return(this.Redirect("/Trips/All")); }