From 26ad0ba80ab61fbea78c9adfe584138ad4a24983 Mon Sep 17 00:00:00 2001 From: philipp Date: Fri, 17 Nov 2023 10:30:30 +0100 Subject: [PATCH 1/5] notifications --- src/model/notification.rs | 32 +++++++++++++++++++++----------- staging-diff.sql | 9 +++++++++ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/model/notification.rs b/src/model/notification.rs index d345f74..8ba4741 100644 --- a/src/model/notification.rs +++ b/src/model/notification.rs @@ -8,25 +8,35 @@ pub struct Notification { pub user_id: i64, pub message: String, pub read_at: NaiveDateTime, + pub created_at: NaiveDateTime, pub category: String, + pub link: Option, } impl Notification { - //pub async fn create(db: &SqlitePool, msg: String) -> bool { - // sqlx::query!("INSERT INTO log(msg) VALUES (?)", msg,) - // .execute(db) - // .await - // .is_ok() - //} + pub async fn create( + db: &SqlitePool, + user: &User, + message: &str, + category: &str, + link: Option<&str>, + ) { + sqlx::query!( + "INSERT INTO notification(user_id, message, category, link) VALUES (?, ?, ?, ?)", + user.id, + message, + category, + link + ) + .execute(db) + .await + .unwrap() + } async fn for_user(db: &SqlitePool, user: &User) -> Vec { sqlx::query_as!( Log, - " -SELECT id, user_id, message, read_at, category -FROM notification -WHERE user_id = {} - ", + "SELECT * FROM notification WHERE user_id = {}", user.id ) .fetch_all(db) diff --git a/staging-diff.sql b/staging-diff.sql index e69de29..a87f58f 100644 --- a/staging-diff.sql +++ b/staging-diff.sql @@ -0,0 +1,9 @@ +CREATE TABLE IF NOT EXISTS "notification" ( + "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, + "user_id" INTEGER NOT NULL REFERENCES user(id), + "message" TEXT NOT NULL, + "read_at" DATETIME, + "created_at" DATETIME DEFAULT CURRENT_TIMESTAMP, + "category" TEXT NOT NULL, + "link" TEXT +); From ab64583efc43cfc98e0b85c502f2a59bd986c5b8 Mon Sep 17 00:00:00 2001 From: philipp Date: Fri, 17 Nov 2023 10:30:30 +0100 Subject: [PATCH 2/5] notifications --- src/model/notification.rs | 32 +++++++++++++++++++++----------- staging-diff.sql | 9 +++++++++ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/model/notification.rs b/src/model/notification.rs index d345f74..8ba4741 100644 --- a/src/model/notification.rs +++ b/src/model/notification.rs @@ -8,25 +8,35 @@ pub struct Notification { pub user_id: i64, pub message: String, pub read_at: NaiveDateTime, + pub created_at: NaiveDateTime, pub category: String, + pub link: Option, } impl Notification { - //pub async fn create(db: &SqlitePool, msg: String) -> bool { - // sqlx::query!("INSERT INTO log(msg) VALUES (?)", msg,) - // .execute(db) - // .await - // .is_ok() - //} + pub async fn create( + db: &SqlitePool, + user: &User, + message: &str, + category: &str, + link: Option<&str>, + ) { + sqlx::query!( + "INSERT INTO notification(user_id, message, category, link) VALUES (?, ?, ?, ?)", + user.id, + message, + category, + link + ) + .execute(db) + .await + .unwrap() + } async fn for_user(db: &SqlitePool, user: &User) -> Vec { sqlx::query_as!( Log, - " -SELECT id, user_id, message, read_at, category -FROM notification -WHERE user_id = {} - ", + "SELECT * FROM notification WHERE user_id = {}", user.id ) .fetch_all(db) diff --git a/staging-diff.sql b/staging-diff.sql index e69de29..a87f58f 100644 --- a/staging-diff.sql +++ b/staging-diff.sql @@ -0,0 +1,9 @@ +CREATE TABLE IF NOT EXISTS "notification" ( + "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, + "user_id" INTEGER NOT NULL REFERENCES user(id), + "message" TEXT NOT NULL, + "read_at" DATETIME, + "created_at" DATETIME DEFAULT CURRENT_TIMESTAMP, + "category" TEXT NOT NULL, + "link" TEXT +); From f4cdd0ae283d8afe5e577304c62a10bd45a89ac1 Mon Sep 17 00:00:00 2001 From: philipp Date: Wed, 10 Jan 2024 14:20:30 +0100 Subject: [PATCH 3/5] add table --- migration.sql | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/migration.sql b/migration.sql index 2cc86d4..a727b31 100644 --- a/migration.sql +++ b/migration.sql @@ -135,3 +135,13 @@ CREATE TABLE IF NOT EXISTS "boat_damage" ( "verified_at" datetime, "lock_boat" boolean not null default false -- if true: noone can use the boat ); + +CREATE TABLE IF NOT EXISTS "notification" ( + "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, + "user_id" INTEGER NOT NULL REFERENCES user(id), + "message" TEXT NOT NULL, + "read_at" DATETIME, + "created_at" DATETIME DEFAULT CURRENT_TIMESTAMP, + "category" TEXT NOT NULL, + "link" TEXT +); From 28acee3085a14cc79ae743cb08481af5a95938bb Mon Sep 17 00:00:00 2001 From: philipp Date: Fri, 17 Nov 2023 10:30:30 +0100 Subject: [PATCH 4/5] notifications --- src/model/notification.rs | 32 +++++++++++++++++++++----------- staging-diff.sql | 9 +++++++++ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/model/notification.rs b/src/model/notification.rs index d345f74..8ba4741 100644 --- a/src/model/notification.rs +++ b/src/model/notification.rs @@ -8,25 +8,35 @@ pub struct Notification { pub user_id: i64, pub message: String, pub read_at: NaiveDateTime, + pub created_at: NaiveDateTime, pub category: String, + pub link: Option, } impl Notification { - //pub async fn create(db: &SqlitePool, msg: String) -> bool { - // sqlx::query!("INSERT INTO log(msg) VALUES (?)", msg,) - // .execute(db) - // .await - // .is_ok() - //} + pub async fn create( + db: &SqlitePool, + user: &User, + message: &str, + category: &str, + link: Option<&str>, + ) { + sqlx::query!( + "INSERT INTO notification(user_id, message, category, link) VALUES (?, ?, ?, ?)", + user.id, + message, + category, + link + ) + .execute(db) + .await + .unwrap() + } async fn for_user(db: &SqlitePool, user: &User) -> Vec { sqlx::query_as!( Log, - " -SELECT id, user_id, message, read_at, category -FROM notification -WHERE user_id = {} - ", + "SELECT * FROM notification WHERE user_id = {}", user.id ) .fetch_all(db) diff --git a/staging-diff.sql b/staging-diff.sql index e69de29..a87f58f 100644 --- a/staging-diff.sql +++ b/staging-diff.sql @@ -0,0 +1,9 @@ +CREATE TABLE IF NOT EXISTS "notification" ( + "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, + "user_id" INTEGER NOT NULL REFERENCES user(id), + "message" TEXT NOT NULL, + "read_at" DATETIME, + "created_at" DATETIME DEFAULT CURRENT_TIMESTAMP, + "category" TEXT NOT NULL, + "link" TEXT +); From 09fffa1830edf43d91f69c977c4f973ae660fbde Mon Sep 17 00:00:00 2001 From: philipp Date: Wed, 10 Jan 2024 14:20:30 +0100 Subject: [PATCH 5/5] add table --- migration.sql | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/migration.sql b/migration.sql index 2bbf1c9..d271b5a 100644 --- a/migration.sql +++ b/migration.sql @@ -140,3 +140,13 @@ CREATE TABLE IF NOT EXISTS "boat_damage" ( "verified_at" datetime, "lock_boat" boolean not null default false -- if true: noone can use the boat ); + +CREATE TABLE IF NOT EXISTS "notification" ( + "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, + "user_id" INTEGER NOT NULL REFERENCES user(id), + "message" TEXT NOT NULL, + "read_at" DATETIME, + "created_at" DATETIME DEFAULT CURRENT_TIMESTAMP, + "category" TEXT NOT NULL, + "link" TEXT +);