This repository has been archived on 2023-04-02. You can view files and clone it, but cannot push or open issues or pull requests.
trainav/trainav.web/Pages/Trip.cshtml.cs

67 lines
1.8 KiB
C#
Raw Permalink Normal View History

2020-06-11 20:29:16 +02:00
using System.Collections.Generic;
using System.Linq;
using LinqToDB;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.RazorPages;
2022-04-29 14:59:24 +02:00
using trainav.web.database;
using trainav.web.database.Tables;
using trainav.web.Utils;
2020-06-11 20:29:16 +02:00
2022-05-03 00:20:02 +02:00
namespace trainav.web.Pages;
2020-06-11 20:29:16 +02:00
2022-05-03 00:20:02 +02:00
public class TripModel : PageModel {
public List<Leg> Legs;
public bool RedirToIndex;
public User AuthorizedUser;
2022-05-03 00:20:02 +02:00
public new string User;
2022-05-03 00:20:02 +02:00
public void OnGet() {
using var db = new Database.DbConn();
AuthorizedUser = db.Users.FirstOrDefault(p => p.Username == AuthUtil.GetRemoteUser(HttpContext, db));
2022-05-03 00:20:02 +02:00
if (Request.Query.ContainsKey("separator")) {
var leg = db.Legs.First(p => p.LegId == int.Parse(Request.Query["legid"]));
if (leg.UserId != AuthorizedUser!.UserId)
2020-06-11 20:29:16 +02:00
return;
2022-05-03 00:20:02 +02:00
db.Insert(new Leg {
TripId = int.Parse(Request.Query["id"]),
UserId = AuthorizedUser.UserId,
2022-05-03 00:20:02 +02:00
TrainType = "placeholder",
TrainNr = int.Parse(Request.Query["legid"]),
ArrStation = "_",
ArrStationId = 0,
ArrTime = "_",
DepStation = "_",
DepStationId = 0,
DepTime = leg.DepTime + "_placeholder"
});
return;
}
2020-10-19 01:55:36 +02:00
2022-05-03 00:20:02 +02:00
Legs = db.Legs.Where(p => p.TripId == int.Parse(Request.Query["id"])).OrderBy(p => p.DepTime).ToList();
2020-06-13 07:30:56 +02:00
2022-05-03 00:20:02 +02:00
if (!db.Legs.Any(p => p.TripId == int.Parse(Request.Query["id"]))) {
RedirToIndex = true;
return;
2020-06-11 20:29:16 +02:00
}
2022-05-03 00:20:02 +02:00
User = db.Users.First(p => p.UserId == Legs.First().UserId).Username;
}
2022-05-03 00:20:02 +02:00
public void OnPost() {
using var db = new Database.DbConn();
if (!Request.Form.ContainsKey("comment"))
return;
2022-05-21 13:33:16 +02:00
AuthorizedUser = db.Users.FirstOrDefault(p => p.Username == AuthUtil.GetRemoteUser(HttpContext, db));
2022-05-03 00:20:02 +02:00
var leg = db.Legs.First(p => p.LegId == int.Parse(Request.Form["id"]));
2022-05-21 13:33:16 +02:00
if (leg.UserId != AuthorizedUser!.UserId)
2022-05-03 00:20:02 +02:00
return;
2022-05-03 00:20:02 +02:00
leg.Comment = Request.Form["comment"];
db.Update(leg);
2020-06-11 20:29:16 +02:00
}
}