summaryrefslogblamecommitdiff
path: root/src/commands/modification.rs
blob: 595b6778642d79598139a5ef183c7f3ecfff1810 (plain) (tree)
1
2
3

                                
                                                                                                                                                                                                                           




















                                                                                                                    
                                                     










                                                                                                                                  
    




                                                                                                       




                                                             
                                       
                                                                         




                                                                                             
                                                                                                                                               

              
                                                                                                                                                  

      
                                            




                                                                                             
                                                                                                                        

              


                                                                                                                    
      






                                                                                                           
                                                                      

                                                                                          

                                                                          


                         
use std::io::{Error, ErrorKind};

use crate::{modrinth::{project, versions, extract_current_version}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids}, input::Input, get_current_list};

pub async fn modification(config: Cfg, args: Option<Vec<String>>) -> Result<(), Box<dyn std::error::Error>> {
    
    if args.is_none() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))) }

    let arguments = Input::from(args.unwrap().join(" "))?;

    if arguments.args.is_none() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); };

    match arguments.command.as_str() {
        "add" => {
            add(config, arguments.args.unwrap()).await
        },
        "remove" => {
            remove(config, arguments.args.unwrap())
        },
        _ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_SUBCOMMAND")))
    }
}

async fn add(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
    //TODO! DO NOT PANIC IF MOD IS ALREADY IN MODS DB
    if args.is_empty() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); };
    
    let current_list = get_current_list(config.clone())?;

    let project = project(String::from(&config.apis.modrinth), &args[0]).await;

    dbg!(&project);

    if project.versions.is_empty() { panic!("This should never happen"); };

    let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), current_list.clone()).await;
    
    let current_id = extract_current_version(available_versions.clone())?;

    let current_version = available_versions.clone().into_iter().find(|v| v.id == current_id).unwrap();

    let file = current_version.files.into_iter().find(|f| f.primary).unwrap().url;
    
    let mut available_versions_vec: Vec<String> = Vec::new();
    for ver in available_versions {
        available_versions_vec.push(ver.id);
    }
    //add to current list and mod table
    match userlist_get_all_ids(config.clone(), current_list.clone().id) {
        Ok(mods) => {
            dbg!(&mods);
            if mods.contains(&project.id) {
                return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_ON_LIST"))); } 
            else {
                userlist_insert(config.clone(), current_list.id, String::from(&project.id), current_version.id, available_versions_vec, file)?;
            } 
        },
        Err(..) => userlist_insert(config.clone(), current_list.id, String::from(&project.id), current_version.id, available_versions_vec, file)?,
    };
    
    match mods_get_all_ids(config.clone()) {
        Ok(mods) => {
            dbg!(&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)?;
        },
    };
    Ok(())
}

fn remove(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
    if args.is_empty() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); };

    let current_list = get_current_list(config.clone())?;
    let mod_id = mods_get_id(config.clone(), String::from(&args[0]))?;
    
    //TODO implement remove from modlist if not in any other lists && config clean is true
    match userlist_remove(config, current_list.id, mod_id) {
        Err(..) => { Err(Box::new(Error::new(ErrorKind::Other, "TBD"))) },
        Ok(()) => Ok(()),
    }
}