Added base entity.

This commit is contained in:
Andrew Lalis 2024-07-24 16:32:08 -04:00
parent 8ce75ddff6
commit a24ab226c2
2 changed files with 52 additions and 4 deletions

View File

@ -1,6 +1,17 @@
import std.stdio;
import slf4d;
import handy_httpd;
import handy_httpd.handlers.path_handler;
void main()
{
writeln("Edit source/app.d to start your project.");
import model.base;
void main() {
ServerConfig cfg;
cfg.workerPoolSize = 5;
cfg.port = 8080;
PathHandler pathHandler = new PathHandler();
pathHandler.addMapping(Method.GET, "/status", (ref ctx) {
ctx.response.writeBodyString("online");
});
HttpServer server = new HttpServer(pathHandler, cfg);
server.start();
}

View File

@ -0,0 +1,37 @@
module model.base;
/**
* The base class for all persistent entities with a unique integer id.
* It offers some basic utilities like equality and comparison by default.
*/
abstract class IdEntity {
const ulong id;
this(ulong id) {
this.id = id;
}
override bool opEquals(Object other) const {
if (IdEntity e = cast(IdEntity) other) {
return e.id == id;
}
return false;
}
override size_t toHash() const {
import std.conv : to;
return id.to!size_t;
}
override string toString() const {
import std.format : format;
return format!"IdEntity(id = %d)"(id);
}
override int opCmp(Object other) const {
if (IdEntity e = cast(IdEntity) other) {
return this.id < e.id;
}
return 1;
}
}