finnow/finnow-api/source/util/repository.d

32 lines
667 B
D
Raw Normal View History

module util.repository;
import handy_httpd.components.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);
}
}