public void AddTrip(TripAddModel model) { 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 = model.Seats, Description = model.Description }; this.db.Trips.Add(trip); this.db.SaveChanges(); }
public HttpResponse Add(TripAddModel model) { if (!this.IsUserLoggedIn()) { return(this.Redirect("/Users/Login")); } if (string.IsNullOrWhiteSpace(model.StartPoint)) { return(this.Error("Starting Point is required!")); } if (string.IsNullOrWhiteSpace(model.EndPoint)) { return(this.Error("End Point is required!")); } if (!DateTime.TryParseExact(model.DepartureTime, "dd.MM.yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out _)) { return(this.Error("Invalid DateTime format!")); } if (!Uri.TryCreate(model.ImagePath, UriKind.Absolute, out _)) { return(this.Error("Image URL is invalid!")); } if (model.Seats < 2 || model.Seats > 6) { return(this.Error("Seats shoud be between 2 and 6!")); } if (string.IsNullOrWhiteSpace(model.Description) || model.Description.Length > 80) { return(this.Error("Description is required and shoud be less than 80 characters!")); } this.tripsService.AddTrip(model); return(this.Redirect("/Trips/All")); }