everything is more generic now
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Hamcha 2023-07-06 20:39:15 +02:00
parent 58bf506240
commit 62f9f672a7
Signed by: hamcha
GPG key ID: 1669C533B8CF6D89
6 changed files with 32 additions and 27 deletions

View file

@ -6,13 +6,13 @@ use crate::state::AppState;
pub mod page; pub mod page;
pub mod site; pub mod site;
pub struct Repository { pub struct Database {
pool: PgPool, pool: PgPool,
} }
impl From<&Arc<AppState>> for Repository { impl From<&Arc<AppState>> for Database {
fn from(state: &Arc<AppState>) -> Self { fn from(state: &Arc<AppState>) -> Self {
Repository { Database {
pool: state.database.clone(), pool: state.database.clone(),
} }
} }

View file

@ -5,10 +5,10 @@ use crate::content::{
page::{Page, PageRepository}, page::{Page, PageRepository},
}; };
use super::Repository; use super::Database;
#[async_trait] #[async_trait]
impl PageRepository for Repository { impl PageRepository for Database {
async fn get_page_from_url(&self, site: &str, slug: &str) -> Result<Page, content::Error> { async fn get_page_from_url(&self, site: &str, slug: &str) -> Result<Page, content::Error> {
let page_query = sqlx::query_as( 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") "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")

View file

@ -6,10 +6,10 @@ use crate::content::{
Error, Error,
}; };
use super::Repository; use super::Database;
#[async_trait] #[async_trait]
impl SiteRepository for Repository { impl SiteRepository for Database {
async fn get_site_by_name(&self, name: &str) -> Result<APISite, Error> { async fn get_site_by_name(&self, name: &str) -> Result<APISite, Error> {
let site = sqlx::query_as!( let site = sqlx::query_as!(
APISite, APISite,

View file

@ -3,12 +3,12 @@ use std::sync::Arc;
use async_trait::async_trait; use async_trait::async_trait;
use axum::{extract::FromRequestParts, http::request::Parts}; use axum::{extract::FromRequestParts, http::request::Parts};
use crate::{database::Repository, state::AppState}; use crate::{database::Database, state::AppState};
use super::error::ApiError; use super::error::ApiError;
#[async_trait] #[async_trait]
impl FromRequestParts<Arc<AppState>> for Repository { impl FromRequestParts<Arc<AppState>> for Database {
type Rejection = ApiError<'static>; type Rejection = ApiError<'static>;
async fn from_request_parts( async fn from_request_parts(

View file

@ -8,20 +8,20 @@ use std::sync::Arc;
use crate::{ use crate::{
content::page::PageRepository, content::page::PageRepository,
database::Repository, database::Database,
http::{error::ApiError, session::RequireUser}, http::{error::ApiError, session::RequireUser},
state::AppState, state::AppState,
}; };
async fn get_page( async fn get_page<Repo: PageRepository>(
repository: Repository, repository: Repo,
Path((site, slug)): Path<(String, String)>, Path((site, slug)): Path<(String, String)>,
) -> Result<impl IntoResponse, ApiError<'static>> { ) -> Result<impl IntoResponse, ApiError<'static>> {
Ok(Json(repository.get_page_from_url(&site, &slug).await?)) Ok(Json(repository.get_page_from_url(&site, &slug).await?))
} }
async fn create_page( async fn create_page<Repo: PageRepository>(
State(state): State<Arc<AppState>>, repository: Repo,
Path(site): Path<String>, Path(site): Path<String>,
RequireUser(user): RequireUser, RequireUser(user): RequireUser,
) -> Result<impl IntoResponse, ApiError<'static>> { ) -> Result<impl IntoResponse, ApiError<'static>> {
@ -30,6 +30,6 @@ async fn create_page(
pub fn router() -> Router<Arc<AppState>> { pub fn router() -> Router<Arc<AppState>> {
Router::new() Router::new()
.route("/:site", post(create_page)) .route("/:site", post(create_page::<Database>))
.route("/:site/:slug", get(get_page)) .route("/:site/:slug", get(get_page::<Database>))
} }

View file

@ -9,20 +9,20 @@ use std::sync::Arc;
use crate::{ use crate::{
content::site::{CreateSiteOptions, SiteRepository, UpdateSiteOptions}, content::site::{CreateSiteOptions, SiteRepository, UpdateSiteOptions},
database::Repository, database::Database,
http::{error::ApiError, json::JsonBody, session::RequireUser}, http::{error::ApiError, json::JsonBody, session::RequireUser},
state::AppState, state::AppState,
}; };
async fn get_site( async fn get_site<Repo: SiteRepository>(
repository: Repository, repository: Repo,
Path(name): Path<String>, Path(name): Path<String>,
) -> Result<impl IntoResponse, ApiError<'static>> { ) -> Result<impl IntoResponse, ApiError<'static>> {
Ok(Json(repository.get_site_by_name(&name).await?)) Ok(Json(repository.get_site_by_name(&name).await?))
} }
async fn create_site( async fn create_site<Repo: SiteRepository>(
repository: Repository, repository: Repo,
RequireUser(user): RequireUser, RequireUser(user): RequireUser,
JsonBody(options): JsonBody<CreateSiteOptions>, JsonBody(options): JsonBody<CreateSiteOptions>,
) -> Result<impl IntoResponse, ApiError<'static>> { ) -> Result<impl IntoResponse, ApiError<'static>> {
@ -30,8 +30,8 @@ async fn create_site(
Ok(Json(json!({"ok": true}))) Ok(Json(json!({"ok": true})))
} }
async fn update_site( async fn update_site<Repo: SiteRepository>(
repository: Repository, repository: Repo,
Path(name): Path<String>, Path(name): Path<String>,
RequireUser(user): RequireUser, RequireUser(user): RequireUser,
JsonBody(options): JsonBody<UpdateSiteOptions>, JsonBody(options): JsonBody<UpdateSiteOptions>,
@ -40,8 +40,8 @@ async fn update_site(
Ok(Json(json!({"ok": true}))) Ok(Json(json!({"ok": true})))
} }
async fn delete_site( async fn delete_site<Repo: SiteRepository>(
repository: Repository, repository: Repo,
Path(name): Path<String>, Path(name): Path<String>,
RequireUser(user): RequireUser, RequireUser(user): RequireUser,
) -> Result<impl IntoResponse, ApiError<'static>> { ) -> Result<impl IntoResponse, ApiError<'static>> {
@ -51,6 +51,11 @@ async fn delete_site(
pub fn router() -> Router<Arc<AppState>> { pub fn router() -> Router<Arc<AppState>> {
Router::new() Router::new()
.route("/", post(create_site)) .route("/", post(create_site::<Database>))
.route("/:name", get(get_site).put(update_site).delete(delete_site)) .route(
"/:name",
get(get_site::<Database>)
.put(update_site::<Database>)
.delete(delete_site::<Database>),
)
} }