simpify db query code

This commit is contained in:
neri 2021-09-10 17:44:37 +02:00
parent 4396bb1b56
commit 63fff3adf3
1 changed files with 9 additions and 20 deletions

View File

@ -7,12 +7,8 @@ use actix_web::{
web, Error, HttpRequest, HttpResponse,
};
use async_std::{fs, path::Path};
use futures::TryStreamExt;
use mime::Mime;
use sqlx::{
postgres::{PgPool, PgRow},
Row,
};
use sqlx::postgres::PgPool;
use url::Url;
use crate::deleter;
@ -54,24 +50,17 @@ async fn load_file_info(
id: &str,
db: &web::Data<sqlx::Pool<sqlx::Postgres>>,
) -> Result<(String, String, String, bool), Error> {
let mut rows = sqlx::query(
sqlx::query_as(
"SELECT file_id, file_name, kind, delete_on_download from files WHERE file_id = $1",
)
.bind(id)
.fetch(db.as_ref());
let row: PgRow = rows
.try_next()
.await
.map_err(|db_err| {
log::error!("could not run select statement {:?}", db_err);
error::ErrorInternalServerError("could not run select statement")
})?
.ok_or_else(|| error::ErrorNotFound("file does not exist or has expired"))?;
let file_id: String = row.get("file_id");
let file_name: String = row.get("file_name");
let file_kind: String = row.get("kind");
let delete_on_download: bool = row.get("delete_on_download");
Ok((file_id, file_name, file_kind, delete_on_download))
.fetch_optional(db.as_ref())
.await
.map_err(|db_err| {
log::error!("could not run select statement {:?}", db_err);
error::ErrorInternalServerError("could not run select statement")
})?
.ok_or_else(|| error::ErrorNotFound("file does not exist or has expired"))
}
fn get_content_type(path: &Path) -> Mime {