using System.Collections.Generic;
using System.Linq;
using bahnplan.web.database;
using bahnplan.web.database.Tables;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace bahnplan.web.Pages {
	public class SharedTripModel : PageModel {
		public     List<Leg> Legs;
		public     bool      RedirToIndex;
		public new string    User;

		public void OnGet() {
			using var db = new Database.DbConn();

			if (!db.Legs.Any(p => p.TripId == int.Parse(Request.Query["id"])))
				RedirToIndex = true;

			Legs = db.Legs.Where(p => p.TripId == int.Parse(Request.Query["id"])).OrderBy(p => p.DepTime).ToList();
			User = db.Users.First(p => p.UserId == Legs.First().UserId).Username;
			if (Request.Query["user"] != User)
				RedirToIndex = true;
		}
	}
}