⚡ Adds DTOs
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -271,6 +271,7 @@ checksum = "e8496eeb328dce26ee9d9b73275d396d9bddb433fa30106cf6056dd8c3c2764c"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"byteorder",
|
||||
"chrono",
|
||||
"diesel_derives",
|
||||
"downcast-rs",
|
||||
"itoa",
|
||||
|
||||
@@ -8,5 +8,5 @@ rocket = { version = "0.5.1", features = ["json"] }
|
||||
serde = { version = "1.0.228", features = ["derive"] }
|
||||
dotenv = "0.15.0"
|
||||
uuid = { version = "1.18.1", features = ["v4", "serde"] }
|
||||
diesel = { version = "2.3.2", features = ["postgres", "uuid"] }
|
||||
diesel = { version = "2.3.2", features = ["postgres", "uuid", "chrono"] }
|
||||
chrono = "0.4.42"
|
||||
|
||||
1
src/api/mod.rs
Normal file
1
src/api/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod tracker;
|
||||
29
src/api/tracker.rs
Normal file
29
src/api/tracker.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use crate::dtos::TrackerDTO;
|
||||
use crate::models::{AppState, Tracker};
|
||||
use crate::schema::trackers::dsl::trackers;
|
||||
use diesel::{QueryDsl, RunQueryDsl, SelectableHelper};
|
||||
use rocket::State;
|
||||
use rocket::serde::json::Json;
|
||||
|
||||
#[get("/?<offset>&<limit>")]
|
||||
pub fn index(
|
||||
offset: Option<i64>,
|
||||
limit: Option<i64>,
|
||||
data: &State<AppState>,
|
||||
) -> Json<Vec<TrackerDTO>> {
|
||||
let mut db = data.db.lock().unwrap();
|
||||
|
||||
let offset = offset.unwrap_or(0);
|
||||
let limit = limit.unwrap_or(10);
|
||||
|
||||
println!("hey");
|
||||
let results = trackers
|
||||
.offset(offset)
|
||||
.limit(limit)
|
||||
.select(Tracker::as_select())
|
||||
.load(&mut *db)
|
||||
.expect("Error loading trackers");
|
||||
|
||||
println!("hi");
|
||||
Json(results.iter().map(TrackerDTO::from).collect())
|
||||
}
|
||||
41
src/dtos.rs
Normal file
41
src/dtos.rs
Normal file
@@ -0,0 +1,41 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use crate::models::{Hit, Tracker};
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct TrackerDTO {
|
||||
pub id: String,
|
||||
pub created_at: String,
|
||||
}
|
||||
|
||||
impl TrackerDTO {
|
||||
pub fn from(tracker: &Tracker) -> TrackerDTO {
|
||||
TrackerDTO {
|
||||
id: tracker.id.to_string(),
|
||||
created_at: tracker.created_at.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct HitDTO {
|
||||
pub id: String,
|
||||
pub tracker_id: String,
|
||||
pub ip: String,
|
||||
pub agent: Option<String>,
|
||||
pub language: Option<String>,
|
||||
pub created_at: String,
|
||||
}
|
||||
|
||||
impl HitDTO {
|
||||
pub fn from(hit: &Hit) -> HitDTO {
|
||||
HitDTO {
|
||||
id: hit.id.to_string(),
|
||||
tracker_id: hit.tracker_id.to_string(),
|
||||
ip: hit.ip.clone(),
|
||||
agent: hit.agent.clone(),
|
||||
language: hit.language.clone(),
|
||||
created_at: hit.created_at.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
mod api;
|
||||
mod models;
|
||||
mod schema;
|
||||
mod tracker;
|
||||
mod dtos;
|
||||
|
||||
use crate::api::tracker;
|
||||
use crate::models::Hit;
|
||||
use crate::schema::hits::dsl::*;
|
||||
use diesel::{Connection, PgConnection, QueryDsl, RunQueryDsl, SelectableHelper};
|
||||
@@ -30,7 +32,9 @@ fn rocket() -> _ {
|
||||
.expect("Error loading hits");
|
||||
println!("results: {}", results.len());
|
||||
|
||||
let app_data = models::AppState::new(db);
|
||||
rocket::build()
|
||||
.manage(app_data)
|
||||
.mount("/", routes![index])
|
||||
.mount("/tracker", routes![tracker::index])
|
||||
}
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
use diesel::{Insertable, Queryable, Selectable};
|
||||
use diesel::data_types::PgTimestamp;
|
||||
use chrono::NaiveDateTime;
|
||||
use diesel::{PgConnection, Queryable, Selectable};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Queryable, Selectable, Insertable, Clone)]
|
||||
#[derive(Queryable, Selectable, Clone)]
|
||||
#[diesel(table_name = crate::schema::trackers)]
|
||||
#[diesel(check_for_backend(diesel::pg::Pg))]
|
||||
pub struct Tracker {
|
||||
pub id: Uuid,
|
||||
pub created_at: PgTimestamp,
|
||||
pub created_at: NaiveDateTime,
|
||||
}
|
||||
|
||||
#[derive(Queryable, Selectable, Insertable, Clone)]
|
||||
#[derive(Queryable, Selectable, Clone)]
|
||||
#[diesel(table_name = crate::schema::hits)]
|
||||
#[diesel(check_for_backend(diesel::pg::Pg))]
|
||||
pub struct Hit {
|
||||
@@ -19,5 +20,17 @@ pub struct Hit {
|
||||
pub ip: String,
|
||||
pub agent: Option<String>,
|
||||
pub language: Option<String>,
|
||||
pub created_at: PgTimestamp,
|
||||
pub created_at: NaiveDateTime,
|
||||
}
|
||||
|
||||
pub struct AppState {
|
||||
pub db: Arc<Mutex<PgConnection>>,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
pub fn new(db: PgConnection) -> Self {
|
||||
AppState {
|
||||
db: Arc::new(Mutex::new(db)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
use serde::Serialize;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct Tracker {
|
||||
pub id: Uuid,
|
||||
pub created_at: String,
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
pub fn index() -> &'static str {
|
||||
"Tracker"
|
||||
}
|
||||
Reference in New Issue
Block a user