Telegram.Bot.SpaceApi/Pages/Edit.cshtml.cs
2023-04-08 14:58:43 +02:00

37 lines
1.1 KiB
C#

using Microsoft.AspNetCore.Mvc.RazorPages;
using Telegram.Bot.SpaceApi.Backend;
using Telegram.Bot.SpaceApi.Backend.Database;
using Telegram.Bot.SpaceApi.Backend.Utils;
namespace Telegram.Bot.SpaceApi.Pages;
public class EditModel : PageModel {
public void OnGet() { }
public async void OnPost() {
Response.Redirect("/");
await using var db = new DatabaseContext();
var bot = db.Bots.First(p => p.Id == int.Parse(Request.RouteValues["id"]!.ToString()!));
if (Request.Form["action"] == "edit_bot") {
BotOrchestrator.DelBot(bot);
bot.Name = Request.Form["bot_name"]!;
bot.Token = Request.Form["bot_token"]!;
bot.Chat = Request.Form["bot_chat"]!;
bot.ApiUrl = Request.Form["api_url"]!;
if (Request.Form.ContainsKey("api_override_address") && !string.IsNullOrWhiteSpace(Request.Form["api_override_address"])) {
bot.ApiOverrideAddress = Request.Form["api_override_address"]!;
}
else {
bot.ApiOverrideAddress = null;
}
await db.SaveChangesAsync();
await Task.WhenAny(Task.Run(() => BotOrchestrator.AddBot(bot)), Task.Delay(1000)).Result;
}
}
}