Skip to content

Commit

Permalink
controller endpoints getAccountTable, register, login
Browse files Browse the repository at this point in the history
  • Loading branch information
jmsundin committed Jan 28, 2024
1 parent 7b7a17c commit f1ce0d6
Showing 1 changed file with 42 additions and 14 deletions.
56 changes: 42 additions & 14 deletions src/main/java/Controller/SocialMediaController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import io.javalin.http.Context;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

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
Expand All @@ -30,9 +32,11 @@ public SocialMediaController(){
*/
public Javalin startAPI() {
Javalin app = Javalin.create();
app.get("example-endpoint", this::exampleHandler);
app.get("/", this::exampleHandler);
app.get("/getAccountTable", this::getAccountTable);
app.post("/register", this::register);
app.post("/login", this::login);

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

Expand All @@ -44,17 +48,41 @@ 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);
private void register(Context context) throws JsonProcessingException {
Account accountFromBody = context.bodyAsClass(Account.class);

if (!accountService.isValidCredentials(accountFromBody.username, accountFromBody.password)) {
context.result("").status(400);
} else {
Account registeredAccount = accountService.addAccount(accountFromBody);
if (registeredAccount != null) {
context.json(registeredAccount).status(200);
} else {
context.result("").status(400);
}
}
}

private void login(Context context) {
Account account = context.bodyAsClass(Account.class);

if(addedAccount == null){
ctx.status(400);
}else{
ctx.json(mapper.writeValueAsString(addedAccount));
if (!accountService.accountIsRegistered(account.username)) {
context.status(401);
} else {
Account accountLoggedIn = accountService.login(account.username, account.password);

if (accountLoggedIn == null) {
context.status(401);
} else {
context.json(accountLoggedIn).status(200);
}
}
}

private void getAccountTable(Context context) {
List<Account> accounts = accountService.getAllAccounts();
context.json(accounts).status(200);
}

}
}

0 comments on commit f1ce0d6

Please sign in to comment.