Skip to content

Commit

Permalink
Add plot samples
Browse files Browse the repository at this point in the history
  • Loading branch information
evanjt committed Sep 18, 2024
1 parent fdaaf27 commit 015ffc9
Show file tree
Hide file tree
Showing 7 changed files with 407 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/areas/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ pub struct Area {
geom: Option<Value>,
}

#[derive(ToSchema, Serialize)]
pub struct AreaBasicWithProject {
pub id: Uuid,
pub name: Option<String>,
pub project: crate::common::models::GenericNameAndID,
}

// impl Sensor {
// pub fn from(sensor_db: crate::db::sensor::Model) -> Self {
// Sensor {
Expand Down
9 changes: 9 additions & 0 deletions src/common/models.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
use sea_orm::FromQueryResult;
use serde::Deserialize;
use serde::Serialize;
use utoipa::ToSchema;
use uuid::Uuid;

#[derive(ToSchema, Deserialize, Default)]
pub struct FilterOptions {
pub filter: Option<String>, // JSON-encoded filter
pub range: Option<String>, // range in the format "[0,24]"
pub sort: Option<String>, // sort in the format '["id", "ASC"]'
}

#[derive(ToSchema, Serialize, FromQueryResult)]
pub struct GenericNameAndID {
pub id: Uuid,
pub name: String,
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ async fn main() {
.nest("/v1/plots", plots::views::router(db.clone()))
.nest("/v1/areas", areas::views::router(db.clone()))
.nest("/v1/projects", projects::views::router(db.clone()))
.nest("/v1/plot_samples", samples::views::router(db.clone()))
.merge(SwaggerUi::new("/swagger-ui").url("/api-docs/openapi.json", ApiDoc::openapi()))
.merge(Redoc::with_url("/redoc", ApiDoc::openapi()))
.merge(Scalar::with_url("/scalar", ApiDoc::openapi()));
Expand Down
8 changes: 8 additions & 0 deletions src/plots/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ pub struct PlotSimple {
pub coord_y: Option<f64>,
pub coord_z: Option<f64>,
}
#[derive(ToSchema, Serialize)]
pub struct PlotBasicWithAreaAndProject {
pub id: Uuid,
pub name: String,
pub area: crate::areas::models::AreaBasicWithProject,
}

#[derive(ToSchema, Serialize)]
pub struct Plot {
id: Uuid,
Expand Down Expand Up @@ -78,6 +85,7 @@ impl From<areas::db::Model> for Area {
}
}
}

impl From<(PlotWithCoords, Option<Area>)> for Plot {
fn from((plot_db, area_db_vec): (PlotWithCoords, Option<Area>)) -> Self {
let area = area_db_vec.into_iter().next().map_or(
Expand Down
2 changes: 2 additions & 0 deletions src/samples/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
pub mod db;
pub mod models;
pub mod views;
228 changes: 228 additions & 0 deletions src/samples/models.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
use sea_orm::ColumnTrait;
use sea_orm::Condition;
use sea_orm::EntityTrait;
use sea_orm::{query::*, DatabaseConnection, QueryFilter, QueryOrder};
use sea_query::Order;
use serde::Serialize;
use utoipa::ToSchema;
use uuid::Uuid;

#[derive(ToSchema, Serialize)]
pub struct PlotSample {
pub id: Uuid,
pub name: String,
pub upper_depth_cm: f64,
pub lower_depth_cm: f64,
pub plot_id: Uuid,
pub sample_weight: Option<f64>,
pub subsample_weight: Option<f64>,
pub ph: Option<f64>,
pub rh: Option<f64>,
pub loi: Option<f64>,
pub mfc: Option<f64>,
pub c: Option<f64>,
pub n: Option<f64>,
pub cn: Option<f64>,
pub clay_percent: Option<f64>,
pub silt_percent: Option<f64>,
pub sand_percent: Option<f64>,
pub fe_ug_per_g: Option<f64>,
pub na_ug_per_g: Option<f64>,
pub al_ug_per_g: Option<f64>,
pub k_ug_per_g: Option<f64>,
pub ca_ug_per_g: Option<f64>,
pub mg_ug_per_g: Option<f64>,
pub mn_ug_per_g: Option<f64>,
pub s_ug_per_g: Option<f64>,
pub cl_ug_per_g: Option<f64>,
pub p_ug_per_g: Option<f64>,
pub si_ug_per_g: Option<f64>,
pub subsample_replica_weight: Option<f64>,
pub fungi_per_g: Option<f64>,
pub bacteria_per_g: Option<f64>,
pub archea_per_g: Option<f64>,
pub methanogens_per_g: Option<f64>,
pub methanotrophs_per_g: Option<f64>,
pub replicate: i32,
pub plot: crate::plots::models::PlotBasicWithAreaAndProject,
}

impl PlotSample {
pub async fn get_all(
db: &DatabaseConnection,
condition: Condition,
order_column: <super::db::Entity as sea_orm::EntityTrait>::Column,
order_direction: Order,
offset: u64,
limit: u64,
) -> Vec<Self> {
let samples = crate::samples::db::Entity::find()
.filter(condition)
.order_by(order_column, order_direction)
.offset(offset)
.limit(limit)
.all(db)
.await
.unwrap();

let mut plot_samples: Vec<PlotSample> = Vec::new();

for sample in samples {
let plot_obj = crate::plots::db::Entity::find()
.filter(crate::plots::db::Column::Id.eq(sample.plot_id))
.one(db)
.await
.unwrap()
.unwrap();

let area = crate::areas::db::Entity::find()
.filter(crate::areas::db::Column::Id.eq(plot_obj.area_id))
.one(db)
.await
.unwrap()
.unwrap();

let project = crate::projects::db::Entity::find()
.filter(crate::projects::db::Column::Id.eq(area.project_id))
.one(db)
.await
.unwrap()
.unwrap();

let plot = crate::plots::models::PlotBasicWithAreaAndProject {
id: plot_obj.id,
name: plot_obj.name,
area: crate::areas::models::AreaBasicWithProject {
id: area.id,
name: area.name,
project: crate::common::models::GenericNameAndID {
id: project.id,
name: project.name,
},
},
};

plot_samples.push(PlotSample {
id: sample.id,
name: sample.name,
upper_depth_cm: sample.upper_depth_cm,
lower_depth_cm: sample.lower_depth_cm,
plot_id: sample.plot_id,
sample_weight: sample.sample_weight,
subsample_weight: sample.subsample_weight,
ph: sample.ph,
rh: sample.rh,
loi: sample.loi,
mfc: sample.mfc,
c: sample.c,
n: sample.n,
cn: sample.cn,
clay_percent: sample.clay_percent,
silt_percent: sample.silt_percent,
sand_percent: sample.sand_percent,
fe_ug_per_g: sample.fe_ug_per_g,
na_ug_per_g: sample.na_ug_per_g,
al_ug_per_g: sample.al_ug_per_g,
k_ug_per_g: sample.k_ug_per_g,
ca_ug_per_g: sample.ca_ug_per_g,
mg_ug_per_g: sample.mg_ug_per_g,
mn_ug_per_g: sample.mn_ug_per_g,
s_ug_per_g: sample.s_ug_per_g,
cl_ug_per_g: sample.cl_ug_per_g,
p_ug_per_g: sample.p_ug_per_g,
si_ug_per_g: sample.si_ug_per_g,
subsample_replica_weight: sample.subsample_replica_weight,
fungi_per_g: sample.fungi_per_g,
bacteria_per_g: sample.bacteria_per_g,
archea_per_g: sample.archea_per_g,
methanogens_per_g: sample.methanogens_per_g,
methanotrophs_per_g: sample.methanotrophs_per_g,
replicate: sample.replicate,
plot: plot,
});
}
plot_samples
}

pub async fn get_one(id: Uuid, db: &DatabaseConnection) -> Option<Self> {
let sample = crate::samples::db::Entity::find()
.filter(crate::samples::db::Column::Id.eq(id))
.one(db)
.await
.unwrap()
.unwrap();

let plot_obj = crate::plots::db::Entity::find()
.filter(crate::plots::db::Column::Id.eq(sample.plot_id))
.one(db)
.await
.unwrap()
.unwrap();

let area = crate::areas::db::Entity::find()
.filter(crate::areas::db::Column::Id.eq(plot_obj.area_id))
.one(db)
.await
.unwrap()
.unwrap();

let project = crate::projects::db::Entity::find()
.filter(crate::projects::db::Column::Id.eq(area.project_id))
.one(db)
.await
.unwrap()
.unwrap();

let plot = crate::plots::models::PlotBasicWithAreaAndProject {
id: plot_obj.id,
name: plot_obj.name,
area: crate::areas::models::AreaBasicWithProject {
id: area.id,
name: area.name,
project: crate::common::models::GenericNameAndID {
id: project.id,
name: project.name,
},
},
};

Some(PlotSample {
id: sample.id,
name: sample.name,
upper_depth_cm: sample.upper_depth_cm,
lower_depth_cm: sample.lower_depth_cm,
plot_id: sample.plot_id,
sample_weight: sample.sample_weight,
subsample_weight: sample.subsample_weight,
ph: sample.ph,
rh: sample.rh,
loi: sample.loi,
mfc: sample.mfc,
c: sample.c,
n: sample.n,
cn: sample.cn,
clay_percent: sample.clay_percent,
silt_percent: sample.silt_percent,
sand_percent: sample.sand_percent,
fe_ug_per_g: sample.fe_ug_per_g,
na_ug_per_g: sample.na_ug_per_g,
al_ug_per_g: sample.al_ug_per_g,
k_ug_per_g: sample.k_ug_per_g,
ca_ug_per_g: sample.ca_ug_per_g,
mg_ug_per_g: sample.mg_ug_per_g,
mn_ug_per_g: sample.mn_ug_per_g,
s_ug_per_g: sample.s_ug_per_g,
cl_ug_per_g: sample.cl_ug_per_g,
p_ug_per_g: sample.p_ug_per_g,
si_ug_per_g: sample.si_ug_per_g,
subsample_replica_weight: sample.subsample_replica_weight,
fungi_per_g: sample.fungi_per_g,
bacteria_per_g: sample.bacteria_per_g,
archea_per_g: sample.archea_per_g,
methanogens_per_g: sample.methanogens_per_g,
methanotrophs_per_g: sample.methanotrophs_per_g,
replicate: sample.replicate,
plot: plot,
})
}
}
Loading

0 comments on commit 015ffc9

Please sign in to comment.