From ff23a11e632812b685f594324e6004c6da81cd4d Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Sun, 5 Feb 2023 09:23:29 +0100 Subject: Fixed update shit not correctly updating --- src/commands/modification.rs | 73 ++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 36 deletions(-) (limited to 'src/commands/modification.rs') diff --git a/src/commands/modification.rs b/src/commands/modification.rs index c82d6b5..12a635f 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -1,4 +1,9 @@ -use crate::{modrinth::{project, versions, extract_current_version, Version, projects}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids, userlist_get_current_version, lists_get_all_ids, mods_remove}, input::{Input, ModOptions}, files::{delete_version, download_versions}, List, error::{MLE, ErrorType, MLError}}; +use crate::{modrinth::{project, versions, extract_current_version, Version, projects}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, userlist_get_all_ids, userlist_get_current_version, lists_get_all_ids, mods_remove}, input::{Input, ModOptions}, files::{delete_version, download_versions}, List, error::{MLE, ErrorType, MLError}}; + +//TODO DO IT +pub struct ModVer { + +} pub async fn modification(config: Cfg, input: Input) -> MLE<()> { match input.clone().mod_options.ok_or("").unwrap() { @@ -13,22 +18,26 @@ pub async fn modification(config: Cfg, input: Input) -> MLE<()> { async fn add(config: Cfg, input: Input) -> MLE<()> { - mod_add(config, vec![String::from(input.mod_id.unwrap())], input.list.unwrap(), input.direct_download).await?; + mods_add(config, vec![input.mod_id.unwrap()], input.list.unwrap(), input.direct_download, input.set_version).await?; Ok(()) } - -pub async fn mod_add(config: Cfg, mod_id: Vec, list: List, direct_download: bool) -> MLE<()> { +//TODO impl specific version +pub async fn mods_add(config: Cfg, mod_id: Vec, list: List, direct_download: bool, set_version: bool) -> MLE<()> { - //Fix printing (its horrible) - println!("Adding mod(s) {:?}", mod_id); + println!("Add mods to {}", list.id); + println!(" └Add mods:"); let projects = if mod_id.len() == 1 { vec![project(String::from(&config.apis.modrinth), &mod_id[0]).await] } else { projects(String::from(&config.apis.modrinth), mod_id).await }; + + let mut downloadstack: Vec = Vec::new(); for project in projects { + println!("\t└{}", project.title); + println!("\t └Get versions"); let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; let mut available_versions_vec: Vec = Vec::new(); @@ -37,64 +46,56 @@ pub async fn mod_add(config: Cfg, mod_id: Vec, list: List, direct_downlo let file: String; if !available_versions.is_empty() { let current_id = extract_current_version(available_versions.clone())?; + println!("\t └Current version: {}", current_id); current_version = Some(available_versions.clone().into_iter().find(|v| v.id == current_id).unwrap()); current_version_id = current_version.clone().unwrap().id; file = current_version.clone().ok_or("").unwrap().files.into_iter().find(|f| f.primary).unwrap().url; - for ver in available_versions { available_versions_vec.push(ver.id); }; } else { - println!("There's currently no mod version for your specified target"); + println!("\t └There's currently no mod version for your specified target"); current_version = None; current_version_id = String::from("NONE"); file = String::from("NONE"); available_versions_vec.push(String::from("NONE")); } - //add to current list and mod table - match userlist_get_all_ids(config.clone(), list.clone().id) { - Ok(mods) => { - if mods.contains(&project.id) { - return Err(MLError::new(ErrorType::ModError, "MOD_ALREADY_ON_LIST")); } - else { - userlist_insert(config.clone(), String::from(&list.id), String::from(&project.id), String::from(¤t_version_id), available_versions_vec, file)?; - } + match userlist_insert(config.clone(), &list.id, &project.id, ¤t_version_id, available_versions_vec, &file, set_version) { + Err(e) => { + let expected_err = format!("SQL: UNIQUE constraint failed: {}.mod_id", list.id); + if e.to_string() == expected_err { Err(MLError::new(ErrorType::ModError, "MOD_ALREADY_ON_SELECTED_LIST")) } else { Err(e) } }, - Err(..) => userlist_insert(config.clone(), String::from(&list.id), String::from(&project.id), String::from(¤t_version_id), available_versions_vec, file)?, - }; + Ok(..) => { Ok(..) }, + }?; - match mods_get_all_ids(config.clone()) { - Ok(mods) => { - if mods.contains(&project.id) { - //return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_IN_DATABASE"))) - } else { - mods_insert(config.clone(), String::from(&project.id), String::from(&project.title), project.versions)?; - } - }, - Err(..) => { - mods_insert(config.clone(), String::from(&project.id), String::from(&project.title), project.versions)?; + match mods_insert(config.clone(), &project.id, &project.slug, &project.title) { + Err(e) => { + if e.to_string() == "SQL: UNIQUE constraint failed: mods.id" { Ok(..) } else { Err(e) } }, - }; + Ok(..) => Ok(..), + }?; - if direct_download && current_version.is_some() { download_versions(list.clone(), config.clone(), vec![current_version.unwrap()]).await?; }; - - } + downloadstack.push(current_version.unwrap()); + }; + + //Download all the added mods + if direct_download { + download_versions(list.clone(), config.clone(), downloadstack).await?; + }; Ok(()) } fn remove(config: Cfg, input: Input) -> MLE<()> { - - //TODO inplement deletion by slug or title - let mod_id = mods_get_id(config.clone(), input.clone().mod_id.unwrap())?; + + let mod_id = mods_get_id(&config.data, input.mod_id.as_ref().unwrap())?; let version = userlist_get_current_version(config.clone(), input.clone().list.unwrap().id, String::from(&mod_id))?; - //TODO implement remove from modlist if not in any other lists && config clean is true userlist_remove(config.clone(), input.clone().list.unwrap().id, String::from(&mod_id))?; delete_version(input.list.unwrap(), version)?; -- cgit v1.2.3