54 lines
1.9 KiB
D
54 lines
1.9 KiB
D
module profile.api;
|
|
|
|
import std.json;
|
|
|
|
import handy_httpd;
|
|
import asdf;
|
|
|
|
import profile.model;
|
|
import profile.service;
|
|
import profile.data;
|
|
import profile.data_impl_sqlite;
|
|
import auth.model;
|
|
import auth.service;
|
|
|
|
void handleCreateNewProfile(ref HttpRequestContext ctx) {
|
|
JSONValue obj = ctx.request.readBodyAsJson();
|
|
string name = obj.object["name"].str;
|
|
if (!validateProfileName(name)) {
|
|
ctx.response.status = HttpStatus.BAD_REQUEST;
|
|
ctx.response.writeBodyString("Invalid profile name.");
|
|
return;
|
|
}
|
|
AuthContext auth = getAuthContext(ctx);
|
|
ProfileRepository profileRepo = new FileSystemProfileRepository(auth.user.username);
|
|
profileRepo.createProfile(name);
|
|
}
|
|
|
|
void handleGetProfiles(ref HttpRequestContext ctx) {
|
|
AuthContext auth = getAuthContext(ctx);
|
|
ProfileRepository profileRepo = new FileSystemProfileRepository(auth.user.username);
|
|
Profile[] profiles = profileRepo.findAll();
|
|
ctx.response.writeBodyString(serializeToJson(profiles), "application/json");
|
|
}
|
|
|
|
void handleDeleteProfile(ref HttpRequestContext ctx) {
|
|
string name = ctx.request.getPathParamAs!string("name");
|
|
if (!validateProfileName(name)) {
|
|
ctx.response.status = HttpStatus.BAD_REQUEST;
|
|
ctx.response.writeBodyString("Invalid profile name.");
|
|
return;
|
|
}
|
|
AuthContext auth = getAuthContext(ctx);
|
|
ProfileRepository profileRepo = new FileSystemProfileRepository(auth.user.username);
|
|
profileRepo.deleteByName(name);
|
|
}
|
|
|
|
void handleGetProperties(ref HttpRequestContext ctx) {
|
|
ProfileContext profileCtx = getProfileContextOrThrow(ctx);
|
|
ProfileRepository profileRepo = new FileSystemProfileRepository(profileCtx.user.username);
|
|
ProfileDataSource ds = profileRepo.getDataSource(profileCtx.profile);
|
|
auto propsRepo = ds.getPropertiesRepository();
|
|
ProfileProperty[] props = propsRepo.findAll();
|
|
ctx.response.writeBodyString(serializeToJson(props), "application/json");
|
|
} |