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 site;
pub struct Repository {
pub struct Database {
pool: PgPool,
}
impl From<&Arc<AppState>> for Repository {
impl From<&Arc<AppState>> for Database {
fn from(state: &Arc<AppState>) -> Self {
Repository {
Database {
pool: state.database.clone(),
}
}

View file

@ -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<Page, content::Error> {
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")

View file

@ -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<APISite, Error> {
let site = sqlx::query_as!(
APISite,

View file

@ -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<Arc<AppState>> for Repository {
impl FromRequestParts<Arc<AppState>> for Database {
type Rejection = ApiError<'static>;
async fn from_request_parts(

View file

@ -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<Repo: PageRepository>(
repository: Repo,
Path((site, slug)): Path<(String, String)>,
) -> Result<impl IntoResponse, ApiError<'static>> {
Ok(Json(repository.get_page_from_url(&site, &slug).await?))
}
async fn create_page(
State(state): State<Arc<AppState>>,
async fn create_page<Repo: PageRepository>(
repository: Repo,
Path(site): Path<String>,
RequireUser(user): RequireUser,
) -> Result<impl IntoResponse, ApiError<'static>> {
@ -30,6 +30,6 @@ async fn create_page(
pub fn router() -> Router<Arc<AppState>> {
Router::new()
.route("/:site", post(create_page))
.route("/:site/:slug", get(get_page))
.route("/:site", post(create_page::<Database>))
.route("/:site/:slug", get(get_page::<Database>))
}

View file

@ -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<Repo: SiteRepository>(
repository: Repo,
Path(name): Path<String>,
) -> Result<impl IntoResponse, ApiError<'static>> {
Ok(Json(repository.get_site_by_name(&name).await?))
}
async fn create_site(
repository: Repository,
async fn create_site<Repo: SiteRepository>(
repository: Repo,
RequireUser(user): RequireUser,
JsonBody(options): JsonBody<CreateSiteOptions>,
) -> Result<impl IntoResponse, ApiError<'static>> {
@ -30,8 +30,8 @@ async fn create_site(
Ok(Json(json!({"ok": true})))
}
async fn update_site(
repository: Repository,
async fn update_site<Repo: SiteRepository>(
repository: Repo,
Path(name): Path<String>,
RequireUser(user): RequireUser,
JsonBody(options): JsonBody<UpdateSiteOptions>,
@ -40,8 +40,8 @@ async fn update_site(
Ok(Json(json!({"ok": true})))
}
async fn delete_site(
repository: Repository,
async fn delete_site<Repo: SiteRepository>(
repository: Repo,
Path(name): Path<String>,
RequireUser(user): RequireUser,
) -> Result<impl IntoResponse, ApiError<'static>> {
@ -51,6 +51,11 @@ async fn delete_site(
pub fn router() -> Router<Arc<AppState>> {
Router::new()
.route("/", post(create_site))
.route("/:name", get(get_site).put(update_site).delete(delete_site))
.route("/", post(create_site::<Database>))
.route(
"/:name",
get(get_site::<Database>)
.put(update_site::<Database>)
.delete(delete_site::<Database>),
)
}