feat(server): POST request to create distros

distro
Jef Roosens 2023-08-17 10:56:08 +02:00
parent b7be311485
commit 50ebffb459
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
4 changed files with 20 additions and 4 deletions

View File

@ -7,6 +7,7 @@ use serde::{Deserialize, Serialize};
#[sea_orm(table_name = "distro")] #[sea_orm(table_name = "distro")]
pub struct Model { pub struct Model {
#[sea_orm(primary_key)] #[sea_orm(primary_key)]
#[serde(skip_deserializing)]
pub id: i32, pub id: i32,
#[sea_orm(unique)] #[sea_orm(unique)]
pub slug: String, pub slug: String,

View File

@ -43,4 +43,10 @@ impl DistroQuery {
Distro::insert(model).exec(&self.conn).await Distro::insert(model).exec(&self.conn).await
} }
pub async fn insert_model(&self, model: distro::Model) -> Result<distro::Model> {
let mut model: distro::ActiveModel = model.into();
model.id = NotSet;
model.insert(&self.conn).await
}
} }

View File

@ -35,10 +35,12 @@ impl IntoResponse for ServerError {
ServerError::IO(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(), ServerError::IO(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
ServerError::Axum(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(), ServerError::Axum(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
ServerError::Status(status) => status.into_response(), ServerError::Status(status) => status.into_response(),
ServerError::Db(sea_orm::DbErr::RecordNotFound(_)) => { ServerError::Db(err) => match err {
StatusCode::NOT_FOUND.into_response() sea_orm::DbErr::RecordNotFound(_) => StatusCode::NOT_FOUND,
sea_orm::DbErr::Query(_) => StatusCode::BAD_REQUEST,
_ => StatusCode::INTERNAL_SERVER_ERROR,
} }
ServerError::Db(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(), .into_response(),
} }
} }
} }

View File

@ -9,7 +9,7 @@ use crate::db;
pub fn router() -> Router<crate::Global> { pub fn router() -> Router<crate::Global> {
Router::new() Router::new()
.route("/", get(get_distros)) .route("/", get(get_distros).post(post_distro))
.route("/:id", get(get_single_distro)) .route("/:id", get(get_single_distro))
} }
@ -41,3 +41,10 @@ async fn get_single_distro(
Ok(Json(repo)) Ok(Json(repo))
} }
async fn post_distro(
State(global): State<crate::Global>,
Json(model): Json<db::distro::Model>,
) -> crate::Result<Json<db::distro::Model>> {
Ok(Json(global.db.distro.insert_model(model).await?))
}