diff options
author | Cara Salter <cara@devcara.com> | 2022-07-09 22:44:35 -0400 |
---|---|---|
committer | Cara Salter <cara@devcara.com> | 2022-07-09 22:44:35 -0400 |
commit | 90823a79ef80f6a589a414fb8a3006c8ff17a95d (patch) | |
tree | d7124a7ab9e43f9e79a32375da3cea2a9c36a7e8 /src/handlers/ships.rs | |
parent | c9ff6a9d85fb50e0760acad921709582c2e402cb (diff) | |
download | homeworld-90823a79ef80f6a589a414fb8a3006c8ff17a95d.tar.gz homeworld-90823a79ef80f6a589a414fb8a3006c8ff17a95d.zip |
fmt
Diffstat (limited to 'src/handlers/ships.rs')
-rw-r--r-- | src/handlers/ships.rs | 75 |
1 files changed, 53 insertions, 22 deletions
diff --git a/src/handlers/ships.rs b/src/handlers/ships.rs index 9d985ea..91944fc 100644 --- a/src/handlers/ships.rs +++ b/src/handlers/ships.rs @@ -1,56 +1,87 @@ use std::sync::Arc; -use axum::{Json, extract::Path, Extension}; -use hyper::StatusCode; -use solarlib::ship::{Ship, DbShip, Sha256}; -use sqlx::{query_as, query, Error as SqlxError}; +use axum::{extract::Path, Extension, Json}; use axum_auth::AuthBearer; +use hyper::StatusCode; +use solarlib::ship::{DbShip, Sha256, Ship}; +use sqlx::{query, query_as, Error as SqlxError}; use tracing::log::warn; -use crate::{errors::{JsonResult, StringResult, ServiceError}, State}; - +use crate::{ + errors::{JsonResult, ServiceError, StringResult}, + State, +}; pub async fn list(state: Extension<Arc<State>>) -> JsonResult<Json<Vec<Ship>>> { let mut conn = state.conn.acquire().await?; - let db_ships = query_as!(DbShip, "SELECT * FROM ships").fetch_all(&mut conn).await?; + let db_ships = query_as!(DbShip, "SELECT * FROM ships") + .fetch_all(&mut conn) + .await?; - let ships = db_ships.into_iter().map(|d| d.into()).collect::<Vec<Ship>>(); + let ships = db_ships + .into_iter() + .map(|d| d.into()) + .collect::<Vec<Ship>>(); Ok(Json(ships)) } -pub async fn new(Json(new_ship): Json<Ship>, state: Extension<Arc<State>>, AuthBearer(token): AuthBearer) -> StringResult { +pub async fn new( + Json(new_ship): Json<Ship>, + state: Extension<Arc<State>>, + AuthBearer(token): AuthBearer, +) -> StringResult { check_bearer(token)?; let mut conn = state.conn.acquire().await?; - query!("INSERT INTO ships (name, shasum, download_url, version) VALUES ($1, $2, $3, $4)", new_ship.name, new_ship.shasum.to_string(), new_ship.download_url, new_ship.version).execute(&mut conn).await?; + query!( + "INSERT INTO ships (name, shasum, download_url, version) VALUES ($1, $2, $3, $4)", + new_ship.name, + new_ship.shasum.to_string(), + new_ship.download_url, + new_ship.version + ) + .execute(&mut conn) + .await?; Ok("OK") } -pub async fn delete(Path(shasum): Path<Sha256>, state: Extension<Arc<State>>, AuthBearer(token): AuthBearer) -> StringResult { +pub async fn delete( + Path(shasum): Path<Sha256>, + state: Extension<Arc<State>>, + AuthBearer(token): AuthBearer, +) -> StringResult { check_bearer(token)?; let mut conn = state.conn.acquire().await?; - query!("DELETE FROM ships WHERE shasum=$1", shasum.to_string()).execute(&mut conn).await?; + query!("DELETE FROM ships WHERE shasum=$1", shasum.to_string()) + .execute(&mut conn) + .await?; Ok("OK") } -pub async fn get(Path(shasum): Path<Sha256>, state: Extension<Arc<State>>) -> JsonResult<Json<DbShip>> { +pub async fn get( + Path(shasum): Path<Sha256>, + state: Extension<Arc<State>>, +) -> JsonResult<Json<DbShip>> { let mut conn = state.conn.acquire().await?; - let db_ship = match query_as!(DbShip, "SELECT * FROM ships WHERE shasum=$1", shasum.to_string()).fetch_one(&mut conn).await { + let db_ship = match query_as!( + DbShip, + "SELECT * FROM ships WHERE shasum=$1", + shasum.to_string() + ) + .fetch_one(&mut conn) + .await + { Ok(d) => d, - Err(e) => { - match e { - SqlxError::RowNotFound => { - return Err(ServiceError::NotFound) - }, - _ => { - return Err(e.into()); - } + Err(e) => match e { + SqlxError::RowNotFound => return Err(ServiceError::NotFound), + _ => { + return Err(e.into()); } }, }; |