idk what I did last

This commit is contained in:
Ide
2026-06-12 19:24:39 +02:00
parent c00e3205cd
commit b3dd373403
6 changed files with 147 additions and 13 deletions
+11 -1
View File
@@ -3,4 +3,14 @@ CREATE TABLE
itemname TEXT NOT NULL PRIMARY KEY,
checked INT NOT NULL, -- actually a boolean
amount INT NOT NULL
) STRICT;
) STRICT;
CREATE TABLE
IF NOT EXISTS adds (
itemname TEXT NOT NULL,
date_added INT NOT NULL,
PRIMARY KEY (itemname, date_added)
) STRICT;
CREATE TABLE
IF NOT EXISTS categories (itemname TEXT NOT NULL PRIMARY KEY, category TEXT) STRICT;
+52 -2
View File
@@ -3,7 +3,7 @@ extern crate rocket;
use std::{env, path::Path};
use inkopslista_lib::{Item, ItemData};
use inkopslista_lib::{ButtonItem, Item, ItemData};
use rocket::{
fs::{FileServer, NamedFile},
serde::json::Json,
@@ -47,6 +47,42 @@ fn get_list(db: &State<ConnectionThreadSafe>) -> Json<Vec<Item>> {
Json(list)
}
#[get("/api/favorites")]
fn get_favorites(db: &State<ConnectionThreadSafe>) -> Json<Vec<ButtonItem>> {
let query = "WITH favorites as (SELECT itemname, COUNT(date_added) day_count
FROM adds
WHERE (date(julianday('now')) - date(date_added, 'unixepoch')) < 184
GROUP BY itemname
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)
.unwrap()
.into_iter()
.map(|row| {
let row = row.unwrap();
ButtonItem {
name: row.read::<&str, _>("itemname").to_string(),
category: row
.try_read::<&str, _>("category")
.ok()
.map(ToString::to_string),
}
})
.collect();
Json(list)
}
#[put("/api/favourites/<itemname>", data = "<category>")]
fn set_categories(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();
statement.bind((2, category)).unwrap();
statement.bind((3, category)).unwrap();
while let Ok(sqlite::State::Row) = statement.next() {}
}
#[put("/api/list/<item>", data = "<data>")]
fn put_list(item: &str, data: Json<ItemData>, db: &State<ConnectionThreadSafe>) {
// TODO: errorhandling
@@ -60,6 +96,10 @@ fn put_list(item: &str, data: Json<ItemData>, db: &State<ConnectionThreadSafe>)
statement.bind((4, data.checked as i64)).unwrap();
statement.bind((5, data.amount)).unwrap();
while let Ok(sqlite::State::Row) = statement.next() {}
let query = "INSERT INTO adds VALUES(?, strftime('%s','now')) ON CONFLICT DO NOTHING";
let mut statement = db.prepare(query).unwrap();
statement.bind((1, item)).unwrap();
while let Ok(sqlite::State::Row) = statement.next() {}
}
#[delete("/api/list/<item>")]
@@ -84,6 +124,16 @@ fn rocket() -> _ {
.disable::<rocket::shield::Frame>(),
)
.manage(db)
.mount("/", routes![index, get_list, put_list, delete_item])
.mount(
"/",
routes![
index,
get_list,
put_list,
delete_item,
get_favorites,
set_categories
],
)
.mount("/", FileServer::from(www_path()))
}