module util.repository;

import handy_http_primitives : Optional;
import d2sqlite3;
import util.sqlite;

class CrudRepository(T, string table)
    if (__traits(hasMember, T, "id")) {

    protected Database db;
    this(Database db) {
        this.db = db;
    }

    abstract T parseResult(Row r);

    Optional!T findById(ulong id) {
        return util.sqlite.findById(db, table, &parseResult, id);
    }

    bool existsById(ulong id) {
        return util.sqlite.exists(
            db,
            "SELECT id FROM " ~ table ~ " WHERE id = ?",
            id
        );
    }

    void deleteById(ulong id) {
        util.sqlite.deleteById(db, table, id);
    }
}