{"size": 1039, "ext": "js", "max_stars_repo_path": "CaloriesCalculatorApi/Repositories/Category.js", "max_stars_repo_name": "MackoLysy/CaloriesCalculator", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "CaloriesCalculatorApi/Repositories/Category.js", "max_issues_repo_name": "MackoLysy/CaloriesCalculator", "max_issues_repo_issues_event_min_datetime": "2020-09-07T07:14:21.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-26T17:37:42.000Z", "max_forks_repo_path": "CaloriesCalculatorApi/Repositories/Category.js", "max_forks_repo_name": "MackoLysy/CaloriesCalculator", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 9, "max_forks_count": null, "avg_line_length": 28.0810810811, "max_line_length": 85, "alphanum_fraction": 0.5428296439}
const IRepository = require('./IRepository');


class Category extends IRepository {
    constructor() {
        super();
        this.tableName = "category";
    }

    async createTable() {
        var sql = "CREATE TABLE IF NOT EXISTS " + this.tableName + " " +
            "(id INTEGER PRIMARY KEY AUTOINCREMENT," +
            "name TEXT," +
            "parentId INTEGER" +
            ")";
        await this._run(sql);
    }

    findCategoryByName(name) {
        var sql = 'SELECT * FROM ' + this.tableName + ' WHERE name = "' + name + '"';
        return this._get(sql);
    }

    async addCategory(category) {
        console.log(category);
        var sql = "INSERT INTO " + this.tableName + "(name, parentId) VALUES (?, ?)";
        if (!category.parent) {
            return this._insert(sql, [category.name, 0]);
        }
        else {
            var parent = await this.findCategoryByName(category.parent);
            return this._insert(sql, [category.name, parent.id]);
        }
    }
}

module.exports = Category;