From 462716633e0623bbf411371cdee72079011b8f0a Mon Sep 17 00:00:00 2001 From: Chris Jean-Marie Date: Sat, 14 Dec 2024 04:57:13 +0000 Subject: [PATCH] Get calendar events from database --- backend/src/calendar.rs | 44 ++++++++++++++++--- backend/templates/authorized.html | 46 ++++++++++---------- backend/templates/dashboard.html | 71 ++++++++++++++++++++----------- backend/{promote.sh => totest.sh} | 2 +- 4 files changed, 109 insertions(+), 54 deletions(-) rename backend/{promote.sh => totest.sh} (86%) diff --git a/backend/src/calendar.rs b/backend/src/calendar.rs index f7b3ed7..67b321c 100644 --- a/backend/src/calendar.rs +++ b/backend/src/calendar.rs @@ -1,12 +1,12 @@ use askama::Template; use askama_axum::{IntoResponse, Response}; use axum::{ - extract::{Path, State}, - response::{Html, Redirect}, - Extension, + body::Body, extract::{Path, Query, Request, State}, response::{Html, Redirect}, Extension }; use http::StatusCode; -use sqlx::PgPool; +use serde::{Deserialize, Serialize}; +use serde_json::json; +use sqlx::{types::Json, PgPool, Row}; use crate::{ middlewares::is_authorized, @@ -87,17 +87,24 @@ pub async fn calendar( } } +#[derive(Deserialize, Serialize, Debug)] +pub struct EventParams { + start: String, + end: String, +} pub async fn get_events( Path(calendar): Path, State(db_pool): State, + Query(params): Query, Extension(user_data): Extension>, ) -> String { println!("Calendar: {}", calendar); + //println!("Paramters: {:?}", params); // Is the user logged in? let logged_in = user_data.is_some(); // Set default events - let mut events = "[]"; + let mut eventstring: String = "[]".to_string(); if logged_in { // Extract the user data. @@ -106,9 +113,32 @@ pub async fn get_events( if is_authorized("/calendar", user_data, db_pool.clone()).await { // Get requested calendar events from database - events = "[{\"title\": \"Chris and Terri\", \"start\": \"2024-12-23T14:00:00\", \"end\": \"2024-12-27T10:00:00\", \"allDay\": false}, {\"title\": \"Stephen\", \"start\": \"2024-12-27T14:00:00\", \"end\": \"2024-12-31T10:00:00\", \"allDay\": false}]"; + let events = sqlx::query(r#"select to_json(json_agg(json_build_object( + 'title', ce.title, + 'start', ce.start_time, + 'end', ce.end_time, + 'allDay', false))) + from calendar_events ce + join calendar c on c.id = ce.calendar_id + join calendar_event_types cet on cet.id = ce.event_type_id + where ce.celebrate = true + and c.name = $1 + and start_time > $2 + and start_time < $3"#) + .bind(calendar) + .bind(chrono::DateTime::parse_from_rfc3339(¶ms.start).unwrap()) + .bind(chrono::DateTime::parse_from_rfc3339(¶ms.end).unwrap()) + .fetch_one(&db_pool) + .await; + + if let Ok(json_string) = events { + if let Ok(stringevents) = json_string.try_get_raw(0).map(|v| v.as_str().unwrap_or("")) { + println!("PgValue: {:?}", stringevents); + eventstring = stringevents.to_string(); + } + } } } - events.to_string() + eventstring } diff --git a/backend/templates/authorized.html b/backend/templates/authorized.html index c5fcadb..6cc1fb4 100644 --- a/backend/templates/authorized.html +++ b/backend/templates/authorized.html @@ -1,31 +1,33 @@ {% extends "base.html" %} {% block content %} -
- - {% endblock content %} \ No newline at end of file diff --git a/backend/templates/dashboard.html b/backend/templates/dashboard.html index 21971a7..02a98af 100644 --- a/backend/templates/dashboard.html +++ b/backend/templates/dashboard.html @@ -2,31 +2,54 @@ {% block center %}
-

Points of Interest

- TLC Creations - Seguin Township +
+
+
Points of Interest
+
+ TLC Creations +
+
+ Seguin Township +
+
+
+
+
+
+
+
Seguin Fire Rating
+ Fire Rating: MODERATE +
+
-
- Fire Rating: MODERATE -
-
-

Web links

-

Fonts

- -

Family tree

- +
+
+

Web links

+

Fonts

+ +
+ +
+
{% endblock center %} \ No newline at end of file diff --git a/backend/promote.sh b/backend/totest.sh similarity index 86% rename from backend/promote.sh rename to backend/totest.sh index a636d87..17adef1 100755 --- a/backend/promote.sh +++ b/backend/totest.sh @@ -2,6 +2,6 @@ cargo build --release ssh chris@192.168.59.31 'pkill jean-marie' scp target/release/jean-marie chris@192.168.59.31:/opt/jean-marie scp runsite.sh chris@192.168.59.31:/opt/jean-marie -scp .env chris@192.168.59.31:/opt/jean-marie +#scp .env chris@192.168.59.31:/opt/jean-marie scp -r templates chris@192.168.59.31:/opt/jean-marie ssh chris@192.168.59.31 '/opt/jean-marie/runsite.sh'