Skip to content

Commit

Permalink
Refactor area model
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Sep 12, 2024
1 parent a4f31df commit bb87454
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 19 deletions.
6 changes: 1 addition & 5 deletions src/area/model.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{Error, Result};
use geojson::{GeoJson, Geometry};
use rusqlite::{named_params, Connection, OptionalExtension, Row};
use serde::Serialize;
use serde_json::{Map, Value};
use std::{
thread::sleep,
Expand All @@ -10,15 +9,12 @@ use std::{
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
use tracing::{debug, error, info};

#[derive(Debug, Eq, PartialEq, Serialize)]
#[derive(Debug, Eq, PartialEq)]
pub struct Area {
pub id: i64,
pub tags: Map<String, Value>,
#[serde(with = "time::serde::rfc3339")]
pub created_at: OffsetDateTime,
#[serde(with = "time::serde::rfc3339")]
pub updated_at: OffsetDateTime,
#[serde(with = "time::serde::rfc3339::option")]
pub deleted_at: Option<OffsetDateTime>,
}

Expand Down
7 changes: 4 additions & 3 deletions src/rpc/add_area.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::model::RpcArea;
use crate::auth::Token;
use crate::Result;
use crate::{area, discord};
use crate::{area::Area, auth::Token};
use deadpool_sqlite::Pool;
use jsonrpc_v2::{Data, Params};
use serde::Deserialize;
Expand All @@ -14,7 +15,7 @@ pub struct Args {
pub tags: Map<String, Value>,
}

pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<Area> {
pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<RpcArea> {
let token = pool
.get()
.await?
Expand All @@ -34,5 +35,5 @@ pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<Ar
);
info!(log_message);
discord::send_message_to_channel(&log_message, discord::CHANNEL_API).await;
Ok(area)
Ok(area.into())
}
5 changes: 3 additions & 2 deletions src/rpc/get_area.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::model::RpcArea;
use crate::{area::Area, auth::Token, Result};
use deadpool_sqlite::Pool;
use jsonrpc_v2::{Data, Params};
Expand All @@ -10,7 +11,7 @@ pub struct Args {
pub id: String,
}

pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<Area> {
pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<RpcArea> {
pool.get()
.await?
.interact(move |conn| Token::select_by_secret(&args.token, conn))
Expand All @@ -22,5 +23,5 @@ pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<Ar
.interact(move |conn| Area::select_by_id_or_alias(&args.id, conn))
.await??
.unwrap();
Ok(area)
Ok(area.into())
}
1 change: 1 addition & 0 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod get_element;
pub mod get_most_commented_countries;
pub mod get_trending_communities;
pub mod get_trending_countries;
pub mod model;
pub mod remove_area;
pub mod remove_area_tag;
pub mod remove_element_tag;
Expand Down
28 changes: 28 additions & 0 deletions src/rpc/model.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use crate::area::Area;
use serde::Serialize;
use serde_json::{Map, Value};
use time::OffsetDateTime;

#[derive(Debug, Eq, PartialEq, Serialize)]
pub struct RpcArea {
pub id: i64,
pub tags: Map<String, Value>,
#[serde(with = "time::serde::rfc3339")]
pub created_at: OffsetDateTime,
#[serde(with = "time::serde::rfc3339")]
pub updated_at: OffsetDateTime,
#[serde(with = "time::serde::rfc3339::option")]
pub deleted_at: Option<OffsetDateTime>,
}

impl Into<RpcArea> for Area {
fn into(self) -> RpcArea {
RpcArea {
id: self.id,
tags: self.tags,
created_at: self.created_at,
updated_at: self.updated_at,
deleted_at: self.deleted_at,
}
}
}
6 changes: 3 additions & 3 deletions src/rpc/remove_area.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::area::Area;
use super::model::RpcArea;
use crate::Result;
use crate::{area, auth::Token, discord};
use deadpool_sqlite::Pool;
Expand All @@ -13,7 +13,7 @@ pub struct Args {
pub id: String,
}

pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<Area> {
pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<RpcArea> {
let token = pool
.get()
.await?
Expand All @@ -33,5 +33,5 @@ pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<Ar
);
info!(log_message);
discord::send_message_to_channel(&log_message, discord::CHANNEL_API).await;
Ok(area)
Ok(area.into())
}
7 changes: 4 additions & 3 deletions src/rpc/remove_area_tag.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::model::RpcArea;
use crate::{
area::{self, Area},
area::{self},
auth::Token,
discord, Result,
};
Expand All @@ -16,7 +17,7 @@ pub struct Args {
pub tag: String,
}

pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<Area> {
pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<RpcArea> {
let token = pool
.get()
.await?
Expand All @@ -38,5 +39,5 @@ pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<Ar
);
info!(log_message);
discord::send_message_to_channel(&log_message, discord::CHANNEL_API).await;
Ok(area)
Ok(area.into())
}
7 changes: 4 additions & 3 deletions src/rpc/set_area_tag.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::model::RpcArea;
use crate::{
area::{self, Area},
area::{self},
auth::Token,
discord, Result,
};
Expand All @@ -18,7 +19,7 @@ pub struct Args {
pub value: Value,
}

pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<Area> {
pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<RpcArea> {
let token = pool
.get()
.await?
Expand All @@ -42,5 +43,5 @@ pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<Ar
);
info!(log_message);
discord::send_message_to_channel(&log_message, discord::CHANNEL_API).await;
Ok(area)
Ok(area.into())
}

0 comments on commit bb87454

Please sign in to comment.