From 62f9f672a7f8e4f475dee848a4ff39bbbb322628 Mon Sep 17 00:00:00 2001 From: Hamcha Date: Thu, 6 Jul 2023 20:39:15 +0200 Subject: [PATCH] everything is more generic now --- src/database/mod.rs | 6 +++--- src/database/page.rs | 4 ++-- src/database/site.rs | 4 ++-- src/http/repository.rs | 4 ++-- src/routes/posts.rs | 14 +++++++------- src/routes/sites.rs | 27 ++++++++++++++++----------- 6 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/database/mod.rs b/src/database/mod.rs index f05b914..fc664cc 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -6,13 +6,13 @@ use crate::state::AppState; pub mod page; pub mod site; -pub struct Repository { +pub struct Database { pool: PgPool, } -impl From<&Arc> for Repository { +impl From<&Arc> for Database { fn from(state: &Arc) -> Self { - Repository { + Database { pool: state.database.clone(), } } diff --git a/src/database/page.rs b/src/database/page.rs index 183b91b..2aff86a 100644 --- a/src/database/page.rs +++ b/src/database/page.rs @@ -5,10 +5,10 @@ use crate::content::{ page::{Page, PageRepository}, }; -use super::Repository; +use super::Database; #[async_trait] -impl PageRepository for Repository { +impl PageRepository for Database { async fn get_page_from_url(&self, site: &str, slug: &str) -> Result { let page_query = sqlx::query_as( "SELECT pages.* FROM pages JOIN sites ON site = sites.id WHERE slug = $1 AND name = $2 AND pages.deleted_at IS NULL AND sites.deleted_at IS NULL") diff --git a/src/database/site.rs b/src/database/site.rs index 0f5d1ba..a6b8235 100644 --- a/src/database/site.rs +++ b/src/database/site.rs @@ -6,10 +6,10 @@ use crate::content::{ Error, }; -use super::Repository; +use super::Database; #[async_trait] -impl SiteRepository for Repository { +impl SiteRepository for Database { async fn get_site_by_name(&self, name: &str) -> Result { let site = sqlx::query_as!( APISite, diff --git a/src/http/repository.rs b/src/http/repository.rs index 86d8a5e..a17105e 100644 --- a/src/http/repository.rs +++ b/src/http/repository.rs @@ -3,12 +3,12 @@ use std::sync::Arc; use async_trait::async_trait; use axum::{extract::FromRequestParts, http::request::Parts}; -use crate::{database::Repository, state::AppState}; +use crate::{database::Database, state::AppState}; use super::error::ApiError; #[async_trait] -impl FromRequestParts> for Repository { +impl FromRequestParts> for Database { type Rejection = ApiError<'static>; async fn from_request_parts( diff --git a/src/routes/posts.rs b/src/routes/posts.rs index c92328e..58e4c0b 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -8,20 +8,20 @@ use std::sync::Arc; use crate::{ content::page::PageRepository, - database::Repository, + database::Database, http::{error::ApiError, session::RequireUser}, state::AppState, }; -async fn get_page( - repository: Repository, +async fn get_page( + repository: Repo, Path((site, slug)): Path<(String, String)>, ) -> Result> { Ok(Json(repository.get_page_from_url(&site, &slug).await?)) } -async fn create_page( - State(state): State>, +async fn create_page( + repository: Repo, Path(site): Path, RequireUser(user): RequireUser, ) -> Result> { @@ -30,6 +30,6 @@ async fn create_page( pub fn router() -> Router> { Router::new() - .route("/:site", post(create_page)) - .route("/:site/:slug", get(get_page)) + .route("/:site", post(create_page::)) + .route("/:site/:slug", get(get_page::)) } diff --git a/src/routes/sites.rs b/src/routes/sites.rs index 1028c7b..757ddb3 100644 --- a/src/routes/sites.rs +++ b/src/routes/sites.rs @@ -9,20 +9,20 @@ use std::sync::Arc; use crate::{ content::site::{CreateSiteOptions, SiteRepository, UpdateSiteOptions}, - database::Repository, + database::Database, http::{error::ApiError, json::JsonBody, session::RequireUser}, state::AppState, }; -async fn get_site( - repository: Repository, +async fn get_site( + repository: Repo, Path(name): Path, ) -> Result> { Ok(Json(repository.get_site_by_name(&name).await?)) } -async fn create_site( - repository: Repository, +async fn create_site( + repository: Repo, RequireUser(user): RequireUser, JsonBody(options): JsonBody, ) -> Result> { @@ -30,8 +30,8 @@ async fn create_site( Ok(Json(json!({"ok": true}))) } -async fn update_site( - repository: Repository, +async fn update_site( + repository: Repo, Path(name): Path, RequireUser(user): RequireUser, JsonBody(options): JsonBody, @@ -40,8 +40,8 @@ async fn update_site( Ok(Json(json!({"ok": true}))) } -async fn delete_site( - repository: Repository, +async fn delete_site( + repository: Repo, Path(name): Path, RequireUser(user): RequireUser, ) -> Result> { @@ -51,6 +51,11 @@ async fn delete_site( pub fn router() -> Router> { Router::new() - .route("/", post(create_site)) - .route("/:name", get(get_site).put(update_site).delete(delete_site)) + .route("/", post(create_site::)) + .route( + "/:name", + get(get_site::) + .put(update_site::) + .delete(delete_site::), + ) }