feat: Owner PATCH route
This commit is contained in:
parent
8a7cfac885
commit
1c4abab453
2 changed files with 60 additions and 1 deletions
|
|
@ -75,6 +75,7 @@ async fn main() {
|
||||||
.routes(routes!(routes::book_instance::create_book_instance))
|
.routes(routes!(routes::book_instance::create_book_instance))
|
||||||
.routes(routes!(routes::owner::get_owner_by_id))
|
.routes(routes!(routes::owner::get_owner_by_id))
|
||||||
.routes(routes!(routes::owner::create_owner))
|
.routes(routes!(routes::owner::create_owner))
|
||||||
|
.routes(routes!(routes::owner::update_owner))
|
||||||
.routes(routes!(routes::owner::get_owners))
|
.routes(routes!(routes::owner::get_owners))
|
||||||
.routes(routes!(routes::websocket::ws_handler))
|
.routes(routes!(routes::websocket::ws_handler))
|
||||||
.route("/", get(index))
|
.route("/", get(index))
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use std::sync::Arc;
|
||||||
|
|
||||||
use axum::{extract::{Path, State}, Json};
|
use axum::{extract::{Path, State}, Json};
|
||||||
use reqwest::{StatusCode};
|
use reqwest::{StatusCode};
|
||||||
use sea_orm::{ActiveModelTrait, ActiveValue::{NotSet, Set}, EntityTrait, TryIntoModel};
|
use sea_orm::{ActiveModelTrait, ActiveValue::{NotSet, Set, Unchanged}, EntityTrait, TryIntoModel};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use utoipa::IntoParams;
|
use utoipa::IntoParams;
|
||||||
|
|
||||||
|
|
@ -83,6 +83,64 @@ pub async fn create_owner(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize, utoipa::ToSchema)]
|
||||||
|
pub struct OwnerUpdateParams {
|
||||||
|
first_name: Option<String>,
|
||||||
|
last_name: Option<String>,
|
||||||
|
contact: Option<String>
|
||||||
|
}
|
||||||
|
|
||||||
|
#[axum::debug_handler]
|
||||||
|
#[utoipa::path(
|
||||||
|
patch,
|
||||||
|
path = "/owner/{id}",
|
||||||
|
params(OwnerByIdParams),
|
||||||
|
request_body = OwnerUpdateParams,
|
||||||
|
responses(
|
||||||
|
(status = OK, body = owner::Model, description = "Successfully updated owner"),
|
||||||
|
(status = NOT_FOUND, description = "No owner with this id exists in the database")
|
||||||
|
),
|
||||||
|
summary = "Update an owner",
|
||||||
|
description = "Update an owner",
|
||||||
|
tag = "owner-api",
|
||||||
|
)]
|
||||||
|
pub async fn update_owner(
|
||||||
|
State(state): State<Arc<AppState>>,
|
||||||
|
Path(id): Path<u32>,
|
||||||
|
Json(instance_payload): Json<OwnerUpdateParams>,
|
||||||
|
) -> (StatusCode, Json<Option<owner::Model>>) {
|
||||||
|
|
||||||
|
if let Ok(Some(owner)) = Owner::find_by_id(id).one(state.db_conn.as_ref()).await {
|
||||||
|
let mut owner: owner::ActiveModel = owner.into();
|
||||||
|
owner.first_name = match instance_payload.first_name {
|
||||||
|
None => owner.first_name,
|
||||||
|
Some(v) => Set(v)
|
||||||
|
};
|
||||||
|
owner.last_name = match instance_payload.last_name {
|
||||||
|
None => owner.last_name,
|
||||||
|
Some(v) => Set(v)
|
||||||
|
};
|
||||||
|
owner.contact = match instance_payload.contact {
|
||||||
|
None => owner.contact,
|
||||||
|
Some(v) => Set(v)
|
||||||
|
};
|
||||||
|
|
||||||
|
match owner.update(state.db_conn.as_ref()).await {
|
||||||
|
Err(e) => {
|
||||||
|
log::error!(target: "api", "Error while updating owner from api: {:#?}", e);
|
||||||
|
(StatusCode::INTERNAL_SERVER_ERROR, Json(None))
|
||||||
|
},
|
||||||
|
Ok(res) => {
|
||||||
|
let model = res.try_into_model().expect("All fields should be set once the owner is saved");
|
||||||
|
let _ = state.event_bus.send(Event::WebsocketBroadcast(WebsocketMessage::NewOwner(Arc::new(model.clone()))));
|
||||||
|
(StatusCode::OK, Json(Some(model)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
(StatusCode::NOT_FOUND, Json(None))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
#[utoipa::path(
|
#[utoipa::path(
|
||||||
get,
|
get,
|
||||||
|
|
|
||||||
Reference in a new issue