Skip to content

Feature/friendspage #228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added src/.DS_Store
Binary file not shown.
Binary file added src/main/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import java.util.Set;

@RestController
public class UsersController {
@Autowired
Expand All @@ -27,4 +30,25 @@ public RedirectView afterLogin() {

return new RedirectView("/posts");
}

@GetMapping("/users/friends")
public ModelAndView viewFriends() {
DefaultOidcUser principal = (DefaultOidcUser) SecurityContextHolder
.getContext()
.getAuthentication()
.getPrincipal();
String username = (String) principal.getAttributes().get("email");

// Find the user by username
User currentUser = userRepository.findUserByUsername(username)
.orElseThrow(() -> new RuntimeException("User not found"));

// Get friends
Set<User> friends = currentUser.getFriends();

// Pass friends to the view named "friends"
ModelAndView mav = new ModelAndView("friends");
mav.addObject("friends", friends);
return mav;
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/makersacademy/acebook/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import jakarta.persistence.*;
import lombok.Data;

import java.util.HashSet;
import java.util.Set;

import static java.lang.Boolean.TRUE;

@Data
Expand All @@ -19,6 +22,17 @@ public User() {
this.enabled = TRUE;
}

// Join table to link user ID with friend ID
@ManyToMany
@JoinTable(
name = "friends",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "friend_id")
)

// Using hashset to store friends to prevent duplicates and lets us do faster access/search
private Set<User> friends = new HashSet<>();

public User(String username) {
this.username = username;
this.enabled = TRUE;
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application-test.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ spring.datasource.username=
spring.datasource.password=
flyway.baseline-on-migrate=true
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
6 changes: 3 additions & 3 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
okta:
oauth2:
issuer: https://dev-edward-andress.uk.auth0.com/
client-id: ${OKTA_CLIENT_ID}
client-secret: ${OKTA_CLIENT_SECRET}
issuer: ${ISSUER_ACEBOOK}
client-id: ${CLIENT_ID_ACEBOOK}
client-secret: ${CLIENT_SECRET_ACEBOOK}
10 changes: 10 additions & 0 deletions src/main/resources/db/migration/V4__create_friends_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE friends (
user_id bigint NOT NULL,
friend_id bigint NOT NULL,
PRIMARY KEY (user_id, friend_id),

-- These constraints force the tables to use only existing user ids.
-- On delete cascade deletes friendships if either user or friend deletes their account.
CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
CONSTRAINT fk_friend FOREIGN KEY (friend_id) REFERENCES users(id) ON DELETE CASCADE
);
13 changes: 13 additions & 0 deletions src/main/resources/templates/friends.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Friends</title>
</head>
<body>
<ul th:each="friend: ${friends}">
<li th:text="${friend.username}" />
</ul>

</body>
</html>
18 changes: 10 additions & 8 deletions src/test/java/com/makersacademy/acebook/feature/SignUpTest.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
package com.makersacademy.acebook.feature;

import com.github.javafaker.Faker;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class SignUpTest {

WebDriver driver;
Faker faker;

@Before
@BeforeEach
public void setup() {
System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
driver = new ChromeDriver();
faker = new Faker();
}

@After
@AfterEach
public void tearDown() {
driver.close();
driver.quit(); // safer than close()
}

@Test
Expand All @@ -37,6 +38,7 @@ public void successfulSignUpAlsoLogsInUser() {
driver.findElement(By.name("action")).click();
driver.findElement(By.name("action")).click();
String greetingText = driver.findElement(By.id("greeting")).getText();
Assert.assertEquals("Signed in as " + email, greetingText);

assertEquals("Signed in as " + email, greetingText);
}
}
22 changes: 22 additions & 0 deletions src/test/java/com/makersacademy/acebook/model/UserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.makersacademy.acebook.model;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItem;

import org.junit.jupiter.api.Test;

public class UserTest {

private User karen = new User("Karen", true);
private User zehad = new User("Zehad", true);

@Test
public void checkWhoIsFriends() {
// Hashset friends table is not bidirectional,
// Both friends must be added each other
karen.getFriends().add(zehad);
zehad.getFriends().add(karen);
assertThat(karen.getFriends(), hasItem(zehad));
assertThat(zehad.getFriends(), hasItem(karen));
}
}