public ActionResult Edit(Guid id, [Bind(Include = "AirRouteId,NumberOfFreeSeats,DepartureDate,ArrivalDate,Price")] FlightFormModel request) { try { var routes = db.AirRoutes.Select(x => new SelectListItem { Value = x.AirRouteId.ToString(), Text = x.AirLine.Name + " " + x.StartAirportCode + "-" + x.FinishAirportCode }); ViewBag.AirRouteId = new SelectList(routes, "Value", "Text", request.AirRouteId); if (ModelState.IsValid) { if (!db.AirRoutes.Any(p => p.AirRouteId == request.AirRouteId)) { return(View(request)); } Flight flight = db.Flights.FirstOrDefault(p => p.PublicId == id); if (flight == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } DateTime start = DateTime.Parse(request.DepartureDate, new CultureInfo("pl-PL"), DateTimeStyles.NoCurrentDateDefault); DateTime finish = DateTime.Parse(request.ArrivalDate, new CultureInfo("pl-PL"), DateTimeStyles.NoCurrentDateDefault); flight.AirRouteId = request.AirRouteId; flight.AirRoute = db.AirRoutes.Find(request.AirRouteId); flight.ArrivalDate = finish; flight.DepartureDate = start; flight.NumberOfFreeSeats = request.NumberOfFreeSeats; flight.Price = request.Price; if (flight.DepartureDate >= flight.ArrivalDate) { ViewBag.Error = "Niezgodne daty"; return(View(request)); } db.Entry(flight).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(request)); } catch (Exception e) { var routes = db.AirRoutes.Select(x => new SelectListItem { Value = x.AirRouteId.ToString(), Text = x.AirLine.Name + " " + x.StartAirportCode + "-" + x.FinishAirportCode }); ViewBag.Error = "Błąd w formularzu!"; ViewBag.AirRouteId = new SelectList(routes, "Value", "Text", request.AirRouteId); return(View(request)); } }
public ActionResult Create([Bind(Include = "AirRouteId,NumberOfFreeSeats,DepartureDate,ArrivalDate,Price")] FlightFormModel request) { try { var routes = db.AirRoutes.Select(x => new SelectListItem { Value = x.AirRouteId.ToString(), Text = x.AirLine.Name + " " + x.StartAirportCode + "-" + x.FinishAirportCode }); ViewBag.AirRouteId = new SelectList(routes, "Value", "Text"); if (ModelState.IsValid) { if (!db.AirRoutes.Any(p => p.AirRouteId == request.AirRouteId)) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } DateTime start = DateTime.Parse(request.DepartureDate, new CultureInfo("pl-PL"), DateTimeStyles.NoCurrentDateDefault); DateTime finish = DateTime.Parse(request.ArrivalDate, new CultureInfo("pl-PL"), DateTimeStyles.NoCurrentDateDefault); Flight flight = new Flight() { PublicId = Guid.NewGuid(), AirRouteId = request.AirRouteId, AirRoute = db.AirRoutes.Find(request.AirRouteId), NumberOfFreeSeats = request.NumberOfFreeSeats, DepartureDate = start, ArrivalDate = finish, Price = request.Price }; if (flight.DepartureDate >= flight.ArrivalDate || flight.DepartureDate < DateTime.Now || flight.ArrivalDate < DateTime.Now) { ViewBag.Error = "Niezgodne daty"; return(View(request)); } db.Flights.Add(flight); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(request)); } catch { var routes = db.AirRoutes.Select(x => new SelectListItem { Value = x.AirRouteId.ToString(), Text = x.AirLine.Name + " " + x.StartAirportCode + "-" + x.FinishAirportCode }); ViewBag.AirRouteId = new SelectList(routes, "Value", "Text"); return(View(request)); } }
// GET: Flights/Edit/5 public ActionResult Edit(Guid id) { if (id == Guid.Empty) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Flight flight = db.Flights.FirstOrDefault(p => p.PublicId == id); if (flight == null) { return(HttpNotFound()); } FlightFormModel model = new FlightFormModel() { AirRouteId = flight.AirRouteId, NumberOfFreeSeats = flight.NumberOfFreeSeats, DepartureDate = flight.DepartureDate.ToString("yyyy-MM-dd hh:mm"), ArrivalDate = flight.ArrivalDate.ToString("yyyy-MM-dd hh:mm"), Price = flight.Price }; var routes = db.AirRoutes.Select(x => new SelectListItem { Value = x.AirRouteId.ToString(), Text = x.AirLine.Name + " " + x.StartAirportCode + "-" + x.FinishAirportCode }); ViewBag.AirRouteId = new SelectList(routes, "Value", "Text", model.AirRouteId); return(View(model)); }
public async Task <ActionResult <Application> > FlightSearch(FlightFormModel viewModel) { var flightData = new Application(); try { flightData = await _postManServices.GetFlightDetails(viewModel); } catch (Exception ex) { throw ex; } return(PartialView("ResponseView", flightData)); }
/// <summary> /// This method is used to get the response from the main service /// </summary> /// <param name="task"></param> /// <returns></returns> public async Task <Application> GetFlightDetails(FlightFormModel task) { var BaseUrl = _appSettings.Value.TangoServiceURL; var content = JsonConvert.SerializeObject(task); var httpResponse = await _client.PostAsync(BaseUrl, new StringContent(content, Encoding.Default, "application/json")); if (!httpResponse.IsSuccessStatusCode) { throw new Exception("Error on server side"); } var createdTask = JsonConvert.DeserializeObject <Application>(await httpResponse.Content.ReadAsStringAsync()); return(createdTask); }