Skip to content
Snippets Groups Projects
Commit ccce018b authored by b5-mohammed's avatar b5-mohammed
Browse files

event

parent e9d1052a
No related branches found
No related tags found
No related merge requests found
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