package core.operation;

import core.user.User;
import dao.DBManagement;
import error.GExcptSQL;

import java.io.File;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UploadFile extends Operation {
    @Override
    public Map<String, Object> execute(User subject) throws Exception {
        String table = (String) this.getOptions().get("file_type");
        Map<String,String> limits = (Map<String, String>) this.getOptions().get("limits");
        String field = core.operation.utils.Utils.getFileField(table);
        String filePath = (String) this.getOptions().get("file_url");
        Map<String,String> vMap = new HashMap<>();
        vMap.put(field,filePath);
        List<String> ls = new ArrayList<>();
        ls.add(field);
        List<String[]> resultLs = DBManagement.select(ls,table,limits,0,1);
        String lastFilePath = resultLs.get(0)[0];
        new File(lastFilePath).delete();
        DBManagement.update(table,vMap,limits);
        return this.getOptions();
    }
}
