55 lines
1.8 KiB
D
55 lines
1.8 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;
|
|
import util.json;
|
|
|
|
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();
|
|
writeJsonBody(ctx, profiles);
|
|
}
|
|
|
|
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();
|
|
writeJsonBody(ctx, props);
|
|
} |