Skip to content

Commit

Permalink
account register handler
Browse files Browse the repository at this point in the history
  • Loading branch information
jmsundin committed Jan 25, 2024
1 parent fe8f705 commit 449b3b0
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/main/java/Controller/SocialMediaController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -18,6 +32,7 @@ public Javalin startAPI() {
Javalin app = Javalin.create();
app.get("example-endpoint", this::exampleHandler);

app.post("/register", this::registerHandler);
return app;
}

Expand All @@ -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));
}

}
}

0 comments on commit 449b3b0

Please sign in to comment.