init latest_aur_version and make it non option

This commit is contained in:
lukas-heiligenbrunner 2024-01-02 19:56:39 +01:00
parent 80e2299dc8
commit 5a356e65a7
4 changed files with 9 additions and 8 deletions

View File

@ -28,6 +28,10 @@ pub async fn package_add(
) -> Result<(), BadRequest<String>> { ) -> Result<(), BadRequest<String>> {
let db = db as &DatabaseConnection; let db = db as &DatabaseConnection;
let pkg = get_info_by_name(input.name.clone().as_str())
.await
.map_err(|_| BadRequest(Some("couldn't download package metadata".to_string())))?;
let mut pkg_model = match Packages::find() let mut pkg_model = match Packages::find()
.filter(packages::Column::Name.eq(input.name.clone())) .filter(packages::Column::Name.eq(input.name.clone()))
.one(db) .one(db)
@ -38,6 +42,7 @@ pub async fn package_add(
let new_package = packages::ActiveModel { let new_package = packages::ActiveModel {
name: Set(input.name.clone()), name: Set(input.name.clone()),
status: Set(0), status: Set(0),
latest_aur_version: Set(pkg.version.clone()),
..Default::default() ..Default::default()
}; };
@ -46,10 +51,6 @@ pub async fn package_add(
Some(p) => p.into(), Some(p) => p.into(),
}; };
let pkg = get_info_by_name(input.name.clone().as_str())
.await
.map_err(|_| BadRequest(Some("couldn't download package metadata".to_string())))?;
let version_model = match Versions::find() let version_model = match Versions::find()
.filter(versions::Column::Version.eq(pkg.version.clone())) .filter(versions::Column::Version.eq(pkg.version.clone()))
.one(db) .one(db)

View File

@ -54,7 +54,7 @@ pub struct ListPackageModel {
#[get("/packages/list")] #[get("/packages/list")]
pub async fn package_list( pub async fn package_list(
db: &State<DatabaseConnection>, db: &State<DatabaseConnection>,
) -> Result<Json<Vec<ListPackageModel>>, String> { ) -> Result<Json<Vec<ListPackageModel>>, NotFound<String>> {
let db = db as &DatabaseConnection; let db = db as &DatabaseConnection;
let all: Vec<ListPackageModel> = Packages::find() let all: Vec<ListPackageModel> = Packages::find()
@ -70,7 +70,7 @@ pub async fn package_list(
.into_model::<ListPackageModel>() .into_model::<ListPackageModel>()
.all(db) .all(db)
.await .await
.unwrap(); .map_err(|e| NotFound(e.to_string()))?;
Ok(Json(all)) Ok(Json(all))
} }

View File

@ -14,7 +14,7 @@ pub struct Model {
pub status: i32, pub status: i32,
pub out_of_date: i32, pub out_of_date: i32,
pub latest_version_id: Option<i32>, pub latest_version_id: Option<i32>,
pub latest_aur_version: Option<String>, pub latest_aur_version: String,
} }
impl ActiveModelBehavior for ActiveModel {} impl ActiveModelBehavior for ActiveModel {}

View File

@ -46,7 +46,7 @@ async fn aur_check_versions(db: DatabaseConnection) -> anyhow::Result<()> {
Some(result) => { Some(result) => {
let mut package: packages::ActiveModel = package.into(); let mut package: packages::ActiveModel = package.into();
package.latest_aur_version = Set(Some(result.version.clone())); package.latest_aur_version = Set(result.version.clone());
let _ = package.update(&db).await; let _ = package.update(&db).await;
} }
} }