diff options
author | Jesse Morgan <jesse@jesterpm.net> | 2013-09-10 00:06:54 -0700 |
---|---|---|
committer | Jesse Morgan <jesse@jesterpm.net> | 2013-09-10 00:06:54 -0700 |
commit | 5306005833083589d0f93b0966b458da969e5f38 (patch) | |
tree | 9779a58d78bdcf27311e2593174afb83041a21fa /src/com/p4square/grow/backend | |
parent | 90c95d9bf125dabb5bd34954394127cf2c1bb1b1 (diff) |
Adding redirect page /account.
/account redirects to either the assessment or the training page, as
appropriate.
This change adds the /accounts/UID backend resource, which simply stores
an arbitrary json document for now.
Diffstat (limited to 'src/com/p4square/grow/backend')
-rw-r--r-- | src/com/p4square/grow/backend/GrowBackend.java | 7 | ||||
-rw-r--r-- | src/com/p4square/grow/backend/resources/AccountResource.java | 71 |
2 files changed, 77 insertions, 1 deletions
diff --git a/src/com/p4square/grow/backend/GrowBackend.java b/src/com/p4square/grow/backend/GrowBackend.java index d072dfb..09c1d84 100644 --- a/src/com/p4square/grow/backend/GrowBackend.java +++ b/src/com/p4square/grow/backend/GrowBackend.java @@ -15,10 +15,12 @@ import org.restlet.routing.Router; import com.p4square.grow.config.Config; import com.p4square.grow.backend.db.CassandraDatabase; + +import com.p4square.grow.backend.resources.AccountResource; import com.p4square.grow.backend.resources.SurveyResource; import com.p4square.grow.backend.resources.SurveyResultsResource; -import com.p4square.grow.backend.resources.TrainingResource; import com.p4square.grow.backend.resources.TrainingRecordResource; +import com.p4square.grow.backend.resources.TrainingResource; /** * Main class for the backend application. @@ -40,6 +42,9 @@ public class GrowBackend extends Application { public Restlet createInboundRoot() { Router router = new Router(getContext()); + // Account API + router.attach("/accounts/{userId}", AccountResource.class); + // Survey API router.attach("/assessment/question/{questionId}", SurveyResource.class); diff --git a/src/com/p4square/grow/backend/resources/AccountResource.java b/src/com/p4square/grow/backend/resources/AccountResource.java new file mode 100644 index 0000000..f3404c0 --- /dev/null +++ b/src/com/p4square/grow/backend/resources/AccountResource.java @@ -0,0 +1,71 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.backend.resources; + +import java.io.IOException; + +import org.restlet.data.Status; +import org.restlet.resource.ServerResource; +import org.restlet.representation.Representation; +import org.restlet.representation.StringRepresentation; + +import org.apache.log4j.Logger; + +import com.p4square.grow.backend.GrowBackend; +import com.p4square.grow.backend.db.CassandraDatabase; + +/** + * Stores a document about a user. + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class AccountResource extends ServerResource { + private static final Logger LOG = Logger.getLogger(AccountResource.class); + + private CassandraDatabase mDb; + + private String mUserId; + + @Override + public void doInit() { + super.doInit(); + + final GrowBackend backend = (GrowBackend) getApplication(); + mDb = backend.getDatabase(); + + mUserId = getAttribute("userId"); + } + + /** + * Handle GET Requests. + */ + @Override + protected Representation get() { + String result = mDb.getKey("accounts", mUserId); + + if (result == null) { + setStatus(Status.CLIENT_ERROR_NOT_FOUND); + return null; + } + + return new StringRepresentation(result); + } + + /** + * Handle PUT requests + */ + @Override + protected Representation put(Representation entity) { + try { + mDb.putKey("accounts", mUserId, entity.getText()); + setStatus(Status.SUCCESS_NO_CONTENT); + + } catch (IOException e) { + setStatus(Status.SERVER_ERROR_INTERNAL); + } + + return null; + } +} |