finished implementing setting category

This commit is contained in:
ide
2026-08-17 22:47:02 +02:00
parent b3dd373403
commit 495b0de7a7
8 changed files with 1712 additions and 1176 deletions
+1
View File
@@ -9,3 +9,4 @@ serde = { version = "1.0.228", features = ["derive"] }
serde_json = "1.0.145"
inkopslista-lib = { path = "../lib" }
sqlite = "0.37.0"
clap = { version = "4.6.6", features = ["derive", "env"] }
+14 -8
View File
@@ -1,8 +1,7 @@
#[macro_use]
extern crate rocket;
use std::{env, path::Path};
use clap::Parser;
use inkopslista_lib::{ButtonItem, Item, ItemData};
use rocket::{
fs::{FileServer, NamedFile},
@@ -11,6 +10,7 @@ use rocket::{
State,
};
use sqlite::ConnectionThreadSafe;
use std::path::{Path, PathBuf};
/// The directory where the web files for the gui is stored.
fn www_path() -> &'static Path {
@@ -53,7 +53,7 @@ fn get_favorites(db: &State<ConnectionThreadSafe>) -> Json<Vec<ButtonItem>> {
FROM adds
WHERE (date(julianday('now')) - date(date_added, 'unixepoch')) < 184
GROUP BY itemname
HAVING day_count > 0)
HAVING day_count > 0)
SELECT favorites.itemname, category FROM favorites LEFT OUTER JOIN categories ON favorites.itemname = categories.itemname";
let list: Vec<ButtonItem> = db
.prepare(query)
@@ -73,8 +73,8 @@ fn get_favorites(db: &State<ConnectionThreadSafe>) -> Json<Vec<ButtonItem>> {
Json(list)
}
#[put("/api/favourites/<itemname>", data = "<category>")]
fn set_categories(db: &State<ConnectionThreadSafe>, itemname: &str, category: &str) {
#[put("/api/favourites/<itemname>/category", data = "<category>")]
fn put_category(db: &State<ConnectionThreadSafe>, itemname: &str, category: &str) {
let query = "INSERT INTO categories VALUES (?, ?) ON CONFLICT DO UPDATE SET category = ?";
let mut statement = db.prepare(query).unwrap();
statement.bind((1, itemname)).unwrap();
@@ -111,10 +111,16 @@ fn delete_item(item: &str, db: &State<ConnectionThreadSafe>) {
while let Ok(sqlite::State::Row) = statement.next() {}
}
#[derive(Parser)]
struct Opt {
#[clap(long, env = "DB_PATH")]
db_path: PathBuf,
}
#[launch]
fn rocket() -> _ {
let db_path = env::var("DB_PATH").unwrap();
let db = sqlite::Connection::open_thread_safe(db_path).unwrap();
let opt = Opt::parse();
let db = sqlite::Connection::open_thread_safe(opt.db_path).unwrap();
db.execute(include_str!("db/init.sql"))
.expect("unable to initialize database");
rocket::build()
@@ -132,7 +138,7 @@ fn rocket() -> _ {
put_list,
delete_item,
get_favorites,
set_categories
put_category
],
)
.mount("/", FileServer::from(www_path()))