using MySqlConnector; using System.Security.Cryptography; using System.Text; var SK_DB = "db-cred"; var SK_LOGGED = "logged-in"; var builder = WebApplication.CreateBuilder(new WebApplicationOptions { Args = args, WebRootPath = "frontend/dist" }); builder.Services.AddDistributedMemoryCache(); builder.Services.AddSession(options => { options.Cookie.HttpOnly = true; options.Cookie.IsEssential = true; }); var app = builder.Build(); var connection = new MySqlConnection(); app.UseStaticFiles(); app.UseRouting(); app.UseSession(); app.MapPost("/login", (LoginForm lgf, HttpResponse response, HttpContext context) => { var pass = hashPassword(lgf.password!); connection = new MySqlConnection(context.Session.GetString(SK_DB)); connection.Open(); var command = new MySqlCommand(null, connection); if (lgf.mode == "login") { command.CommandText = "SELECT * FROM user WHERE login = @login AND password = @password;"; command.Parameters.AddWithValue("@login", lgf.login); command.Parameters.AddWithValue("@password", pass); var reader = command.ExecuteReader(); while (reader.Read()) { context.Session.SetString(SK_LOGGED, "true"); connection.Close(); return "ok"; } } else { Console.WriteLine("registering"); command.CommandText = "INSERT INTO user (email, login, password) VALUES (@email, @login, @password)"; command.Parameters.AddWithValue("@login", lgf.login); command.Parameters.AddWithValue("@password", pass); command.Parameters.AddWithValue("@email", lgf.email); command.ExecuteNonQuery(); connection.Close(); return "ok"; } connection.Close(); return "error"; }); app.MapPost("/check-db", async (DbForm dbf, HttpResponse response, HttpContext context) => { try { var connstr = $"server={dbf.server};port={dbf.port};user={dbf.user};password={dbf.password};database={dbf.database}"; var conn = new MySqlConnection(connstr); Console.WriteLine(connstr); await conn.OpenAsync(); connection = conn; await conn.CloseAsync(); context.Session.SetString(SK_DB, connstr); return "ok"; } catch (Exception _) { return "error"; } }); app.MapMethods("/check-db", new[] { "OPTIONS" }, (HttpResponse response) => { response.Headers.AccessControlAllowOrigin = "*"; response.Headers.AccessControlAllowHeaders = "*"; return "ok"; }); app.MapGet("/", (HttpContext context) => { context.Response.SendFileAsync("./frontend/dist/index.html"); }); app.MapPost("/book", (Book book, HttpContext context) => { if (!checkRequest(context)) return "error"; connection.Open(); var command = new MySqlCommand(null, connection); if (book.id == null) { command.CommandText = "INSERT INTO book (author, title, year, publisher, isbn, format, pages, description) VALUES (@author, @title, @year, @publisher, @isbn, @format, @pages, @description)"; } else { command.CommandText = "UPDATE book SET author = @author, title = @title, year = @year, publisher = @publisher, isbn = @isbn, format = @format, pages = @pages, description = @description WHERE id = @id"; command.Parameters.AddWithValue("@id", book.id); } command.Parameters.AddWithValue("@author", book.author); command.Parameters.AddWithValue("@title", book.title); command.Parameters.AddWithValue("@year", MySqlDbType.Int32).Value = book.year; command.Parameters.AddWithValue("@publisher", book.publisher); command.Parameters.AddWithValue("@isbn", book.isbn); command.Parameters.AddWithValue("@format", book.format); command.Parameters.AddWithValue("@pages", book.pages); command.Parameters.AddWithValue("@description", book.description); command.ExecuteNonQuery(); connection.Close(); return "ok"; }); app.MapGet("/books", (HttpContext context) => { if (!checkRequest(context)) return Results.Ok("error"); connection.Open(); var command = new MySqlCommand("SELECT * FROM book;", connection); var reader = command.ExecuteReader(); List> books = new List>(); while (reader.Read()) { Dictionary book = new Dictionary(); book.Add("id", reader.GetValue(0).ToString() ?? ""); book.Add("author", reader.GetValue(1).ToString() ?? ""); book.Add("title", reader.GetValue(2).ToString() ?? ""); book.Add("year", reader.GetValue(3).ToString() ?? ""); book.Add("publisher", reader.GetValue(4).ToString() ?? ""); book.Add("isbn", reader.GetValue(5).ToString() ?? ""); book.Add("format", reader.GetValue(6).ToString() ?? ""); book.Add("pages", reader.GetValue(7).ToString() ?? ""); book.Add("description", reader.GetValue(8).ToString() ?? ""); books.Add(book); } connection.Close(); return Results.Json(books); }); // Remove book app.MapDelete("/book", (HttpContext context) => { if (!checkRequest(context)) return Results.Unauthorized(); connection.Open(); var command = new MySqlCommand(null, connection); command.CommandText = "DELETE FROM book WHERE id = @id;"; command.Parameters.AddWithValue("@id", MySqlDbType.Int32).Value = Int32.Parse(context.Request.Query["bid"]); command.ExecuteNonQuery(); connection.Close(); return Results.Ok("ok"); }); app.Run("http://localhost:8000"); String hashPassword(string pass) { var sha256 = SHA256.Create(); return BitConverter.ToString(sha256.ComputeHash(Encoding.UTF8.GetBytes(pass!))); } Boolean checkRequest(HttpContext context) { return context.Session.GetString(SK_LOGGED) == "true"; } class DbForm { public string? server { get; set; } public int port { get; set; } public string? database { get; set; } public string? user { get; set; } public string? password { get; set; } } class LoginForm { public string? email { get; set; } public string? login { get; set; } public string? password { get; set; } public string? mode { get; set; } } class Book { public int? id { get; set; } public string? author { get; set; } public string? title { get; set; } public int? year { get; set; } public string? publisher { get; set; } public string? isbn { get; set; } public string? format { get; set; } public int? pages { get; set; } public string? description { get; set; } }