Started rocket backend

This commit is contained in:
Johannes Randerath 2024-08-10 20:24:26 +02:00
parent 07ae4c4118
commit e37a57ea81
13 changed files with 188 additions and 167 deletions

View File

@ -5,9 +5,13 @@ edition = "2021"
[dependencies] [dependencies]
rocket = "0.5.1" rocket = "0.5.1"
rocket_db_pools = "0.2.0" diesel = "2"
[dependencies.rocket_dyn_templates] [dependencies.rocket_dyn_templates]
version = "0.2.0" version = "0.2.0"
features = ["tera"] features = ["tera"]
[dependencies.rocket_db_pools]
version = "0.2.0"
features = ["diesel_mysql"]

View File

@ -1,2 +1,5 @@
[default] [default]
template_dir = "templates" template_dir = "templates"
[default.databases.inventur]
url = "mysql://inventur:inventur@localhost/inventur?socket=/var/lib/mysql/mysql.sock"

9
diesel.toml Normal file
View File

@ -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"

0
migrations/.keep Normal file
View File

View File

@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
DROP TABLE users;

View File

@ -0,0 +1,6 @@
-- Your SQL goes here
CREATE TABLE users (
id INT PRIMARY KEY,
email VARCHAR(64) NOT NULL,
name VARCHAR(255)
);

View File

@ -3,14 +3,100 @@
use rocket::fs::{FileServer, relative}; use rocket::fs::{FileServer, relative};
use rocket_dyn_templates::{Template, context}; 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/<tname>")] #[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<String>,
}
#[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("/<tname>")]
fn table(tname: &str) -> Template { 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="<data>")]
fn create(data: Form<New_table<'_>>) {
//println!("{:?}", data);
}
#[derive(FromForm)]
struct Args <'r> {
value: &'r str,
}
#[get("/table/<tname>?filter&<column>&<mode>&<args..>")]
async fn filter(db:Connection<Db>, tname: &str, column: &str, mode: &str, args: Args<'_>) -> &'static str {
todo!()
} }
#[get("/")] #[get("/")]
fn index() -> Template { fn index(db: Connection<Db>) -> Template {
Template::render("test", context!{foo: 123,}) Template::render("test", context!{foo: 123,})
} }
@ -18,6 +104,9 @@ fn index() -> Template {
fn rocket() -> _ { fn rocket() -> _ {
rocket::build() rocket::build()
.attach(Template::fairing()) .attach(Template::fairing())
.attach(Db::init())
.mount("/", FileServer::from(relative!("static"))) .mount("/", FileServer::from(relative!("static")))
.mount("/", routes![index, table]) .mount("/", routes![index])
.mount("/table", routes![create, table])
//connect_db();
} }

0
src/models.rs Normal file
View File

11
src/schema.rs Normal file
View File

@ -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<Varchar>,
}
}

View File

@ -1,4 +1,4 @@
{# vim: set filetype=html #} {# vim: set filetype=html :#}
<!DOCTYPE html> <!DOCTYPE html>
<!-- <!--
This file is part of Inventory. This file is part of Inventory.
@ -17,14 +17,6 @@
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>Inventory - {% block pagename %}{% endblock pagename %}</title> <title>Inventory - {% block pagename %}{% endblock pagename %}</title>
<script type="text/javascript">
let caller = "";
document.getElementsByClassName('mdl').forEach((btn) => {
btn.addEventListener('click', (event) => {
caller = btn.id;
});
});
</script>
{% block script %}{% endblock script %} {% block script %}{% endblock script %}
</head> </head>
@ -68,10 +60,10 @@
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div> </div>
<div class="modal-body"> <div class="modal-body">
<form> <form id="create-table" method="post">
<div class="mb-3"> <div class="mb-3">
<label for="new_table_name" class="form-label">Name</label> <label for="new_table_name" class="form-label">Name</label>
<input type="text" class="form-control" id="new_table_name" aria-describedby="new_table_name_label"> <input type="text" class="form-control" id="name" aria-describedby="new_table_name_label">
<div id="new_table_name_label" class="form-text">Name of the new table</div> <div id="new_table_name_label" class="form-text">Name of the new table</div>
</div> </div>
<div class="mb-3"> <div class="mb-3">
@ -81,7 +73,7 @@
<div class="form-text" id="field_index"><strong>1</strong></div> <div class="form-text" id="field_index"><strong>1</strong></div>
</div> </div>
<div class="col-auto"> <div class="col-auto">
<label for="field_name" class="form-label">Column name</label> <label for="fields" class="form-label">Column name</label>
<input type="text" class="form-control" id="field_name" aria-describedby="field_name_label"> <input type="text" class="form-control" id="field_name" aria-describedby="field_name_label">
</div> </div>
<div class="col-auto"> <div class="col-auto">
@ -172,7 +164,7 @@
<div class="modal-dialog"> <div class="modal-dialog">
<div class="modal-content"> <div class="modal-content">
<div class="modal-header"> <div class="modal-header">
<h1 class="modal-title fs-5">Edit filter on <div id="modal_caller"></div></h1> <h1 class="modal-title fs-5">Edit filter on <span id="modal_caller"></span></h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div> </div>
<div class="modal-body"> <div class="modal-body">

View File

@ -1,147 +0,0 @@
<!DOCTYPE html>
<html>
<head lang="en">
<meta name="author" content="Johannes Randerath" />
<meta charset="utf-8" />
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Inventory</title>
</head>
<body>
<nav class='navbar navbar-expand-lg bg-body bg-body-tertiary'>
<div class='container-fluid'>
<a class='navbar-brand' href='#'>Inventory</a>
<button class='navbar-toggler' type='button' data-bs-toggle='collapse' data-bs-target='#navbarSupportedContent' aria-controls='navbarSupportedContent' aria-expanded='false' aria-label='Toggle navigation'>
<span class='navbar-toggler-icon'></span>
</button>
<div class='collapse navbar-collapse' id='navbarSupportedContent'>
<ul class='navbar-nav me-auto mb-2 mb-lg-0'>
<li class='nav-item dropdown'>
<a class='nav-link dropdown-toggle' role='button' data-bs-toggle='dropdown' aria-expanded='false'>
Choose Table
</a>
</li>
<li class="nav-item">
<a class="nav-link" role="button" data-bs-toggle="modal" data-bs-target="#new_table">
<i class="bi bi-plus-lg"></i>
</a>
</li>
<li class="nav-item">
<a class="nav-link" role="button" data-bs-toggle="modal" data-bs-target="#import_table">
<i class="bi bi-folder2-open"></i>
</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="accordion" id="my_tables">
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#first" aria-expanded="false" aria-controls="first">
First
</button>
</h2>
<div id="first" class="accordion-collapse collapse" data-bs-parent="#my_tables">
<div class="accordion-body">
<h3>Name of first table <a class="btn btn-primary" href="table.html">Open</a></h3>
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>name</th>
<th>...</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>one</td>
<td>...</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- Modals -->
<!-- Create new table -->
<div class="modal fade" id="new_table" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5">New table</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form>
<div class="mb-3">
<label for="new_table_name" class="form-label">Name</label>
<input type="text" class="form-control" id="new_table_name" aria-describedby="new_table_name_label">
<div id="new_table_name_label" class="form-text">Name of the new table</div>
</div>
<div class="mb-3">
<div class="row" id="columns">
<div class="col-auto align-bottom me-1">
<label for="field_index" class="form-label">#</label>
<div class="form-text" id="field_index"><strong>1</strong></div>
</div>
<div class="col-auto">
<label for="field_name" class="form-label">Column name</label>
<input type="text" class="form-control" id="field_name" aria-describedby="field_name_label">
</div>
<div class="col-auto">
<label for="field_name" class="form-label">Data type</label>
<select class="form-select" aria-label="data type">
<option value="text" selected>Text</option>
<option value="number">Number</option>
<option value="date">Date</option>
</select>
</div>
</div>
<div id="dynamic_columns">
<!-- Columns added using plus button -->
</div>
<div class="row justify-content-center mb-3 mt-3">
<div class="col-auto">
<button class="btn btn-primary" type="button" onclick="add_column()"><i class="bi bi-plus-lg"></i></button>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button class="btn btn-secondary" data-bs-dismiss="modal" type="button">Close</button>
<button class="btn btn-primary" type="button" onclick="create_table()">Create</button>
</form>
</div>
</div>
</div>
</div>
<!-- Import new table -->
<div class="modal fade" id="import_table" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5">Import new table</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button class="btn btn-secondary" data-bs-dismiss="modal" type="button">Close</button>
<button class="btn btn-primary" type="button">Import</button>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
</body>
</html>

36
templates/home.html.tera Normal file
View File

@ -0,0 +1,36 @@
{# vim: set filetype=html: #}
{% extends "base" %}
{% block body %}
<div class="accordion" id="my_tables">
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#first" aria-expanded="false" aria-controls="first">
First
</button>
</h2>
<div id="first" class="accordion-collapse collapse" data-bs-parent="#my_tables">
<div class="accordion-body">
<h3>Name of first table <a class="btn btn-primary" href="table.html">Open</a></h3>
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>name</th>
<th>...</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>one</td>
<td>...</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
{% endblock body %}

View File

@ -5,6 +5,7 @@
{% block script %} {% block script %}
<script type="text/javascript"> <script type="text/javascript">
let ncol = 1; let ncol = 1;
let edit_mode = false;
document.addEventListener("DOMContentLoaded", (event) => { document.addEventListener("DOMContentLoaded", (event) => {
for (let row of document.getElementById("content_table").getElementsByTagName('tbody')[0].rows) { for (let row of document.getElementById("content_table").getElementsByTagName('tbody')[0].rows) {
let createClickHandler = function(r) { let createClickHandler = function(r) {
@ -20,15 +21,30 @@
document.getElementById('dynamic_columns').innerHTML += "<div class='row mt-3' id='columns'><div class='col-auto align-bottom'><div class='form-text me-1' id='field_index'><strong>" + ncol + "</strong></div></div><div class='col-auto'><input type='text' class='form-control' id='field_name' aria-describedby='field_name_label'></div><div class='col-auto'><select class='form-select' aria-label='data type'><option value='text' selected>Text</option><option value='number'>Number</option><option value='date'>Date</option></select></div></div>" document.getElementById('dynamic_columns').innerHTML += "<div class='row mt-3' id='columns'><div class='col-auto align-bottom'><div class='form-text me-1' id='field_index'><strong>" + ncol + "</strong></div></div><div class='col-auto'><input type='text' class='form-control' id='field_name' aria-describedby='field_name_label'></div><div class='col-auto'><select class='form-select' aria-label='data type'><option value='text' selected>Text</option><option value='number'>Number</option><option value='date'>Date</option></select></div></div>"
} }
function create_table() { function create_table() {
let form = document.getElementById('create-table');
form.submit();
document.getElementById('dynamic_columns').innerHTML = ""; document.getElementById('dynamic_columns').innerHTML = "";
ncol = 1; ncol = 1;
} }
function set_modal_target(modal_target) {
document.getElementById('modal_caller').innerHTML = modal_target;
}
function toggle_edit_tname() {
let span = document.getElementById('tname');
if (!edit_mode) {
span.innerHTML = "<form><input type='text' class='form-control' id='tname_edit' value='{{ tname }}' /><button class='btn btn-success' type='submit'><i class='bi bi-check'></i></button></form>";
edit_mode = true;
} else {
span.innerHTML = '{{ tname }}';
edit_mode = false;
}
}
</script> </script>
{% endblock script %} {% endblock script %}
{% block body %} {% block body %}
<div class="row justify-content-between"> <div class="row justify-content-between">
<div class="col-auto"> <div class="col-auto">
<h1>Inventory table <button class="btn" type="button"><i class="bi bi-pencil"></i></button></h1> <h1><div class='input-group'><span id="tname">{{ tname }}</span><button class="btn" type="button" onclick="toggle_edit_tname();"><i class="bi bi-pencil"></i></button></div></h1>
</div> </div>
<div class="col-auto"> <div class="col-auto">
<div class="btn-toolbar mt-2" role="toolbar"> <div class="btn-toolbar mt-2" role="toolbar">
@ -76,7 +92,7 @@
</div> </div>
<div class="col-auto"> <div class="col-auto">
<div class="btn-toolbar"> <div class="btn-toolbar">
<button class="btn p-0 me-2" type="button" data-jr-column="{{ column }}" data-bs-toggle="modal" data-bs-target="#filter"><i class="bi bi-filter"></i></button> <button class="btn p-0 me-2" type="button" data-bs-toggle="modal" data-bs-target="#filter" onclick="set_modal_target('{{ column }}');"><i class="bi bi-filter"></i></button>
<button class="btn p-0" type="button" onclick="console.log('click');"><i class="bi bi-sort-down"></i></button> <button class="btn p-0" type="button" onclick="console.log('click');"><i class="bi bi-sort-down"></i></button>
</div> </div>
</div> </div>