initial commit
This commit is contained in:
34
src/models/sessions.rs
Normal file
34
src/models/sessions.rs
Normal file
@ -0,0 +1,34 @@
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
use super::devices::Device;
|
||||
|
||||
pub struct Session {
|
||||
id: i64,
|
||||
device_id: i64,
|
||||
value: String,
|
||||
}
|
||||
|
||||
impl Session {
|
||||
pub async fn create(conn: &SqlitePool, device: &Device, value: &str) -> anyhow::Result<Self> {
|
||||
let device_id = device.id();
|
||||
Ok(sqlx::query_as!(Self, "insert into sessions(device_id, value) values(?, ?) returning id, device_id, value", device_id, value).fetch_one(conn).await?)
|
||||
}
|
||||
|
||||
/// Get the session's id.
|
||||
#[must_use]
|
||||
pub fn id(&self) -> i64 {
|
||||
self.id
|
||||
}
|
||||
|
||||
/// Get the session's device id.
|
||||
#[must_use]
|
||||
pub fn device_id(&self) -> i64 {
|
||||
self.device_id
|
||||
}
|
||||
|
||||
/// Get a reference to the session's value.
|
||||
#[must_use]
|
||||
pub fn value(&self) -> &str {
|
||||
self.value.as_ref()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user