diff --git a/Cargo.toml b/Cargo.toml index 7b24e8d..acd920d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,9 +5,13 @@ edition = "2021" [dependencies] rocket = "0.5.1" -rocket_db_pools = "0.2.0" +diesel = "2" [dependencies.rocket_dyn_templates] version = "0.2.0" features = ["tera"] +[dependencies.rocket_db_pools] +version = "0.2.0" +features = ["diesel_mysql"] + diff --git a/Rocket.toml b/Rocket.toml index 219cb37..f4639e6 100644 --- a/Rocket.toml +++ b/Rocket.toml @@ -1,2 +1,5 @@ [default] template_dir = "templates" + +[default.databases.inventur] +url = "mysql://inventur:inventur@localhost/inventur?socket=/var/lib/mysql/mysql.sock" diff --git a/diesel.toml b/diesel.toml new file mode 100644 index 0000000..a04b848 --- /dev/null +++ b/diesel.toml @@ -0,0 +1,9 @@ +# For documentation on how to configure this file, +# see https://diesel.rs/guides/configuring-diesel-cli + +[print_schema] +file = "src/schema.rs" +custom_type_derives = ["diesel::query_builder::QueryId", "Clone"] + +[migrations_directory] +dir = "/home/user/code/inventur/migrations" diff --git a/migrations/.keep b/migrations/.keep new file mode 100644 index 0000000..e69de29 diff --git a/migrations/2024-08-09-133227_create_users/down.sql b/migrations/2024-08-09-133227_create_users/down.sql new file mode 100644 index 0000000..dc3714b --- /dev/null +++ b/migrations/2024-08-09-133227_create_users/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +DROP TABLE users; diff --git a/migrations/2024-08-09-133227_create_users/up.sql b/migrations/2024-08-09-133227_create_users/up.sql new file mode 100644 index 0000000..1e2245d --- /dev/null +++ b/migrations/2024-08-09-133227_create_users/up.sql @@ -0,0 +1,6 @@ +-- Your SQL goes here +CREATE TABLE users ( + id INT PRIMARY KEY, + email VARCHAR(64) NOT NULL, + name VARCHAR(255) +); diff --git a/src/main.rs b/src/main.rs index 5d63508..12eedd5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,14 +3,100 @@ use rocket::fs::{FileServer, relative}; use rocket_dyn_templates::{Template, context}; +use rocket_db_pools::{Database, Connection}; +use rocket_db_pools::diesel::{QueryResult, MysqlPool, prelude::*}; +use rocket::form::Form; +/*use dotenvy::dotenv; +use std::env;*/ -#[get("/table/")] +#[derive(Database)] +#[database("inventur")] +struct Db(MysqlPool); + +/*fn connect_db() -> MysqlConnection { + dotenv.ok(); + let database_url = env::var("DATABASE_URL").expect("Can't find database url"); + MysqlConnection::establish(&database_url).expect_or_else(|_| panic!("Couldn't connect to database {}.", database_url)); +}*/ + +struct Owner { + id: i64, + email: String, + name: Option, +} + +#[derive(Queryable, Insertable)] +#[diesel(table_name = users)] +struct User { + id: i64, + email: String, +} + +diesel::table! { + users (id) { + id -> BigInt, + email -> Text, + } +} + +#[derive(Queryable, Insertable)] +#[diesel(table_name = jrtables)] +struct JRTable { + id: i64, + name: String, + owner_id: i64, +} + +diesel::table! { + jrtables (id) { + id -> BigInt, + name -> Text, + owner_id -> BigInt, + } +} + +#[get("/")] fn table(tname: &str) -> Template { - Template::render("table", context!{tname: tname, columns: ["name", "djhfae", "fsjhr"], rows: [["1", "first", "hasdjf", "753rgf"], ["2", "second", "7438ued", "🚀"], ["3", "third", "", ""]] }) + let columns = ["name", "djhfae", "fsjhr"]; + let rows = [ + ["1", "first", "hasdjf", "753rgf"], + ["2", "second", "7438ued", "🚀"], + ["3", "third", "", ""] + ]; + Template::render("table", + context!{tname: tname, + columns: columns, + rows: rows, + } + ) +} + +#[derive(FromForm)] +struct New_table<'r> { + name: &'r str, + fields: Vec<&'r str>, + #[field(name = "done")] + complete: bool, +} + +#[post("/create", data="")] +fn create(data: Form>) { + //println!("{:?}", data); +} + + +#[derive(FromForm)] +struct Args <'r> { + value: &'r str, +} + +#[get("/table/?filter&&&")] +async fn filter(db:Connection, tname: &str, column: &str, mode: &str, args: Args<'_>) -> &'static str { + todo!() } #[get("/")] -fn index() -> Template { +fn index(db: Connection) -> Template { Template::render("test", context!{foo: 123,}) } @@ -18,6 +104,9 @@ fn index() -> Template { fn rocket() -> _ { rocket::build() .attach(Template::fairing()) + .attach(Db::init()) .mount("/", FileServer::from(relative!("static"))) - .mount("/", routes![index, table]) + .mount("/", routes![index]) + .mount("/table", routes![create, table]) + //connect_db(); } diff --git a/src/models.rs b/src/models.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/schema.rs b/src/schema.rs new file mode 100644 index 0000000..98a5eb5 --- /dev/null +++ b/src/schema.rs @@ -0,0 +1,11 @@ +// @generated automatically by Diesel CLI. + +diesel::table! { + users (id) { + id -> Integer, + #[max_length = 64] + email -> Varchar, + #[max_length = 255] + name -> Nullable, + } +} diff --git a/templates/base.html.tera b/templates/base.html.tera index 9eaea46..8920a68 100644 --- a/templates/base.html.tera +++ b/templates/base.html.tera @@ -1,4 +1,4 @@ -{# vim: set filetype=html #} +{# vim: set filetype=html :#} - - - - - - - - - diff --git a/templates/home.html.tera b/templates/home.html.tera new file mode 100644 index 0000000..1662a2c --- /dev/null +++ b/templates/home.html.tera @@ -0,0 +1,36 @@ +{# vim: set filetype=html: #} +{% extends "base" %} +{% block body %} +
+
+

+ +

+
+
+

Name of first table Open

+ + + + + + + + + + + + + + + +
#name...
1one...
+
+
+
+ +
+{% endblock body %} + diff --git a/templates/table.html.tera b/templates/table.html.tera index d88271a..b2281cd 100644 --- a/templates/table.html.tera +++ b/templates/table.html.tera @@ -5,6 +5,7 @@ {% block script %} {% endblock script %} {% block body %}
-

Inventory table

+

{{ tname }}

- +