2020-06-11 20:29:16 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using bahnplan.web.database;
|
|
|
|
|
using bahnplan.web.database.Tables;
|
|
|
|
|
using bahnplan.web.JSON;
|
|
|
|
|
using LinqToDB;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
|
|
|
using Leg = bahnplan.web.database.Tables.Leg;
|
|
|
|
|
|
|
|
|
|
// ReSharper disable PossibleInvalidOperationException
|
|
|
|
|
|
|
|
|
|
namespace bahnplan.web.Pages {
|
|
|
|
|
public class OEAPIModel : PageModel {
|
|
|
|
|
public int TripId;
|
|
|
|
|
|
|
|
|
|
public void OnGet() {
|
2020-06-12 00:37:55 +02:00
|
|
|
|
if (HttpContext.Session.GetString("authorized") != "true")
|
2020-06-11 20:29:16 +02:00
|
|
|
|
return;
|
2020-06-12 00:37:55 +02:00
|
|
|
|
|
2020-06-11 20:29:16 +02:00
|
|
|
|
var link = Request.Query["link"].ToString();
|
|
|
|
|
var shortcode = link;
|
|
|
|
|
if (link.Contains("oeffisear.ch"))
|
|
|
|
|
shortcode = link.Split("/#/").Last();
|
|
|
|
|
var jid = shortcode.Split("/").Last();
|
|
|
|
|
shortcode = shortcode.Split("/").First();
|
|
|
|
|
|
|
|
|
|
using var db = new Database.DbConn();
|
|
|
|
|
var response = new WebClient().DownloadString($"https://oeffisear.ch/journeys?{{\"reqId\":\"{shortcode}\"}}");
|
|
|
|
|
|
|
|
|
|
var parsed = OeapiResponse.FromJson(response);
|
|
|
|
|
|
2020-06-12 02:21:59 +02:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(Request.Query["tripid"].ToString()))
|
|
|
|
|
if (db.Trips.First(p => p.TripId == int.Parse(Request.Query["tripid"].ToString())).UserId != int.Parse(HttpContext.Session.GetString("uid")))
|
|
|
|
|
return;
|
|
|
|
|
|
2020-06-11 20:29:16 +02:00
|
|
|
|
var tripId = Request.Query["action"] == "addleg"
|
|
|
|
|
? int.Parse(Request.Query["tripid"])
|
|
|
|
|
: db.InsertWithInt32Identity(new Trip {UserId = int.Parse(HttpContext.Session.GetString("uid"))});
|
|
|
|
|
|
|
|
|
|
foreach (var journey in parsed.Data.Journeys[jid].Legs) {
|
|
|
|
|
var arrtime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
|
|
|
|
|
var deptime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
|
|
|
|
|
arrtime = arrtime.AddSeconds((long) journey.Arrival.PlannedTime).ToLocalTime();
|
|
|
|
|
deptime = deptime.AddSeconds((long) journey.Departure.PlannedTime).ToLocalTime();
|
|
|
|
|
db.InsertWithInt32Identity(new Leg {
|
|
|
|
|
TripId = tripId,
|
|
|
|
|
UserId = int.Parse(HttpContext.Session.GetString("uid")),
|
|
|
|
|
TrainType = journey.Line.ProductName,
|
|
|
|
|
TrainNr = int.Parse(journey.Line.TripNum),
|
|
|
|
|
ArrStation = journey.Arrival.Point.Stop.Name,
|
|
|
|
|
ArrStationId = int.Parse(journey.Arrival.Point.Stop.Id),
|
|
|
|
|
ArrTime = arrtime.ToString("yyyy-MM-ddTHH:mm:ss"),
|
|
|
|
|
DepStation = journey.Departure.Point.Stop.Name,
|
|
|
|
|
DepStationId = int.Parse(journey.Departure.Point.Stop.Id),
|
2020-06-12 00:37:55 +02:00
|
|
|
|
DepTime = deptime.ToString("yyyy-MM-ddTHH:mm:ss")
|
2020-06-11 20:29:16 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TripId = tripId;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|