mirror of
https://github.com/rzuasti/oott.git
synced 2026-07-08 19:21:54 +02:00
Added notifications table and model
This commit is contained in:
@@ -4,3 +4,12 @@ CREATE TABLE devices(
|
||||
vendor TEXT,
|
||||
last_seen TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE notifications(
|
||||
id INTEGER PRIMARY KEY,
|
||||
created_on TEXT NOT NULL,
|
||||
notification_type TEXT NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
body TEXT,
|
||||
is_new INTEGER NOT NULL
|
||||
);
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
pub mod devices;
|
||||
pub mod notifications;
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
use chrono::{DateTime, Utc, serde::ts_seconds};
|
||||
use rusqlite::types::{FromSql, FromSqlError, FromSqlResult, ToSql, ToSqlOutput, ValueRef};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{error::Error, fmt, str::FromStr};
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
pub struct Notification {
|
||||
pub id: u64,
|
||||
#[serde(with = "ts_seconds")]
|
||||
pub created_on: DateTime<Utc>,
|
||||
pub notification_type: NotificationType,
|
||||
pub title: String,
|
||||
pub body: String,
|
||||
pub is_new: bool,
|
||||
}
|
||||
|
||||
impl fmt::Display for Notification {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"id={}, created_on={}, notification_type={}, is_new={}\ntitle={}\nbody={}",
|
||||
self.id, self.created_on, self.notification_type, self.is_new, self.title, self.body
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
pub enum NotificationType {
|
||||
NewDeviceFound,
|
||||
DeviceOnlineAfterTime,
|
||||
Other,
|
||||
}
|
||||
|
||||
impl fmt::Display for NotificationType {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Self::NewDeviceFound => write!(f, "NewDeviceFound"),
|
||||
Self::DeviceOnlineAfterTime => write!(f, "DeviceOnlineAfterTime"),
|
||||
Self::Other => write!(f, "Other"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct NotificationTypeParseError;
|
||||
|
||||
impl fmt::Display for NotificationTypeParseError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "Error parsing notification type")
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for NotificationTypeParseError {}
|
||||
|
||||
impl FromStr for NotificationType {
|
||||
type Err = NotificationTypeParseError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s {
|
||||
"NewDeviceFound" => Ok(NotificationType::NewDeviceFound),
|
||||
"DeviceOnlineAfterTime" => Ok(NotificationType::DeviceOnlineAfterTime),
|
||||
_ => Ok(NotificationType::Other),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToSql for NotificationType {
|
||||
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
|
||||
Ok(self.to_string().into())
|
||||
}
|
||||
}
|
||||
|
||||
impl FromSql for NotificationType {
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
value
|
||||
.as_str()?
|
||||
.parse()
|
||||
.map_err(|e| FromSqlError::Other(Box::new(e)))
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,17 @@
|
||||
use axum::{Router, routing::get};
|
||||
use axum::{Json, Router, http::StatusCode, routing::get};
|
||||
use chrono::Local;
|
||||
use log::info;
|
||||
use tower_http::services::ServeDir;
|
||||
|
||||
use crate::model::devices::Device;
|
||||
|
||||
pub async fn serve() -> Result<(), String> {
|
||||
info!("Starting web server");
|
||||
let static_files = ServeDir::new("./web");
|
||||
|
||||
let router = Router::new()
|
||||
.route("/", get(|| async { "hello" }))
|
||||
.route("/devices", get(get_devices))
|
||||
.nest_service("/web", static_files);
|
||||
info!("Server running at http://0.0.0.0:3000");
|
||||
// Start the server
|
||||
@@ -15,3 +19,16 @@ pub async fn serve() -> Result<(), String> {
|
||||
axum::serve(listener, router).await.unwrap();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_devices() -> Result<Json<Vec<Device>>, StatusCode> {
|
||||
let mut devices = Vec::new();
|
||||
|
||||
devices.push(Device {
|
||||
mac_address: "mac".to_string(),
|
||||
ipv4_address: "ip".to_string(),
|
||||
vendor: "vendor".to_string(),
|
||||
last_seen: Local::now().to_utc(),
|
||||
});
|
||||
|
||||
Ok(Json(devices))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user