Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
DAESD
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
j2-tulloch
DAESD
Commits
ccce018b
Commit
ccce018b
authored
1 week ago
by
b5-mohammed
Browse files
Options
Downloads
Patches
Plain Diff
event
parent
e9d1052a
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
rust_crud_api/src/models/events.rs
+39
-0
39 additions, 0 deletions
rust_crud_api/src/models/events.rs
with
39 additions
and
0 deletions
rust_crud_api/src/models/events.rs
0 → 100644
+
39
−
0
View file @
ccce018b
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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment