From 449b3b039b398f4df663a50d590c1ca6fe6e398e Mon Sep 17 00:00:00 2001 From: Jon Sundin Date: Thu, 25 Jan 2024 23:47:03 +0000 Subject: [PATCH] account register handler --- .../Controller/SocialMediaController.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/main/java/Controller/SocialMediaController.java b/src/main/java/Controller/SocialMediaController.java index 5094705..27fa873 100644 --- a/src/main/java/Controller/SocialMediaController.java +++ b/src/main/java/Controller/SocialMediaController.java @@ -3,12 +3,26 @@ import io.javalin.Javalin; import io.javalin.http.Context; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +import Model.Account; +import Model.Message; +import Service.AccountService; +import Service.MessageService; + /** * TODO: You will need to write your own endpoints and handlers for your controller. The endpoints you will need can be * found in readme.md as well as the test cases. You should * refer to prior mini-project labs and lecture materials for guidance on how a controller may be built. */ public class SocialMediaController { + AccountService accountService; + + public SocialMediaController(){ + accountService = new AccountService(); + } + /** * In order for the test cases to work, you will need to write the endpoints in the startAPI() method, as the test * suite must receive a Javalin object from this method. @@ -18,6 +32,7 @@ public Javalin startAPI() { Javalin app = Javalin.create(); app.get("example-endpoint", this::exampleHandler); + app.post("/register", this::registerHandler); return app; } @@ -29,5 +44,17 @@ private void exampleHandler(Context context) { context.json("sample text"); } + private void registerHandler(Context ctx) throws JsonProcessingException { + ObjectMapper mapper = new ObjectMapper(); + Account account = mapper.readValue(ctx.body(), Account.class); + Account addedAccount = accountService.addAccount(account); + System.out.println(addedAccount); + + if(addedAccount == null){ + ctx.status(400); + }else{ + ctx.json(mapper.writeValueAsString(addedAccount)); + } + } } \ No newline at end of file