Skip to content

Commit

Permalink
added title and description to the database
Browse files Browse the repository at this point in the history
  • Loading branch information
mobergmann committed Mar 14, 2024
1 parent bfc9133 commit 9c1c404
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 24 deletions.
5 changes: 5 additions & 0 deletions public/feed.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ <h1><a href="/challenge.html">Weekly PushUps Challenge</a></h1>
<a class="post-athlete-link"><div class="post-name"></div></a>
</div>

<div class="container">
<h3 class="post-title"></h3>
<div class="post-description"></div>
</div>

<div class="container">
<div><b>Type:&nbsp;</b></div>
<div class="post-activity-type"></div>
Expand Down
11 changes: 11 additions & 0 deletions public/feed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 15 additions & 1 deletion public/record.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@
<form action="" id="form">
<h1>New Activity</h1>

<div class="container">
<label for="title">Title</label>
<div class="input-group">
<i class="fa-solid fa-heading"></i>
<input type="text" placeholder="Title" id="title" class="input">
</div>

<label for="description">Description</label>
<div class="input-group">
<i class="fa-solid fa-comment"></i>
<input type="text" placeholder="Description" id="description" class="input">
</div>
</div>

<div class="container">
<label for="activity_type">Activity Type</label>
<div class="input-group">
Expand Down Expand Up @@ -76,4 +90,4 @@ <h1>New Activity</h1>
<script type="module" src="record.js"></script>
</body>

</html>
</html>
5 changes: 4 additions & 1 deletion public/record.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down
22 changes: 14 additions & 8 deletions public/scripts/api/activities.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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 {
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions public/style/feed.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
padding: 1rem;
border: solid 1px black;
}

.post-title {
margin-bottom: 0rem;
}
6 changes: 6 additions & 0 deletions src/activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Utc>,
Expand All @@ -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<Utc>,
Expand All @@ -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,
Expand Down
34 changes: 20 additions & 14 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,27 @@ pub async fn init() -> Result<SqlitePool, sqlx::Error> {
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?;

// create activities table
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?;

Expand Down Expand Up @@ -262,8 +264,10 @@ pub mod activity {
) -> Result<Activity, Error> {
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)
Expand All @@ -282,7 +286,9 @@ pub mod activity {
) -> Result<Activity, Error> {
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)
Expand Down
2 changes: 2 additions & 0 deletions src/logic/activities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 9c1c404

Please sign in to comment.