module profile.api; import std.json; import asdf; import handy_http_primitives; import handy_http_data.json; import handy_http_handlers.path_handler : getPathParamAs; import profile.model; import profile.service; import profile.data; import profile.data_impl_sqlite; import auth.model; import auth.service; struct NewProfilePayload { string name; } void handleCreateNewProfile(ref ServerHttpRequest request, ref ServerHttpResponse response) { auto payload = readJsonBodyAs!NewProfilePayload(request); string name = payload.name; if (!validateProfileName(name)) { response.status = HttpStatus.BAD_REQUEST; response.writeBodyString("Invalid profile name."); return; } AuthContext auth = getAuthContext(request); ProfileRepository profileRepo = new FileSystemProfileRepository(auth.user.username); Profile p = profileRepo.createProfile(name); writeJsonBody(response, p); } void handleGetProfiles(ref ServerHttpRequest request, ref ServerHttpResponse response) { AuthContext auth = getAuthContext(request); ProfileRepository profileRepo = new FileSystemProfileRepository(auth.user.username); Profile[] profiles = profileRepo.findAll(); writeJsonBody(response, profiles); } void handleDeleteProfile(ref ServerHttpRequest request, ref ServerHttpResponse response) { string name = request.getPathParamAs!string("profile"); if (!validateProfileName(name)) { response.status = HttpStatus.BAD_REQUEST; response.writeBodyString("Invalid profile name."); return; } AuthContext auth = getAuthContext(request); ProfileRepository profileRepo = new FileSystemProfileRepository(auth.user.username); profileRepo.deleteByName(name); } void handleGetProperties(ref ServerHttpRequest request, ref ServerHttpResponse response) { ProfileContext profileCtx = getProfileContextOrThrow(request); ProfileRepository profileRepo = new FileSystemProfileRepository(profileCtx.user.username); ProfileDataSource ds = profileRepo.getDataSource(profileCtx.profile); auto propsRepo = ds.getPropertiesRepository(); ProfileProperty[] props = propsRepo.findAll(); writeJsonBody(response, props); }