diff --git a/src/main/java/Controller/SocialMediaController.java b/src/main/java/Controller/SocialMediaController.java index eb261b0..4e208d9 100644 --- a/src/main/java/Controller/SocialMediaController.java +++ b/src/main/java/Controller/SocialMediaController.java @@ -6,29 +6,36 @@ import com.fasterxml.jackson.core.JsonProcessingException; import Model.Account; -// import Model.Message; +import Model.Message; import Service.AccountService; -// import Service.MessageService; +import Service.MessageService; import java.util.List; // import java.util.ArrayList; /** - * TODO: You will need to write your own endpoints and handlers for your controller. The endpoints you will need can be + * 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. + * refer to prior mini-project labs and lecture materials for guidance on how a + * controller may be built. */ public class SocialMediaController { AccountService accountService; + MessageService messageService; - public SocialMediaController(){ + public SocialMediaController() { accountService = new AccountService(); + messageService = new MessageService(); } /** - * In order for the test cases to work, you will need to write the endpoints in the startAPI() method, as the test + * 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. - * @return a Javalin app object which defines the behavior of the Javalin controller. + * + * @return a Javalin app object which defines the behavior of the Javalin + * controller. */ public Javalin startAPI() { Javalin app = Javalin.create(); @@ -36,22 +43,25 @@ public Javalin startAPI() { app.get("/getAccountTable", this::getAccountTable); app.post("/register", this::register); app.post("/login", this::login); + app.post("/messages", this::messages); return app; } /** * This is an example handler for an example endpoint. - * @param context The Javalin Context object manages information about both the HTTP request and response. + * + * @param context The Javalin Context object manages information about both the + * HTTP request and response. */ private void exampleHandler(Context context) { - context.json("sample text"); + context.json("Hello world"); } private void register(Context context) throws JsonProcessingException { Account accountFromBody = context.bodyAsClass(Account.class); - if (!accountService.isValidCredentials(accountFromBody.username, accountFromBody.password)) { + if (!accountService.isValidCredentials(accountFromBody.username, accountFromBody.password_)) { context.result("").status(400); } else { Account registeredAccount = accountService.addAccount(accountFromBody); @@ -69,8 +79,8 @@ private void login(Context context) { if (!accountService.accountIsRegistered(account.username)) { context.status(401); } else { - Account accountLoggedIn = accountService.login(account.username, account.password); - + Account accountLoggedIn = accountService.login(account.username, account.password_); + if (accountLoggedIn == null) { context.status(401); } else { @@ -79,10 +89,26 @@ private void login(Context context) { } } + private void messages(Context context) { + Message messageFromBody = context.bodyAsClass(Message.class); + int posted_by = messageFromBody.posted_by; + String message_text = messageFromBody.message_text; + long time_posted_epoch = messageFromBody.time_posted_epoch; + + if (messageService.messageIsValid(posted_by, message_text)) { + Message message = messageService.insertMessage(posted_by, message_text, time_posted_epoch); + if (message != null) { + context.json(message).status(200); + } else { + + } + } else { + context.status(400); + } + } + private void getAccountTable(Context context) { List accounts = accountService.getAllAccounts(); context.json(accounts).status(200); } - - } } \ No newline at end of file