feat: add event database entities
This commit is contained in:
parent
1bc9ae01d8
commit
d7c5c85460
6 changed files with 107 additions and 2 deletions
88
src/db/event.rs
Normal file
88
src/db/event.rs
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
use std::{fmt::Display, str::FromStr};
|
||||
|
||||
use chrono::Utc;
|
||||
use r2d2_sqlite::rusqlite::{
|
||||
self,
|
||||
types::{FromSql, FromSqlError},
|
||||
Row, ToSql,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub enum EventType {
|
||||
Watering,
|
||||
}
|
||||
|
||||
impl ToString for EventType {
|
||||
fn to_string(&self) -> String {
|
||||
String::from(match self {
|
||||
Self::Watering => "watering",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ParseEventTypeErr;
|
||||
|
||||
impl Display for ParseEventTypeErr {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "invalid name")
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for ParseEventTypeErr {}
|
||||
|
||||
impl FromStr for EventType {
|
||||
type Err = ParseEventTypeErr;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s {
|
||||
"watering" => Ok(Self::Watering),
|
||||
_ => Err(ParseEventTypeErr),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToSql for EventType {
|
||||
fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> {
|
||||
Ok(self.to_string().into())
|
||||
}
|
||||
}
|
||||
|
||||
impl FromSql for EventType {
|
||||
fn column_result(value: rusqlite::types::ValueRef<'_>) -> rusqlite::types::FromSqlResult<Self> {
|
||||
value
|
||||
.as_str()?
|
||||
.parse()
|
||||
.map_err(|e| FromSqlError::Other(Box::new(e)))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct Event {
|
||||
id: i32,
|
||||
plant_id: i32,
|
||||
event_type: EventType,
|
||||
time: chrono::DateTime<Utc>,
|
||||
description: String,
|
||||
}
|
||||
|
||||
impl Event {
|
||||
pub fn from_row(row: Row<'_>) -> rusqlite::Result<Self> {
|
||||
Ok(Self {
|
||||
id: row.get("id")?,
|
||||
plant_id: row.get("plant_id")?,
|
||||
event_type: row.get("event_type")?,
|
||||
time: row.get("time")?,
|
||||
description: row.get("description")?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct NewEvent {
|
||||
plant_id: i32,
|
||||
event_type: EventType,
|
||||
time: chrono::DateTime<Utc>,
|
||||
description: String,
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
mod comment;
|
||||
mod event;
|
||||
mod plant;
|
||||
|
||||
use r2d2_sqlite::{rusqlite, SqliteConnectionManager};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue