diff --git a/src/event/model.rs b/src/event/model.rs index 8f058b9..9fa2b28 100644 --- a/src/event/model.rs +++ b/src/event/model.rs @@ -1,22 +1,16 @@ use crate::Error; use crate::Result; -use deadpool_sqlite::Pool; use rusqlite::named_params; use rusqlite::Connection; use rusqlite::OptionalExtension; use rusqlite::Row; use serde_json::Value; use std::collections::HashMap; -use std::sync::Arc; use time::format_description::well_known::Rfc3339; use time::OffsetDateTime; use tracing::debug; -pub struct EventRepo { - pool: Arc, -} - -#[derive(PartialEq, Debug)] +#[derive(Debug, Eq, PartialEq)] pub struct Event { pub id: i64, pub user_id: i64, @@ -30,74 +24,6 @@ pub struct Event { pub deleted_at: Option, } -impl EventRepo { - pub fn new(pool: &Arc) -> Self { - Self { pool: pool.clone() } - } - - #[cfg(test)] - pub async fn insert(&self, user_id: i64, element_id: i64, r#type: &str) -> Result { - let r#type = r#type.to_string(); - self.pool - .get() - .await? - .interact(move |conn| Event::insert(user_id, element_id, &r#type, conn)) - .await? - } - - pub async fn select_all( - &self, - sort_order: Option, - limit: Option, - ) -> Result> { - self.pool - .get() - .await? - .interact(move |conn| Event::select_all(sort_order, limit, conn)) - .await? - } - - pub async fn select_updated_since( - &self, - updated_since: &OffsetDateTime, - limit: Option, - ) -> Result> { - let updated_since = updated_since.clone(); - self.pool - .get() - .await? - .interact(move |conn| Event::select_updated_since(&updated_since, limit, conn)) - .await? - } - - pub async fn select_by_id(&self, id: i64) -> Result> { - self.pool - .get() - .await? - .interact(move |conn| Event::select_by_id(id, conn)) - .await? - } - - pub async fn _patch_tags(&self, id: i64, tags: &HashMap) -> Result { - let tags = tags.clone(); - self.pool - .get() - .await? - .interact(move |conn| Event::_patch_tags(id, &tags, conn)) - .await? - } - - #[cfg(test)] - pub async fn set_updated_at(&self, id: i64, updated_at: &OffsetDateTime) -> Result { - let updated_at = updated_at.clone(); - self.pool - .get() - .await? - .interact(move |conn| Event::_set_updated_at(id, &updated_at, conn)) - .await? - } -} - const TABLE: &str = "event"; const _ALL_COLUMNS: &str = "rowid, user_id, element_id, type, tags, created_at, updated_at, deleted_at"; @@ -298,12 +224,7 @@ impl Event { } #[cfg(test)] - pub fn set_updated_at(&self, updated_at: &OffsetDateTime, conn: &Connection) -> Result { - Event::_set_updated_at(self.id, updated_at, conn) - } - - #[cfg(test)] - pub fn _set_updated_at( + pub fn set_updated_at( id: i64, updated_at: &OffsetDateTime, conn: &Connection, @@ -397,15 +318,14 @@ mod test { let conn = mock_conn(); let user = User::insert(1, &OsmUser::mock(), &conn)?; let element = Element::insert(&OverpassElement::mock(1), &conn)?; - Event::insert(user.id, element.id, "", &conn)? - .set_updated_at(&datetime!(2020-01-01 00:00 UTC), &conn)?; + let event_1 = Event::insert(user.id, element.id, "", &conn)?; + let _event_1 = Event::set_updated_at(event_1.id, &datetime!(2020-01-01 00:00 UTC), &conn)?; + let event_2 = Event::insert(1, element.id, "", &conn)?; + let event_2 = Event::set_updated_at(event_2.id, &datetime!(2020-01-02 00:00 UTC), &conn)?; + let event_3 = Event::insert(1, element.id, "", &conn)?; + let event_3 = Event::set_updated_at(event_3.id, &datetime!(2020-01-03 00:00 UTC), &conn)?; assert_eq!( - vec![ - Event::insert(1, element.id, "", &conn)? - .set_updated_at(&datetime!(2020-01-02 00:00 UTC), &conn)?, - Event::insert(1, element.id, "", &conn)? - .set_updated_at(&datetime!(2020-01-03 00:00 UTC), &conn)?, - ], + vec![event_2, event_3,], Event::select_updated_since(&datetime!(2020-01-01 00:00 UTC), None, &conn,)? ); Ok(()) @@ -453,8 +373,8 @@ mod test { let updated_at = OffsetDateTime::now_utc(); let user = User::insert(1, &OsmUser::mock(), &conn)?; let element = Element::insert(&OverpassElement::mock(1), &conn)?; - let event = - Event::insert(user.id, element.id, "", &conn)?.set_updated_at(&updated_at, &conn)?; + let event = Event::insert(user.id, element.id, "", &conn)?; + let event = Event::set_updated_at(event.id, &updated_at, &conn)?; assert_eq!( updated_at, Event::select_by_id(event.id, &conn)?.unwrap().updated_at, diff --git a/src/event/v2.rs b/src/event/v2.rs index 418f470..39b44ca 100644 --- a/src/event/v2.rs +++ b/src/event/v2.rs @@ -1,5 +1,4 @@ use super::Event; -use crate::event::model::EventRepo; use crate::Error; use actix_web::get; use actix_web::web::Data; @@ -8,10 +7,12 @@ use actix_web::web::Path; use actix_web::web::Query; use actix_web::web::Redirect; use actix_web::Either; +use deadpool_sqlite::Pool; use serde::Deserialize; use serde::Serialize; use serde_json::Value; use std::collections::HashMap; +use std::sync::Arc; use time::format_description::well_known::Rfc3339; use time::Duration; use time::OffsetDateTime; @@ -66,40 +67,39 @@ impl Into> for Event { #[get("")] pub async fn get( args: Query, - repo: Data, + pool: Data>, ) -> Result>, Redirect>, Error> { if args.limit.is_none() && args.updated_since.is_none() { return Ok(Either::Right( Redirect::to("https://static.btcmap.org/api/v2/events.json").permanent(), )); } - - Ok(Either::Left(Json(match &args.updated_since { - Some(updated_since) => repo - .select_updated_since(updated_since, args.limit) - .await? - .into_iter() - .map(|it| it.into()) - .collect(), - None => repo - .select_updated_since( + let events = pool + .get() + .await? + .interact(move |conn| match &args.updated_since { + Some(updated_since) => Event::select_updated_since(updated_since, args.limit, conn), + None => Event::select_updated_since( &OffsetDateTime::now_utc() .checked_sub(Duration::days(30)) .unwrap(), args.limit, - ) - .await? - .into_iter() - .map(|it| it.into()) - .collect(), - }))) + conn, + ), + }) + .await??; + Ok(Either::Left(Json( + events.into_iter().map(|it| it.into()).collect(), + ))) } #[get("{id}")] -pub async fn get_by_id(id: Path, repo: Data) -> Result, Error> { +pub async fn get_by_id(id: Path, pool: Data>) -> Result, Error> { let id = id.into_inner(); - repo.select_by_id(id) + pool.get() .await? + .interact(move |conn| Event::select_by_id(id, conn)) + .await?? .map(|it| it.into()) .ok_or(Error::HttpNotFound(format!( "Event with id = {id} doesn't exist" @@ -109,6 +109,7 @@ pub async fn get_by_id(id: Path, repo: Data) -> Result> for Event { } #[get("")] -pub async fn get(args: Query, repo: Data) -> Result>, Error> { - match &args.updated_since { - Some(updated_since) => Ok(Json( - repo.select_updated_since(&updated_since, Some(args.limit.unwrap_or(100))) - .await? - .into_iter() - .map(|it| it.into()) - .collect(), - )), - None => Ok(Json( - repo.select_all(Some("DESC".into()), Some(args.limit.unwrap_or(100))) - .await? - .into_iter() - .map(|it| it.into()) - .collect(), - )), - } +pub async fn get(args: Query, pool: Data>) -> Result>, Error> { + let events = pool + .get() + .await? + .interact(move |conn| match args.updated_since { + Some(updated_since) => { + Event::select_updated_since(&updated_since, Some(args.limit.unwrap_or(100)), conn) + } + None => Event::select_all(Some("DESC".into()), Some(args.limit.unwrap_or(100)), conn), + }) + .await??; + Ok(Json(events.into_iter().map(|it| it.into()).collect())) } #[get("{id}")] -pub async fn get_by_id(id: Path, repo: Data) -> Result, Error> { +pub async fn get_by_id(id: Path, pool: Data>) -> Result, Error> { let id = id.into_inner(); - repo.select_by_id(id) + pool.get() .await? + .interact(move |conn| Event::select_by_id(id, conn)) + .await?? .map(|it| it.into()) .ok_or(Error::HttpNotFound(format!( "Event with id = {id} doesn't exist" @@ -127,6 +125,7 @@ pub async fn get_by_id(id: Path, repo: Data) -> Result Result<()> { HttpServer::new(move || { let element_repo = ElementRepo::new(&pool); - let event_repo = EventRepo::new(&pool); App::new() .wrap_fn(|req, srv| { @@ -82,7 +80,6 @@ pub async fn run() -> Result<()> { .wrap(Compress::default()) .app_data(Data::new(pool.clone())) .app_data(Data::new(element_repo)) - .app_data(Data::new(event_repo)) .app_data(QueryConfig::default().error_handler(error::query_error_handler)) .service( scope("tiles") diff --git a/src/test.rs b/src/test.rs index 33ba236..efebe51 100644 --- a/src/test.rs +++ b/src/test.rs @@ -1,4 +1,4 @@ -use crate::{command::db, element::ElementRepo, event::model::EventRepo}; +use crate::{command::db, element::ElementRepo}; use deadpool_sqlite::{Config, Pool, Runtime}; use rusqlite::Connection; use serde_json::{json, Map, Value}; @@ -26,7 +26,6 @@ pub async fn mock_state() -> State { conn: db.0, pool: pool.clone(), element_repo: ElementRepo::new(&pool), - event_repo: EventRepo::new(&pool), } } @@ -51,7 +50,6 @@ pub struct State { pub conn: Connection, pub pool: Arc, pub element_repo: ElementRepo, - pub event_repo: EventRepo, } pub fn mock_tags() -> Map {