Skip to content
Snippets Groups Projects
Commit 9d7e8064 authored by James2Tulloch's avatar James2Tulloch
Browse files
parents 8ebb22de ccce018b
No related branches found
No related tags found
No related merge requests found
......@@ -23,25 +23,39 @@ pub fn init_db(db_url: &str) -> PyResult<()> {
profilepicture VARCHAR,
firstlogin BOOLEAN NOT NULL DEFAULT TRUE
);
CREATE TABLE IF NOT EXISTS groups (
id SERIAL PRIMARY KEY,
name VARCHAR NOT NULL,
member_count INTEGER NOT NULL DEFAULT 0
);
CREATE TABLE IF NOT EXISTS group_members (
group_id INTEGER REFERENCES groups(id),
user_id INTEGER REFERENCES users(id),
PRIMARY KEY (group_id, user_id)
);
CREATE TABLE IF NOT EXISTS posts (
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(id),
group_id INTEGER REFERENCES groups(id),
content TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(id),
group_id INTEGER REFERENCES groups(id),
content TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE TABLE IF NOT EXISTS events (
id SERIAL PRIMARY KEY,
title TEXT NOT NULL,
description TEXT,
location TEXT NOT NULL,
date DATE NOT NULL,
time TIME NOT NULL,
created_by INTEGER REFERENCES users(id),
group_id INTEGER REFERENCES groups(id),
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
"
).map_err(pg_err)?;
Ok(())
}
}
\ No newline at end of file
use pyo3::prelude::*;
use serde::{Serialize, Deserialize};
#[pyclass]
#[derive(Serialize, Deserialize, Debug)]
pub struct Event {
#[pyo3(get, set)]
pub id: i32,
#[pyo3(get, set)]
pub title: String,
#[pyo3(get, set)]
pub description: Option<String>,
#[pyo3(get, set)]
pub location: String,
#[pyo3(get, set)]
pub date: String, // or NaiveDate
#[pyo3(get, set)]
pub time: String, // or NaiveTime
#[pyo3(get, set)]
pub created_by: i32,
#[pyo3(get, set)]
pub group_id: Option<i32>,
#[pyo3(get, set)]
pub created_at: String,
}
\ No newline at end of file
use crate::models::Event;
use pyo3::prelude::*;
use pyo3::exceptions::PyRuntimeError;
use postgres::{Client, NoTls};
use chrono::{NaiveDate, NaiveTime, NaiveDateTime};
fn pg_err(e: postgres::Error) -> PyErr {
PyRuntimeError::new_err(e.to_string())
}
/// Create a new event
#[pyfunction]
pub fn create_event(
db_url: &str,
title: &str,
description: Option<&str>,
location: &str,
date: &str, // expected format: "YYYY-MM-DD"
time: &str, // expected format: "HH:MM:SS"
created_by: i32,
group_id: Option<i32>
) -> PyResult<()> {
let mut client = Client::connect(db_url, NoTls).map_err(pg_err)?;
let date_parsed = NaiveDate::parse_from_str(date, "%Y-%m-%d")
.map_err(|e| PyRuntimeError::new_err(format!("Invalid date: {}", e)))?;
let time_parsed = NaiveTime::parse_from_str(time, "%H:%M:%S")
.map_err(|e| PyRuntimeError::new_err(format!("Invalid time: {}", e)))?;
client.execute(
"
INSERT INTO events (title, description, location, date, time, created_by, group_id)
VALUES ($1, $2, $3, $4, $5, $6, $7)
",
&[&title, &description, &location, &date_parsed, &time_parsed, &created_by, &group_id]
).map_err(pg_err)?;
Ok(())
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment