From 9c1c40483979873fa340174b5fd574bbeb9d838b Mon Sep 17 00:00:00 2001 From: Moritz Bergmann Date: Thu, 14 Mar 2024 22:19:53 +0100 Subject: [PATCH] added title and description to the database --- public/feed.html | 5 +++++ public/feed.js | 11 +++++++++++ public/record.html | 16 ++++++++++++++- public/record.js | 5 ++++- public/scripts/api/activities.js | 22 +++++++++++++-------- public/style/feed.css | 4 ++++ src/activity.rs | 6 ++++++ src/database.rs | 34 +++++++++++++++++++------------- src/logic/activities.rs | 2 ++ 9 files changed, 81 insertions(+), 24 deletions(-) diff --git a/public/feed.html b/public/feed.html index d49bf82..ffff684 100644 --- a/public/feed.html +++ b/public/feed.html @@ -45,6 +45,11 @@

Weekly PushUps Challenge

+
+

+
+
+
Type: 
diff --git a/public/feed.js b/public/feed.js index 33cc310..8f79623 100644 --- a/public/feed.js +++ b/public/feed.js @@ -142,6 +142,17 @@ async function main() { } } + if (activity.title != undefined && activity.title != null && activity.title != "") { + clone.querySelector(".post-title").innerHTML = activity.title; + } else { + clone.querySelector(".post-title").remove(); + } + if (activity.description != undefined && activity.description != null && activity.description != "") { + clone.querySelector(".post-description").innerHTML = activity.description; + } else { + clone.querySelector(".post-description").remove(); + } + clone.querySelector(".post").id = `${activity.id}`; clone.querySelector(".post-name").innerHTML = author_name; clone.querySelector(".post-athlete-link").href = athlete_link; diff --git a/public/record.html b/public/record.html index 2e5b8ec..bfa1d5d 100644 --- a/public/record.html +++ b/public/record.html @@ -19,6 +19,20 @@

New Activity

+
+ +
+ + +
+ + +
+ + +
+
+
@@ -76,4 +90,4 @@

New Activity

- \ No newline at end of file + diff --git a/public/record.js b/public/record.js index d33f2bc..c6255a5 100644 --- a/public/record.js +++ b/public/record.js @@ -57,6 +57,9 @@ set_time_now("start_time"); document.querySelector("#form").addEventListener("submit", async (e) => { e.preventDefault(); + const title = document.querySelector("#title").value; + const description = document.querySelector("#description").value; + const amount = Number(document.querySelector("#amount").value); const activity_type = document.querySelector("#activity_type").value; @@ -72,7 +75,7 @@ document.querySelector("#form").addEventListener("submit", async (e) => { // todo add timezone to end of string console.log(end_time); - let activity = new NewActivity(amount, activity_type, start_time_str, end_time_str); + let activity = new NewActivity(title, description, amount, activity_type, start_time_str, end_time_str); let res = await create(activity); if (res.ok) { const urlParams = new URLSearchParams(window.location.search); diff --git a/public/scripts/api/activities.js b/public/scripts/api/activities.js index 36d0cb5..147ea80 100644 --- a/public/scripts/api/activities.js +++ b/public/scripts/api/activities.js @@ -5,8 +5,10 @@ export const ActivitType = { } export class Activity { - constructor(id, author_id, amount, activity_type, start_time, end_time) { + constructor(id, title, description, author_id, amount, activity_type, start_time, end_time) { this.id = id; + this.title = title; + this.description = description; this.author_id = author_id; this.amount = amount; this.activity_type = activity_type; @@ -16,7 +18,9 @@ export class Activity { } export class NewActivity { - constructor(amount, activity_type, start_time, end_time) { + constructor(title, description, amount, activity_type, start_time, end_time) { + this.title = title; + this.description = description; this.amount = amount; this.activity_type = activity_type; this.start_time = start_time; @@ -25,7 +29,9 @@ export class NewActivity { } export class EditActivity { - constructor(amount, activity_type, start_time, end_time) { + constructor(title, description, amount, activity_type, start_time, end_time) { + this.title = title; + this.description = description; this.amount = amount; this.activity_type = activity_type; this.start_time = start_time; @@ -45,7 +51,7 @@ export async function get(id) { let response = await fetch(request); if (response.status === STATUS.OK) { let value = await response.json(); - return new Result(true, new Activity(value.id, value.author_id, value.amount, value.activity_type, value.start_time, value.end_time)); + return new Result(true, new Activity(value.id, value.title, value.description, value.author_id, value.amount, value.activity_type, value.start_time, value.end_time)); } else { let error = await response.text(); return new Result(false, error); @@ -67,7 +73,7 @@ export async function get_from_to(from, to) { let raw = await response.json(); let activities = []; for (const value of raw) { - activities.push(new Activity(value.id, value.author_id, value.amount, value.activity_type, value.start_time, value.end_time)); + activities.push(new Activity(value.id, value.title, value.description, value.author_id, value.amount, value.activity_type, value.start_time, value.end_time)); } return new Result(true, activities); } else { @@ -92,7 +98,7 @@ export async function create(activity) { let response = await fetch(request); if (response.status === STATUS.CREATED) { let value = await response.json(); - return new Result(true, new Activity(value.id, value.author_id, value.amount, value.activity_type, value.start_time, value.end_time)); + return new Result(true, new Activity(value.id, value.title, value.description, value.author_id, value.amount, value.activity_type, value.start_time, value.end_time)); } else { let error = await response.text(); return new Result(false, error); @@ -116,7 +122,7 @@ export async function edit(id, activity) { let response = await fetch(request); if (response.status === STATUS.OK) { let value = await response.json(); - return new Result(true, new Activity(value.id, value.author_id, value.amount, value.activity_type, value.start_time, value.end_time)); + return new Result(true, new Activity(value.id, value.title, value.description, value.author_id, value.amount, value.activity_type, value.start_time, value.end_time)); } else { let error = await response.text(); return new Result(false, error); @@ -135,7 +141,7 @@ export async function remove(id) { let response = await fetch(request); if (response.status === STATUS.OK) { let value = await response.json(); - return new Result(true, new Activity(value.id, value.author_id, value.amount, value.activity_type, value.start_time, value.end_time)); + return new Result(true, new Activity(value.id, value.title, value.description, value.author_id, value.amount, value.activity_type, value.start_time, value.end_time)); } else { let error = await response.text(); return new Result(false, error); diff --git a/public/style/feed.css b/public/style/feed.css index 8784829..57a3683 100644 --- a/public/style/feed.css +++ b/public/style/feed.css @@ -3,3 +3,7 @@ padding: 1rem; border: solid 1px black; } + +.post-title { + margin-bottom: 0rem; +} diff --git a/src/activity.rs b/src/activity.rs index edaadcf..ba7f9bd 100644 --- a/src/activity.rs +++ b/src/activity.rs @@ -11,6 +11,8 @@ pub enum ActivityType { pub struct Activity { pub id: i64, pub author_id: i64, + pub title: String, + pub description: String, pub amount: i64, pub activity_type: ActivityType, pub start_time: DateTime, @@ -19,6 +21,8 @@ pub struct Activity { #[derive(Clone, Debug, Deserialize, Serialize)] pub struct BareActivity { + pub title: String, + pub description: String, pub amount: i64, pub activity_type: ActivityType, pub start_time: DateTime, @@ -27,6 +31,8 @@ pub struct BareActivity { #[derive(Clone, Debug, Deserialize, Serialize)] pub struct StringBareActivity { + pub title: String, + pub description: String, pub amount: i64, pub activity_type: ActivityType, pub start_time: String, diff --git a/src/database.rs b/src/database.rs index f42cb41..55e3530 100644 --- a/src/database.rs +++ b/src/database.rs @@ -35,10 +35,10 @@ pub async fn init() -> Result { connection .execute( "CREATE TABLE IF NOT EXISTS 'users' ( - 'id' INTEGER UNIQUE, - 'username' TEXT NOT NULL UNIQUE, - 'password_hash' TEXT NOT NULL UNIQUE, - PRIMARY KEY('id' AUTOINCREMENT))", + 'id' INTEGER UNIQUE, + 'username' TEXT NOT NULL UNIQUE, + 'password_hash' TEXT NOT NULL UNIQUE, + PRIMARY KEY('id' AUTOINCREMENT))", ) .await?; @@ -46,14 +46,16 @@ pub async fn init() -> Result { connection .execute( "CREATE TABLE IF NOT EXISTS 'activities' ( - 'id' INTEGER UNIQUE, - 'start_time' TEXT NOT NULL, - 'end_time' TEXT NOT NULL, - 'amount' INTEGER NOT NULL, - 'activity_type' TEXT NOT NULL, - 'author_id' INTEGER NOT NULL, - FOREIGN KEY('author_id') REFERENCES 'users'('id') ON DELETE CASCADE, - PRIMARY KEY('id' AUTOINCREMENT))", + 'id' INTEGER UNIQUE, + 'title' TEXT, + 'description' TEXT, + 'start_time' TEXT NOT NULL, + 'end_time' TEXT NOT NULL, + 'amount' INTEGER NOT NULL, + 'activity_type' TEXT NOT NULL, + 'author_id' INTEGER NOT NULL, + FOREIGN KEY('author_id') REFERENCES 'users'('id') ON DELETE CASCADE, + PRIMARY KEY('id' AUTOINCREMENT))", ) .await?; @@ -262,8 +264,10 @@ pub mod activity { ) -> Result { let mut connection = pool.acquire().await?; - let activity: Activity = sqlx::query_as("INSERT INTO activities (author_id, amount, activity_type, start_time, end_time) VALUES ($1, $2, $3, $4, $5) RETURNING *") + let activity: Activity = sqlx::query_as("INSERT INTO activities (author_id, title, description, amount, activity_type, start_time, end_time) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *") .bind(author_id) + .bind(&activity.title) + .bind(&activity.description) .bind(activity.amount) .bind(&activity.activity_type) .bind(activity.start_time) @@ -282,7 +286,9 @@ pub mod activity { ) -> Result { let mut connection = pool.acquire().await?; - let result: Activity = sqlx::query_as("UPDATE activities SET amount = $1, activity_type = $2, start_time = $3, end_time = $4 WHERE id = $5 RETURNING *") + let result: Activity = sqlx::query_as("UPDATE activities SET title = $1, description = $2, amount = $3, activity_type = $4, start_time = $5, end_time = $6 WHERE id = $7 RETURNING *") + .bind(&activity.title) + .bind(&activity.description) .bind(activity.amount) .bind(&activity.activity_type) .bind(activity.start_time) diff --git a/src/logic/activities.rs b/src/logic/activities.rs index 47e04ca..58aca87 100644 --- a/src/logic/activities.rs +++ b/src/logic/activities.rs @@ -80,6 +80,8 @@ pub async fn post_activity( let author_id = auth.current_user.unwrap().id; let new_activity = BareActivity { + title: payload.title, + description: payload.description, amount: payload.amount, activity_type: payload.activity_type, start_time: start_time,