summaryrefslogtreecommitdiff
path: root/src/commands/modification.rs
blob: c194202a3fd2c6c3d504b542d503623032bfe24a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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}};

pub async fn modification(config: Cfg, input: Input) -> MLE<()> {
    match input.clone().mod_options.ok_or("").unwrap() {
        ModOptions::Add => {
            add(config, input).await
        },
        ModOptions::Remove => {
            remove(config, input)
        },
    }
}

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?;

    Ok(())
}

pub async fn mod_add(config: Cfg, mod_id: Vec<String>, list: List, disable_download: bool) -> MLE<()> {
    
    //Fix printing (its horrible)
    println!("Adding mod(s) {:?}", mod_id);
    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
    };
    
    for project in projects {
        let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await;

        let mut available_versions_vec: Vec<String> = Vec::new();
        let current_version: Option<Version>;
        let current_version_id: String;
        let file: String;
        if !available_versions.is_empty() {
            let current_id = extract_current_version(available_versions.clone())?;

            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");
            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(&current_version_id), available_versions_vec, file)?;
                } 
            },
            Err(..) => userlist_insert(config.clone(), String::from(&list.id), String::from(&project.id), String::from(&current_version_id), available_versions_vec, file)?,
        };
        
        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)?;
            },
        };

        if !disable_download && current_version.is_some() { download_versions(list.clone(), config.clone(), vec![current_version.unwrap()]).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 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)?;
    
    let list_ids = lists_get_all_ids(config.clone())?;
    
    let mut mod_used = false;
    for id in list_ids {
        let mods = userlist_get_all_ids(config.clone(), id)?;
        if mods.contains(&mod_id) { mod_used = true; break; };
    };

    if !mod_used { mods_remove(config, mod_id)?; };

    Ok(())
}