This commit is contained in:
parent
33b743f213
commit
18c9be898a
2 changed files with 30 additions and 19 deletions
|
@ -1,4 +1,4 @@
|
||||||
use chrono::{serde::*, DateTime, Utc};
|
use chrono::{serde::*, DateTime, NaiveDateTime, Utc};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use sqlx::{types::Json, FromRow};
|
use sqlx::{types::Json, FromRow};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
@ -21,12 +21,9 @@ pub struct Site {
|
||||||
pub description: Option<String>,
|
pub description: Option<String>,
|
||||||
|
|
||||||
/// Times
|
/// Times
|
||||||
#[serde(with = "ts_seconds")]
|
pub created_at: NaiveDateTime,
|
||||||
pub created_at: DateTime<Utc>,
|
pub modified_at: Option<NaiveDateTime>,
|
||||||
#[serde(with = "ts_seconds_option")]
|
pub deleted_at: Option<NaiveDateTime>,
|
||||||
pub modified_at: Option<DateTime<Utc>>,
|
|
||||||
#[serde(with = "ts_seconds_option")]
|
|
||||||
pub deleted_at: Option<DateTime<Utc>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, FromRow)]
|
#[derive(Deserialize, Serialize, FromRow)]
|
||||||
|
@ -56,12 +53,9 @@ pub struct Page {
|
||||||
pub blocks: Json<Vec<PageBlock>>,
|
pub blocks: Json<Vec<PageBlock>>,
|
||||||
|
|
||||||
/// Times
|
/// Times
|
||||||
#[serde(with = "ts_seconds")]
|
pub created_at: NaiveDateTime,
|
||||||
pub created_at: DateTime<Utc>,
|
pub modified_at: Option<NaiveDateTime>,
|
||||||
#[serde(with = "ts_seconds_option")]
|
pub deleted_at: Option<NaiveDateTime>,
|
||||||
pub modified_at: Option<DateTime<Utc>>,
|
|
||||||
#[serde(with = "ts_seconds_option")]
|
|
||||||
pub deleted_at: Option<DateTime<Utc>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize)]
|
#[derive(Deserialize, Serialize)]
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::{Path, State},
|
extract::{Path, State},
|
||||||
|
http::StatusCode,
|
||||||
response::IntoResponse,
|
response::IntoResponse,
|
||||||
routing::{get, post},
|
routing::{get, post},
|
||||||
Json, Router,
|
Json, Router,
|
||||||
|
@ -10,7 +11,6 @@ use std::sync::Arc;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
auth::http::RequireUser,
|
auth::http::RequireUser,
|
||||||
content::Site,
|
|
||||||
http::{
|
http::{
|
||||||
error::{ApiError, ERR_NOT_FOUND},
|
error::{ApiError, ERR_NOT_FOUND},
|
||||||
extract::JsonBody,
|
extract::JsonBody,
|
||||||
|
@ -22,15 +22,22 @@ async fn get_site(
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
Path(name): Path<String>,
|
Path(name): Path<String>,
|
||||||
) -> Result<impl IntoResponse, ApiError<'static>> {
|
) -> Result<impl IntoResponse, ApiError<'static>> {
|
||||||
let site_query = sqlx::query_as("SELECT * FROM sites WHERE name = $1").bind(name);
|
let site = sqlx::query!(
|
||||||
let site: Site = site_query
|
"SELECT sites.id, sites.name, users.name as owner, title, description, sites.created_at FROM sites JOIN users ON sites.owner = users.id WHERE sites.name = $1 ",
|
||||||
.fetch_one(&state.database)
|
name
|
||||||
|
).fetch_one(&state.database)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| match e {
|
.map_err(|e| match e {
|
||||||
sqlx::Error::RowNotFound => ERR_NOT_FOUND,
|
sqlx::Error::RowNotFound => ERR_NOT_FOUND,
|
||||||
_ => e.into(),
|
_ => e.into(),
|
||||||
})?;
|
})?;
|
||||||
Ok(Json(site))
|
Ok(Json(json!({
|
||||||
|
"name": site.name,
|
||||||
|
"owner": site.owner,
|
||||||
|
"title": site.title,
|
||||||
|
"description": site.description,
|
||||||
|
"created_at": site.created_at,
|
||||||
|
})))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
|
@ -51,7 +58,17 @@ async fn create_site(
|
||||||
payload.title,
|
payload.title,
|
||||||
)
|
)
|
||||||
.execute(&state.database)
|
.execute(&state.database)
|
||||||
.await?;
|
.await
|
||||||
|
.map_err(|err| match err {
|
||||||
|
sqlx::Error::Database(dberr) if dberr.constraint() == Some("sites_name_key") => {
|
||||||
|
ApiError::ClientError {
|
||||||
|
status: StatusCode::CONFLICT,
|
||||||
|
code: "name-not-available",
|
||||||
|
message: "the chosen name is not available",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => err.into(),
|
||||||
|
})?;
|
||||||
Ok(Json(json!({"ok": true, "url": "test"})))
|
Ok(Json(json!({"ok": true, "url": "test"})))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue