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/Delete.cshtml.cs

65 lines
2 KiB
C#
Raw Normal View History

using System.Linq;
2020-06-11 20:29:16 +02:00
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 DeleteModel : PageModel {
public User AuthorizedUser;
2020-06-11 20:29:16 +02:00
2022-05-03 00:20:02 +02:00
public void OnGet() {
if (!Request.Query.ContainsKey("confirm") || Request.Query["confirm"] != "true")
return;
2020-06-11 20:29:16 +02:00
2022-05-03 00:20:02 +02:00
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
var id = int.Parse(Request.Query["id"]);
2022-05-03 00:20:02 +02:00
switch (Request.Query["item"]) {
case "trip": {
var trip = db.Trips.First(p => p.TripId == id);
if (trip.UserId != AuthorizedUser!.UserId)
2022-05-03 00:20:02 +02:00
return;
2022-05-03 00:20:02 +02:00
db.Trips.Delete(p => p.TripId == id);
db.Legs.Delete(p => p.TripId == id);
break;
}
case "leg": {
var leg = db.Legs.First(p => p.LegId == id);
if (leg.UserId != AuthorizedUser!.UserId)
2022-05-03 00:20:02 +02:00
return;
var tripid = db.Legs.First(p => p.LegId == id).TripId;
2020-06-13 07:30:56 +02:00
2022-05-03 00:20:02 +02:00
db.Legs.Delete(p => p.LegId == id);
2020-06-13 07:30:56 +02:00
2022-05-03 00:20:02 +02:00
if (!db.Legs.Any(p => p.TripId == tripid))
db.Trips.Delete(p => p.TripId == tripid);
else if (db.Legs.Where(p => p.TripId == tripid).OrderBy(p => p.DepTime).First().DepTime.EndsWith("placeholder"))
db.Delete(db.Legs.Where(p => p.TripId == tripid).OrderBy(p => p.DepTime).First());
else if (db.Legs.Where(p => p.TripId == tripid).OrderByDescending(p => p.DepTime).First().DepTime.EndsWith("placeholder"))
db.Delete(db.Legs.Where(p => p.TripId == tripid).OrderByDescending(p => p.DepTime).First());
2021-08-24 20:56:52 +02:00
2022-05-03 00:20:02 +02:00
var failsafe = false;
foreach (var failsafeleg in db.Legs.Where(p => p.TripId == tripid).OrderBy(p => p.DepTime).ToList())
if (failsafeleg.DepTime.EndsWith("placeholder")) {
if (failsafe == false)
failsafe = true;
else
db.Legs.Delete(p => p.LegId == failsafeleg.LegId);
}
else {
failsafe = false;
2021-08-24 20:56:52 +02:00
}
2022-05-03 00:20:02 +02:00
break;
}
2020-06-11 20:29:16 +02:00
}
}
}