public HttpResponse Add(AddTripFormModel model) { var modelErrors = this.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", null), ImagePath = model.ImagePath, Seats = model.Seats, Description = model.Description, }; db.Trips.Add(trip); db.SaveChanges(); return(Redirect("/Trips/All")); }
public ICollection <string> AddTrip(AddTripFormModel model, string userId) { var modelErrors = this.validator.ValidateTrip(model); if (modelErrors.Any()) { return(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 = int.Parse(model.Seats), Description = model.Description, }; dbContext.Trips.Add(trip); dbContext.SaveChanges(); var userTrip = new UserTrip { UserId = userId, TripId = trip.Id, }; dbContext.UserTrips.Add(userTrip); dbContext.SaveChanges(); return(modelErrors); }
public HttpResponse Add(AddTripFormModel model) { var modelErrors = this.validator.ValidateTrip(model); if (modelErrors.Any()) { return(Error(modelErrors)); } var trip = new Trip { StartPoint = model.StartPoint, EndPoint = model.EndPoint, Seats = model.Seats, Description = model.Description, DepartureTime = model.DepartureTime, ImagePath = model.ImagePath }; this.data.Trips.Add(trip); this.data.SaveChanges(); return(Redirect("/Trips/All")); }
public HttpResponse Add(AddTripFormModel model) { var modelErrors = this.validator.ValidateTrip(model); if (modelErrors.Any()) { return(Error(modelErrors)); } var trip = new Trip { StartPoint = model.StartPoint, EndPoint = model.EndPoint, ImagePath = model.ImagePath, DepartureTime = DateTime.Parse(model.DepartureTime, CultureInfo.InvariantCulture), Description = model.Description, Seat = model.Seats, }; data.Trips.Add(trip); data.UserTrips.Add(new UserTrip { UserId = this.User.Id, TripId = trip.Id, }); data.SaveChanges(); return(Redirect("/Trips/All")); }
public HttpResponse Add(AddTripFormModel model) { var modelErrors = this.tripsService.AddTrip(model, this.User.Id); if (modelErrors.Count > 0) { return(this.Error(modelErrors)); } return(this.Redirect("/Trips/All")); }
public ICollection <string> ValidateTrip(AddTripFormModel trip) { var errors = new List <string>(); trip.StartPoint.Trim(); trip.EndPoint.Trim(); trip.Description.Trim(); trip.ImagePath.Trim(); if (trip.StartPoint == null || trip.StartPoint == string.Empty) { errors.Add($"Trip StartPoint '{trip.StartPoint}' is not valid."); } if (trip.EndPoint == null || trip.EndPoint == string.Empty) { errors.Add($"Trip EndPoint '{trip.StartPoint}' is not valid."); } if (trip.ImagePath == null || trip.ImagePath == string.Empty || !Uri.IsWellFormedUriString(trip.ImagePath, UriKind.Absolute)) { errors.Add($"Image '{trip.ImagePath}' is not valid. It must be a valid URL."); } if (trip.Description == null || trip.Description == string.Empty || trip.Description.Length > DescriptionMaxLenght) { errors.Add($"Description is too long , must not be longer than '{DescriptionMaxLenght}'"); } if (trip.Seats < SeatsMinCount || trip.Seats > SeatsMaxCount) { errors.Add($"Seats count is not valid , must be beatween '{SeatsMinCount} and {SeatsMaxCount}'"); } if (trip.DepartureTime == null) { errors.Add($"DepartureTime not set"); } var date = DateTime.Parse(trip.DepartureTime, CultureInfo.InvariantCulture); if (date.Date == default) { errors.Add($"Wrong format of DepartureDate, Shold be in format 'dd.MM.yyyy HH:mm'"); } else if (date < DateTime.Now) { errors.Add($"DepartureTime is not valid , shoul not be before current date"); } return(errors); }
public HttpResponse Add(AddTripFormModel model) { var modelErrors = this.validator.ValidateAddTrip(model); if (modelErrors.Any()) { return(Error(modelErrors)); } this.tripsService.Create(model); return(Redirect("/Trips/All")); }
public void Create(AddTripFormModel 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 }; this.db.Trips.Add(trip); this.db.SaveChanges(); }
public ICollection <string> ValidateTrip(AddTripFormModel model) { var erros = new List <string>(); if (model.Seats < 2 || model.Seats > 6) { erros.Add("Seats must be between 2 and 6."); } if (model.Description.Length > 80) { erros.Add("Description can be max 80 haracters long."); } return(erros); }
public ICollection <string> ValidateTrip(AddTripFormModel model) { var errors = new List <string>(); if (model.Seats < SeatsMinAmount || model.Seats > SeatsMaxAmount) { errors.Add($"Number of seats '{model.Seats}' is not valid. It must be between {SeatsMinAmount} and {SeatsMaxAmount}."); } if (model.Description.Length > DescriptionMaxLength) { errors.Add($"The description is too long. It must be up to {DescriptionMaxLength} characters."); } return(errors); }
public ICollection <string> ValidateAddTrip(AddTripFormModel model) { var errors = new List <string>(); if (string.IsNullOrEmpty(model.StartPoint)) { errors.Add("A trip must have a start point."); } if (string.IsNullOrEmpty(model.EndPoint)) { errors.Add("A trip must have an end point."); } if (!DateTime.TryParseExact( model.DepartureTime, "dd.MM.yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out _)) { errors.Add("Invalid departure time. Please use dd.MM.yyyy HH:mm format."); } if (model.Seats < 2 || model.Seats > 6) { errors.Add("Seats count should be between 2 and 6."); } if (string.IsNullOrEmpty(model.Description) || model.Description.Length > 80) { errors.Add("Description should be between 1 and 80 characters."); } if (string.IsNullOrEmpty(model.ImagePath) || !Uri.IsWellFormedUriString(model.ImagePath, UriKind.RelativeOrAbsolute)) { errors.Add($"Image {model.ImagePath} is not a valid URL."); } return(errors); }
public ICollection <string> ValidateTrip(AddTripFormModel model) { var errors = new List <string>(); if (string.IsNullOrWhiteSpace(model.StartPoint)) { errors.Add($"Starting point is required"); } if (string.IsNullOrWhiteSpace(model.EndPoint)) { errors.Add($"End point is required"); } var isValidDateTime = DateTime.TryParseExact( model.DepartureTime, "dd.MM.yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out var date); if (!isValidDateTime) { errors.Add($"Departure time {model.DepartureTime} is not a valid date time. Time must be in format (dd.MM.yyyy HH:mm)"); } var isValidIntSeats = int.TryParse(model.Seats, out var seats); if (!isValidIntSeats || seats < 2 || seats > 6) { errors.Add($"Seats must be a valid digit between 2 and 6"); } if (string.IsNullOrWhiteSpace(model.Description) || model.Description.Length > 80) { errors.Add($"A description is required and must be less and 80 symbols"); } return(errors); }
public ICollection <string> ValidateTrip(AddTripFormModel trip) { var errors = new List <string>(); if (string.IsNullOrWhiteSpace(trip.StartPoint)) { errors.Add("Starting point connot be null or whitespace"); } if (string.IsNullOrWhiteSpace(trip.EndPoint)) { errors.Add("End point connot be null or whitespace"); } if (trip.Seats < SeatsMinValue || trip.Seats > SeatsMaxValue) { errors.Add($"Seats must be between {SeatsMinValue} and {SeatsMaxValue}"); } if (string.IsNullOrWhiteSpace(trip.Description) || trip.Description.Length > DescriptionMaxLength) { errors.Add($"Description must be less than or equal {DescriptionMaxLength} characters long"); } if (!Uri.IsWellFormedUriString(trip.ImagePath, UriKind.Absolute)) { errors.Add("Invalid image path"); } if (trip.DepartureTime < DateTime.UtcNow) { errors.Add($"Inavlid Departure time"); } return(errors); }