public IHttpActionResult Post(TripAdd trip) { if (ModelState.IsValid) { var keyValuePairs = this.tripRepository.AddTrip(trip, Request.Properties["vendor"] as Vendor); if (keyValuePairs.ContainsKey("Success")) { return(Created("", keyValuePairs["Success"])); } else if (keyValuePairs.ContainsKey("Errors")) { return(Content(HttpStatusCode.Conflict, new { Errors = keyValuePairs["Errors"] })); } else { return(StatusCode(HttpStatusCode.BadRequest)); } } else { var errors = new Hashtable(); foreach (var pair in ModelState) { if (pair.Value.Errors.Count > 0) { errors[pair.Key] = pair.Value.Errors.Select(error => error.ErrorMessage).ToList().ElementAt(0); } } return(Content(HttpStatusCode.BadRequest, new { Errors = errors })); } }
public Dictionary <string, Dictionary <string, string> > AddTrip(TripAdd trip, Vendor vendor) { Dictionary <string, Dictionary <string, string> > keyValuePairs = new Dictionary <string, Dictionary <string, string> >(); Dictionary <string, string> errors = new Dictionary <string, string>(); Bus bus = new BusRepository().Get(trip.BusId); if (bus == null) { errors.Add("user.BusId", "Invalid Bus"); } else if (bus.VendorId != vendor.VendorId) { errors.Add("user.BusId", "Invalid Bus"); } if (trip.Timing <= DateTime.Now) { errors.Add("user.Timing", "Can not shedule a trip on a past date"); } if (trip.LocationTo == trip.LocationFrom) { errors.Add("user.LocationTo", "Can not shedule a trip on same terminal"); } if (errors.Count == 0) { Trip _trip = new Trip(); _trip.BusId = trip.BusId; _trip.LocationFrom = trip.LocationFrom; _trip.LocationTo = trip.LocationTo; _trip.Timing = trip.Timing; this.Insert(_trip); keyValuePairs.Add("Success", new Dictionary <string, string>() { { "Msg", "Trip Scheduled successfully" } }); } else { keyValuePairs.Add("Errors", errors); } return(keyValuePairs); }