public HttpResponse Add(AddingTripModel model) { var errors = validator.ValidateTripAdding(model); if (errors.Any()) { //return Error(errors); return(Redirect("/Trips/Add")); } var trip = new Trip { DepartureTime = model.DepartureTime, EndPoint = model.EndPoint, StartPoint = model.StartPoint, Seats = model.Seats, Description = model.Description, ImagePath = model.ImagePath }; data.Trips.Add(trip); data.SaveChanges(); return(Redirect("/Trips/All")); }
public ICollection <string> ValidateTripAdding(AddingTripModel trip) { var errors = new List <string>(); var isDepartureDate = DateTime.TryParseExact(trip.DepartureTime, "dd.MM.yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out var departureTime); if (trip.StartPoint == null) { errors.Add("Missing start point."); } if (trip.EndPoint == null) { errors.Add("Missing end point."); } if (!isDepartureDate) { errors.Add("Invalid date.It should be in format: dd.MM.yyyy HH: mm"); } if (trip.Seats < SeatMinValue || trip.Seats > SeatMaxValue) { errors.Add($"Seats should be between {SeatMinValue} and {SeatMaxValue}"); } if (trip.Description == null || trip.Description.Length > DescriptionMaxLength) { errors.Add($"Description should be with maximum {DescriptionMaxLength} symbols."); } return(errors); }