Skip to content

Commit

Permalink
Refactor user model
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Aug 19, 2024
1 parent 3781991 commit 778b5e1
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 155 deletions.
9 changes: 5 additions & 4 deletions src/event/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ mod test {
use crate::osm::osm::OsmUser;
use crate::osm::overpass::OverpassElement;
use crate::test::mock_state;
use crate::user::User;
use crate::Result;
use actix_web::test::TestRequest;
use actix_web::web::{scope, Data};
Expand All @@ -137,7 +138,7 @@ mod test {
#[test]
async fn get_one_row() -> Result<()> {
let state = mock_state().await;
let user = state.user_repo.insert(1, &OsmUser::mock()).await?;
let user = User::insert(1, &OsmUser::mock(), &state.conn)?;
let element = state.element_repo.insert(&OverpassElement::mock(1)).await?;
state.event_repo.insert(user.id, element.id, "").await?;
let app = test::init_service(
Expand All @@ -155,7 +156,7 @@ mod test {
#[test]
async fn get_with_limit() -> Result<()> {
let state = mock_state().await;
state.user_repo.insert(1, &OsmUser::mock()).await?;
User::insert(1, &OsmUser::mock(), &state.conn)?;
state.element_repo.insert(&OverpassElement::mock(1)).await?;
state.event_repo.insert(1, 1, "").await?;
state.event_repo.insert(1, 1, "").await?;
Expand All @@ -175,7 +176,7 @@ mod test {
#[test]
async fn get_updated_since() -> Result<()> {
let state = mock_state().await;
state.user_repo.insert(1, &OsmUser::mock()).await?;
User::insert(1, &OsmUser::mock(), &state.conn)?;
state.element_repo.insert(&OverpassElement::mock(1)).await?;
let event_1 = state.event_repo.insert(1, 1, "").await?;
state
Expand Down Expand Up @@ -205,7 +206,7 @@ mod test {
async fn get_by_id() -> Result<()> {
let state = mock_state().await;
let event_id = 1;
let user = state.user_repo.insert(1, &OsmUser::mock()).await?;
let user = User::insert(1, &OsmUser::mock(), &state.conn)?;
let element = state.element_repo.insert(&OverpassElement::mock(1)).await?;
state.event_repo.insert(user.id, element.id, "").await?;
let app = test::init_service(
Expand Down
7 changes: 4 additions & 3 deletions src/event/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ mod test {
use crate::osm::osm::OsmUser;
use crate::osm::overpass::OverpassElement;
use crate::test::mock_state;
use crate::user::User;
use crate::Result;
use actix_web::test::TestRequest;
use actix_web::web::{scope, Data};
Expand All @@ -156,7 +157,7 @@ mod test {
#[test]
async fn get_not_empty_array() -> Result<()> {
let state = mock_state().await;
let user = state.user_repo.insert(1, &OsmUser::mock()).await?;
let user = User::insert(1, &OsmUser::mock(), &state.conn)?;
let element = state.element_repo.insert(&OverpassElement::mock(1)).await?;
let event = state.event_repo.insert(user.id, element.id, "").await?;
let app = test::init_service(
Expand All @@ -176,7 +177,7 @@ mod test {
#[test]
async fn get_with_limit() -> Result<()> {
let state = mock_state().await;
let user = state.user_repo.insert(1, &OsmUser::mock()).await?;
let user = User::insert(1, &OsmUser::mock(), &state.conn)?;
let element = state.element_repo.insert(&OverpassElement::mock(1)).await?;
let event_1 = state.event_repo.insert(user.id, element.id, "").await?;
let event_2 = state.event_repo.insert(user.id, element.id, "").await?;
Expand All @@ -198,7 +199,7 @@ mod test {
#[test]
async fn get_updated_since() -> Result<()> {
let state = mock_state().await;
let user = state.user_repo.insert(1, &OsmUser::mock()).await?;
let user = User::insert(1, &OsmUser::mock(), &state.conn)?;
let element = state.element_repo.insert(&OverpassElement::mock(1)).await?;
let event_1 = state.event_repo.insert(user.id, element.id, "").await?;
state
Expand Down
3 changes: 0 additions & 3 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use super::db;
use crate::element::ElementRepo;
use crate::event::model::EventRepo;
use crate::report::model::ReportRepo;
use crate::user::UserRepo;
use crate::{area, element, error, user};
use crate::{event, tile};
use crate::{report, Result};
Expand Down Expand Up @@ -43,7 +42,6 @@ pub async fn run() -> Result<()> {
let element_repo = ElementRepo::new(&pool);
let event_repo = EventRepo::new(&pool);
let report_repo = ReportRepo::new(&pool);
let user_repo = UserRepo::new(&pool);

App::new()
.wrap_fn(|req, srv| {
Expand Down Expand Up @@ -88,7 +86,6 @@ pub async fn run() -> Result<()> {
.app_data(Data::new(element_repo))
.app_data(Data::new(event_repo))
.app_data(Data::new(report_repo))
.app_data(Data::new(user_repo))
.app_data(QueryConfig::default().error_handler(error::query_error_handler))
.service(
scope("tiles")
Expand Down
3 changes: 0 additions & 3 deletions src/test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
command::db, element::ElementRepo, event::model::EventRepo, report::model::ReportRepo,
user::UserRepo,
};
use deadpool_sqlite::{Config, Pool, Runtime};
use rusqlite::Connection;
Expand Down Expand Up @@ -31,7 +30,6 @@ pub async fn mock_state() -> State {
element_repo: ElementRepo::new(&pool),
event_repo: EventRepo::new(&pool),
report_repo: ReportRepo::new(&pool),
user_repo: UserRepo::new(&pool),
}
}

Expand All @@ -58,7 +56,6 @@ pub struct State {
pub element_repo: ElementRepo,
pub event_repo: EventRepo,
pub report_repo: ReportRepo,
pub user_repo: UserRepo,
}

pub fn mock_tags() -> Map<String, Value> {
Expand Down
20 changes: 13 additions & 7 deletions src/user/admin.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
auth::{self},
discord,
user::UserRepo,
user::User,
Error,
};
use actix_web::{
Expand All @@ -20,17 +20,23 @@ pub async fn patch_tags(
id: Path<i64>,
args: Json<HashMap<String, Value>>,
pool: Data<Arc<Pool>>,
repo: Data<UserRepo>,
) -> Result<impl Responder, Error> {
let token = auth::service::check(&req, &pool).await?;
repo.select_by_id(*id)
let id = id.into_inner();
pool.get()
.await?
.interact(move |conn| User::select_by_id(id, conn))
.await??
.ok_or(Error::HttpNotFound(format!(
"User with id = {id} doesn't exist"
)))?;
repo.patch_tags(*id, &args).await?;
let cloned_args = args.clone();
pool.get()
.await?
.interact(move |conn| User::patch_tags(id, &cloned_args, conn))
.await??;
let log_message = format!(
"User {} patched tags for user https://api.btcmap.org/v2/users/{} {}",
"User {} patched tags for user https://api.btcmap.org/v3/users/{} {}",
token.owner,
id,
serde_json::to_string_pretty(&args).unwrap(),
Expand All @@ -44,6 +50,7 @@ pub async fn patch_tags(
mod test {
use crate::osm::osm::OsmUser;
use crate::test::mock_state;
use crate::user::User;
use crate::{auth, Result};
use actix_web::http::StatusCode;
use actix_web::test::TestRequest;
Expand All @@ -54,12 +61,11 @@ mod test {
#[test]
async fn patch_tags() -> Result<()> {
let state = mock_state().await;
let user = state.user_repo.insert(1, &OsmUser::mock()).await?;
let user = User::insert(1, &OsmUser::mock(), &state.conn)?;
let token = auth::service::mock_token("test", &state.pool).await.secret;
let app = test::init_service(
App::new()
.app_data(Data::new(state.pool))
.app_data(Data::new(state.user_repo))
.service(super::patch_tags),
)
.await;
Expand Down
1 change: 0 additions & 1 deletion src/user/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod model;
pub use model::User;
pub use model::UserRepo;
pub mod admin;
pub mod v2;
pub mod v3;
78 changes: 1 addition & 77 deletions src/user/model.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
use crate::{osm::osm::OsmUser, Error, Result};
use deadpool_sqlite::Pool;
use rusqlite::{named_params, Connection, OptionalExtension, Row};
use serde_json::{Map, Value};
use std::{collections::HashMap, sync::Arc};
use std::collections::HashMap;
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
#[cfg(test)]
use tracing::debug;

pub struct UserRepo {
pool: Arc<Pool>,
}

pub struct User {
pub id: i64,
pub osm_data: OsmUser,
Expand All @@ -20,77 +15,6 @@ pub struct User {
pub deleted_at: Option<OffsetDateTime>,
}

impl UserRepo {
pub fn new(pool: &Arc<Pool>) -> Self {
Self { pool: pool.clone() }
}

#[cfg(test)]
pub fn mock() -> Self {
Self {
pool: Arc::new(crate::test::mock_db().1),
}
}

#[cfg(test)]
pub async fn insert(&self, id: i64, osm_data: &OsmUser) -> Result<User> {
let osm_data = osm_data.clone();
self.pool
.get()
.await?
.interact(move |conn| User::insert(id, &osm_data, conn))
.await?
}

pub async fn select_all(&self, limit: Option<i64>) -> Result<Vec<User>> {
self.pool
.get()
.await?
.interact(move |conn| User::select_all(limit, conn))
.await?
}

pub async fn select_updated_since(
&self,
updated_since: &OffsetDateTime,
limit: Option<i64>,
) -> Result<Vec<User>> {
let updated_since = updated_since.clone();
self.pool
.get()
.await?
.interact(move |conn| User::select_updated_since(&updated_since, limit, conn))
.await?
}

pub async fn select_by_id(&self, id: i64) -> Result<Option<User>> {
self.pool
.get()
.await?
.interact(move |conn| User::select_by_id(id, conn))
.await?
}

pub async fn patch_tags(&self, id: i64, tags: &HashMap<String, Value>) -> Result<User> {
let tags = tags.clone();
self.pool
.get()
.await?
.interact(move |conn| User::patch_tags(id, &tags, conn))
.await?
}

#[cfg(test)]
pub async fn set_updated_at(&self, id: i64, updated_at: &OffsetDateTime) -> Result<User> {
let updated_at = updated_at.clone();
self.pool
.get()
.await?
.interact(move |conn| User::_set_updated_at(id, &updated_at, conn))
.await?
}
}

impl User {
pub fn insert(id: i64, osm_data: &OsmUser, conn: &Connection) -> Result<User> {
let query = r#"
Expand Down
Loading

0 comments on commit 778b5e1

Please sign in to comment.