refactor(server): clean up some stuff
ci/woodpecker/push/lint Pipeline was successful Details
ci/woodpecker/push/clippy Pipeline failed Details
ci/woodpecker/push/build Pipeline was successful Details

repo-db
Jef Roosens 2023-08-02 22:41:23 +02:00
parent afe73d5314
commit bc19158747
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 41 additions and 40 deletions

View File

@ -1,5 +1,3 @@
use axum::response::{IntoResponse, Response};
use axum::Json;
use serde::{Deserialize, Serialize};
pub const DEFAULT_PAGE: u64 = 0;

View File

@ -21,7 +21,6 @@ impl fmt::Display for ServerError {
ServerError::Axum(err) => write!(fmt, "{}", err),
ServerError::Status(status) => write!(fmt, "{}", status),
ServerError::Db(err) => write!(fmt, "{}", err),
ServerError::Status(status) => write!(fmt, "{}", status),
}
}
}
@ -40,7 +39,6 @@ impl IntoResponse for ServerError {
StatusCode::NOT_FOUND.into_response()
}
ServerError::Db(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
ServerError::Status(status) => status.into_response(),
}
}
}

View File

@ -3,8 +3,8 @@ mod package;
pub use manager::RepoGroupManager;
use axum::body::Body;
use crate::db::entities::{package as db_package, repo as db_repo};
use axum::body::Body;
use axum::extract::{BodyStream, Path, State};
use axum::http::Request;
use axum::http::StatusCode;
@ -47,46 +47,51 @@ async fn post_package_archive(
let path_clone = path.clone();
let repo_clone = repo.clone();
let res = tokio::task::spawn_blocking(move || {
clone.write().unwrap().add_pkg_from_path(&repo_clone, &path_clone)
clone
.write()
.unwrap()
.add_pkg_from_path(&repo_clone, &path_clone)
})
.await?;
// Remove the downloaded file if the adding failed
if let Err(err) = res {
let _ = tokio::fs::remove_file(path).await;
match res {
// Insert the newly added package into the database
Ok(pkg) => {
// Query the repo for its ID, or create it if it does not already exist
let repo_entity = db_repo::Entity::find()
.filter(db_repo::Column::Name.eq(&repo))
.one(&global.db)
.await?;
return Err(err.into());
let repo_id = if let Some(repo_entity) = repo_entity {
repo_entity.id
} else {
let model = db_repo::ActiveModel {
name: sea_orm::Set(repo.clone()),
..Default::default()
};
db_repo::Entity::insert(model)
.exec(&global.db)
.await?
.last_insert_id
};
// Insert the package's data into the database
let mut model: db_package::ActiveModel = pkg.into();
model.repo_id = sea_orm::Set(repo_id);
model.insert(&global.db).await?;
Ok(())
}
// Remove the uploaded file and return the error
Err(err) => {
tokio::fs::remove_file(path).await?;
Err(err.into())
}
}
let pkg = res.unwrap();
// Query the repo for its ID, or create it if it does not already exist
let repo_entity = db_repo::Entity::find()
.filter(db_repo::Column::Name.eq(&repo))
.one(&global.db)
.await?;
let repo_id = if let Some(repo_entity) = repo_entity {
repo_entity.id
} else {
let model = db_repo::ActiveModel {
name: sea_orm::Set(repo.clone()),
..Default::default()
};
db_repo::Entity::insert(model)
.exec(&global.db)
.await?
.last_insert_id
};
// Insert the package's data into the database
let mut model: db_package::ActiveModel = pkg.into();
model.repo_id = sea_orm::Set(repo_id);
model.insert(&global.db).await?;
Ok(())
}
/// Serve the package archive files and database archives. If files are requested for an