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

namespace trainav.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;
			return;
		}

		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;
	}
}