1
Fork 0
mirror of https://github.com/RGBCube/GDUS synced 2025-07-29 05:57:45 +00:00

Add basic db logic

This commit is contained in:
RGBCube 2023-11-24 15:22:55 +03:00
parent 5732a54f7f
commit 6fb17a18c6
No known key found for this signature in database
4 changed files with 64 additions and 4 deletions

14
Cargo.lock generated
View file

@ -1589,6 +1589,8 @@ dependencies = [
"smallvec",
"sqlformat",
"thiserror",
"tokio",
"tokio-stream",
"tracing",
"url",
]
@ -1628,6 +1630,7 @@ dependencies = [
"sqlx-sqlite",
"syn 1.0.109",
"tempfile",
"tokio",
"url",
]
@ -1881,6 +1884,17 @@ dependencies = [
"syn 2.0.39",
]
[[package]]
name = "tokio-stream"
version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "397c988d37662c7dda6d2208364a706264bf3d6138b11d436cbac0ad38832842"
dependencies = [
"futures-core",
"pin-project-lite",
"tokio",
]
[[package]]
name = "tokio-util"
version = "0.7.10"

View file

@ -11,7 +11,7 @@ edition = "2021"
actix-web = "4.4.0"
maud = { version = "0.25.0", features = [ "actix-web" ] }
serde = { version = "1.0.192", features = [ "derive" ] }
sqlx = { version = "0.7.3", features = ["sqlite"] }
sqlx = { version = "0.7.3", features = ["sqlite", "runtime-tokio"] }
tokio = { version = "1.34.0", features = [ "full" ] }
[profile.dev]

View file

@ -4,11 +4,32 @@ mod submit;
use std::io;
use actix_web as web;
use sqlx::{
sqlite::SqliteConnectOptions,
SqlitePool,
};
#[web::main]
async fn main() -> io::Result<()> {
web::HttpServer::new(|| {
let db = SqlitePool::connect_with(SqliteConnectOptions::new().filename("data.db"))
.await
.unwrap();
sqlx::query(
r"
CREATE TABLE IF NOT EXISTS reminders (
date TEXT,
message TEXT
)
",
)
.execute(&db)
.await
.unwrap();
web::HttpServer::new(move || {
web::App::new()
.app_data(web::web::Data::new(db.clone()))
.service(index::index)
.service(submit::submit)
.service(submit::submit_form)

View file

@ -1,10 +1,14 @@
use actix_web as web;
use actix_web::web::Query;
use actix_web::web::{
Data,
Query,
};
use maud::{
html,
Markup,
DOCTYPE,
};
use sqlx::SqlitePool;
#[derive(Debug, serde::Deserialize)]
pub struct Reminder {
@ -13,12 +17,33 @@ pub struct Reminder {
}
#[web::get("/submit-form")]
async fn submit_form(Query(reminder): Query<Reminder>) -> web::Result<Markup> {
async fn submit_form(
data: Data<SqlitePool>,
Query(reminder): Query<Reminder>,
) -> web::Result<Markup> {
println!("{reminder:?}");
sqlx::query(
r"
INSERT INTO
reminders (date, message)
VALUES
(?, ?)
",
)
.bind(reminder.date)
.bind(reminder.message)
.execute(&**data)
.await
.expect("Failed to save reminder.");
Ok(html! {
(DOCTYPE)
h1 { "Kaydedildi." }
p { "Ana sayfaya geri yönlendiriliyorsun..." }
script type="text/javascript" {r#"
setTimeout(() => window.location.href = "/", 5000);
"#}
})
}