Skip to content

Commit

Permalink
implemented messages endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jmsundin committed Jan 30, 2024
1 parent 40be87b commit 10024e0
Showing 1 changed file with 40 additions and 14 deletions.
54 changes: 40 additions & 14 deletions src/main/java/Controller/SocialMediaController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,62 @@
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();
app.get("/", this::exampleHandler);
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);
Expand All @@ -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 {
Expand All @@ -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<Account> accounts = accountService.getAllAccounts();
context.json(accounts).status(200);
}

}
}

0 comments on commit 10024e0

Please sign in to comment.