Skip to content

Final polish to pages. #242

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 201 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
201 commits
Select commit Hold shift + click to select a range
6deeccd
Log in working and Posts page loading.
pazzer Jun 2, 2025
135a5be
Fighting with git.
pazzer Jun 2, 2025
5cc2479
Tests passing.
pazzer Jun 2, 2025
e358171
Merge pull request #1 from jamesdismore/first_bugs
pazzer Jun 2, 2025
045ab91
fixed_db
jamesdismore Jun 3, 2025
bf49303
initial commit - starting html
maddyjm Jun 3, 2025
295c1a9
changed_database_added_to_post_model
jamesdismore Jun 3, 2025
b517375
added_seed_data_to_test
jamesdismore Jun 3, 2025
db4e01f
screened users, new users now diverted to registration page
maddyjm Jun 3, 2025
5125f96
continued working
maddyjm Jun 3, 2025
d5ff782
seed_data_working
jamesdismore Jun 3, 2025
021f8b1
post_and_user_models_working
jamesdismore Jun 3, 2025
5d7a8a3
friendship_and_comment_models_added
jamesdismore Jun 3, 2025
b7609f3
all_models_added
jamesdismore Jun 3, 2025
7aaf6c7
added_all_repos
jamesdismore Jun 3, 2025
1b3767d
attempting errors
maddyjm Jun 3, 2025
58e6e3a
error message shows - added dependancy
maddyjm Jun 3, 2025
8573b48
added_more_seed_data
jamesdismore Jun 4, 2025
722452c
fixed_seed_data
jamesdismore Jun 4, 2025
28dc283
Nav bar, templates, and routes
ItsProbablyAndy Jun 4, 2025
b558b3b
Added disabled links for home and settings
ItsProbablyAndy Jun 4, 2025
0193957
removing commented out code and adding explanations
maddyjm Jun 4, 2025
d2067b4
saving_to_change_branch
jamesdismore Jun 4, 2025
74879e7
html comments and cleaning
maddyjm Jun 4, 2025
dad822a
final changes
maddyjm Jun 4, 2025
2d52f5a
adding stuff so that if user is not saved in OUR database, they get r…
maddyjm Jun 4, 2025
5cfa806
made some changes so that a user can be greeted by their database inf…
maddyjm Jun 4, 2025
335e70e
final PUSH
maddyjm Jun 4, 2025
74def18
after_claire
jamesdismore Jun 4, 2025
9804588
james_merge_database_setup
jamesdismore Jun 4, 2025
698756c
Merge branch 'main' into new-user-page
jamesdismore Jun 4, 2025
d0e4733
Merge pull request #3 from jamesdismore/new-user-page
jamesdismore Jun 4, 2025
d5b6366
Added placeholder avatar
ItsProbablyAndy Jun 4, 2025
6d5fa84
merging changes between main and htmlnavbar
ItsProbablyAndy Jun 4, 2025
acae8e0
fixed_autowire_in_posts
jamesdismore Jun 4, 2025
a06dc78
fixing_users_post_merge
jamesdismore Jun 4, 2025
701c923
redirecting user not logged in, and also got rid of fake index page
maddyjm Jun 4, 2025
37c4ca0
Resolved local bugs; building successfully.
pazzer Jun 4, 2025
72dcc4c
Settings page loading, and changes taking effect.
pazzer Jun 5, 2025
b1c1152
MVP Settings page up and running.
pazzer Jun 5, 2025
33bdad5
Merge pull request #5 from jamesdismore/settings_page
pazzer Jun 5, 2025
cf3de78
friend_requests_table_in_db
jamesdismore Jun 5, 2025
65d9d49
implements full avatar fetch into navbar for user
ItsProbablyAndy Jun 5, 2025
8020b4d
friend request repo and model done
jamesdismore Jun 5, 2025
e076f4a
Model fields now use camelCase, rather than snake_case.
pazzer Jun 5, 2025
7fbecce
Merge pull request #6 from jamesdismore/making_camelcase
pazzer Jun 5, 2025
5461b79
reworked friendships
jamesdismore Jun 5, 2025
453e3d8
reworked friendships inc models and repo
jamesdismore Jun 5, 2025
42f3f6a
+ Added Bootstrap as local file.
pazzer Jun 6, 2025
ac5cb39
Added bootstrap.bundle.min.js locally, and updated links in templates…
pazzer Jun 6, 2025
a7f770d
+ Added .DS_Store to gitignore.
pazzer Jun 6, 2025
9e3fb5b
Removed .DS_Store files.
pazzer Jun 6, 2025
5708c29
Removed .DS_Store files.
pazzer Jun 6, 2025
736e8e3
Merge branch 'main' into add_friends
jamesdismore Jun 6, 2025
03d0bb3
Merge pull request #7 from jamesdismore/add_friends
jamesdismore Jun 6, 2025
36a203a
application.yml hard coded
jamesdismore Jun 6, 2025
6f8ec04
+ Changed int id type to long.
pazzer Jun 6, 2025
30d1408
Merge branch 'main' into friday_am_merge
pazzer Jun 6, 2025
92d0597
Merge pull request #8 from jamesdismore/friday_am_merge
pazzer Jun 6, 2025
5bcfe14
fixing dupe from paul merge
jamesdismore Jun 6, 2025
3e6348a
Actually added the commit this time nav bar fix and working dont break
ItsProbablyAndy Jun 6, 2025
b4c3377
Merge branch 'main' of https://github.com/jamesdismore/makersbook-alb…
ItsProbablyAndy Jun 6, 2025
b61b6d7
changed paul's v10 sql
jamesdismore Jun 6, 2025
4f27547
Relationship gone.
pazzer Jun 6, 2025
dd86dab
Merge pull request #11 from jamesdismore/remove_relationship
pazzer Jun 6, 2025
9cc5a03
cleaned up db
jamesdismore Jun 6, 2025
9f4339e
Merge pull request #12 from jamesdismore/james_cleaning_up
jamesdismore Jun 6, 2025
170f1c2
reenabled posts using user_id method
jamesdismore Jun 6, 2025
ad85eab
Tidied PostController.
pazzer Jun 6, 2025
57431de
Finally changes from James.
pazzer Jun 6, 2025
b61546f
Adjustments to friend_requests.
pazzer Jun 9, 2025
4ef0be3
added friendrequest by userid and status
jamesdismore Jun 9, 2025
ebd3115
pending_request_ui
pazzer Jun 9, 2025
5ff37b6
Pulling down James' DB query to access pending outgoing friend reques…
pazzer Jun 9, 2025
c5d4ed3
fixed friends request model
jamesdismore Jun 9, 2025
3618217
added friendrequest TO user id and status method
jamesdismore Jun 9, 2025
d2a14f6
Pending requests now appearing.
pazzer Jun 9, 2025
14ed012
Pulled request message change.
pazzer Jun 9, 2025
3b81762
unfriend working
jamesdismore Jun 9, 2025
cd76e8c
unfriend ACTUALLY working
jamesdismore Jun 9, 2025
6cb5a6c
implemented comment system to posts page
ItsProbablyAndy Jun 9, 2025
29fe4e8
profile links work
jamesdismore Jun 9, 2025
31b4170
fixed friend repo typo
jamesdismore Jun 9, 2025
0a79421
Added send request form.
pazzer Jun 9, 2025
84740c2
user search by like username first name or last name
jamesdismore Jun 9, 2025
d7d1103
initial commit, photos are added
maddyjm Jun 9, 2025
f57e9c4
user search order priority working
jamesdismore Jun 9, 2025
0b7dcd1
names and last names show up
maddyjm Jun 10, 2025
6fdf97f
about to start DisplayComment and DisplayPost
maddyjm Jun 10, 2025
9de36fc
+ Added _fragments file with 'placeholder' fragment.
pazzer Jun 10, 2025
5c3e0d0
Merge commit. Merge branch 'friends_update_with_paul' of https://gith…
pazzer Jun 10, 2025
9b5523b
Merge branch 'main' into Friends
pazzer Jun 10, 2025
c9c6489
Merge pull request #13 from jamesdismore/Friends
pazzer Jun 10, 2025
f9a4382
Updated accordion behaviour in friendRequests.html
pazzer Jun 10, 2025
14551c3
Merge branch 'Friends'
pazzer Jun 10, 2025
b89c2ca
merged with main
maddyjm Jun 10, 2025
7b59550
Merge pull request #14 from jamesdismore/first-last-names-comments
maddyjm Jun 10, 2025
19f587b
edited new user to save avatar file correctly
maddyjm Jun 10, 2025
5718f25
Merge branch 'main' into fixing-placeholder-image-and-name
maddyjm Jun 10, 2025
9582baf
Added more placeholder conditionals and changed button name. Now tryi…
pazzer Jun 10, 2025
773db10
fallback for null user
maddyjm Jun 10, 2025
c3e8b0c
Merge pull request #15 from jamesdismore/fixing-placeholder-image-and…
pazzer Jun 10, 2025
fa9558b
Fixed button alignment issues.
pazzer Jun 10, 2025
ae10e9e
posts now show in reverse chronological order
maddyjm Jun 10, 2025
1dee130
deleted commented out GetMapping for posts/comment
maddyjm Jun 10, 2025
07d562f
Merging Maddy's Tuesday am changes into Friends.
pazzer Jun 10, 2025
b65e629
Merge pull request #16 from jamesdismore/fixing-placeholder-image-and…
jamesdismore Jun 10, 2025
0f8d4aa
mixed up posts and comments it's fixed now
maddyjm Jun 10, 2025
f5908cd
Merge pull request #17 from jamesdismore/fixing-placeholder-image-and…
maddyjm Jun 10, 2025
73478c2
html css wip
vicrbsn Jun 10, 2025
a4ed18a
pull from main
vicrbsn Jun 10, 2025
5af0eb0
friends page displaying search results, also links to /manageFriends
jamesdismore Jun 10, 2025
39e3a7e
Finishing profile page - can select image, but not currently writing …
pazzer Jun 10, 2025
8b9d40b
friends search excludes existing friends and yourself
jamesdismore Jun 10, 2025
314ffa0
about to see if things work
maddyjm Jun 10, 2025
3361e9c
trying to add comment likes
maddyjm Jun 10, 2025
a284973
thanks ai for your gracious service in helping us get likes working
maddyjm Jun 10, 2025
96fdf20
Minor changes associated with saving image files.
pazzer Jun 10, 2025
dcc3c1a
Can now change avatar via setting page.
pazzer Jun 10, 2025
17f8622
Can now respond to incoming friend requests.
pazzer Jun 11, 2025
ae3ecd5
Added ability to reject friend requests.
pazzer Jun 11, 2025
fad0121
Added flash message to settings page following successful update.
pazzer Jun 11, 2025
c1c38b8
already friend request sent back end working
jamesdismore Jun 11, 2025
e78b2af
html built for already requested
jamesdismore Jun 11, 2025
b574db6
Merge pull request #18 from jamesdismore/Friends
pazzer Jun 11, 2025
1a6cae5
Merge pull request #19 from jamesdismore/funky-javascript-for-likes
maddyjm Jun 11, 2025
c3a4359
friend already requested working
jamesdismore Jun 11, 2025
d8337d2
index and profile nav bar profile redirect to profile/userid of the u…
maddyjm Jun 11, 2025
618162e
Navbar arrives in each file via Thymeleaf fragments.
pazzer Jun 11, 2025
0353e4e
individual user posts are showing + comments on those posts
maddyjm Jun 11, 2025
2780a80
Added friendship pair between hen and dog.
pazzer Jun 11, 2025
871bfbe
users can add likes and comments onto the posts of users from their p…
maddyjm Jun 11, 2025
890b8ec
unfriend/addfriend/withdrawrequest buttons are different colours
jamesdismore Jun 11, 2025
e675f61
added some comments
jamesdismore Jun 11, 2025
8ecb311
Pulled in James's friends work.
pazzer Jun 11, 2025
5de1eb5
Merge branch 'plumbing_in_friends' of https://github.com/jamesdismore…
jamesdismore Jun 11, 2025
3f073e0
bugfixes after paul merge
jamesdismore Jun 11, 2025
60f32ee
profile picture shows on page
maddyjm Jun 11, 2025
8c3abb3
Fixed bug regarding friend request form.
pazzer Jun 11, 2025
38b545c
name + avatar + banner space on profile page
maddyjm Jun 11, 2025
5d23e51
Updated HTML+CSS for both pages
vicrbsn Jun 11, 2025
12f34f7
Merge pull request #20 from jamesdismore/profile-page
jamesdismore Jun 11, 2025
02d122c
cover banners correspond to profile picture colours
maddyjm Jun 11, 2025
6b1982d
Merge branch 'main' into post_pictures
pazzer Jun 11, 2025
7578c54
merged from main
vicrbsn Jun 11, 2025
609d3aa
button only shows on comments where the comment id matches user id
maddyjm Jun 11, 2025
70fcc7c
Merge branch 'frontend3' of https://github.com/jamesdismore/makersboo…
pazzer Jun 11, 2025
0ad4021
comments can be deleted
maddyjm Jun 11, 2025
6b64a87
moving to new branch
jamesdismore Jun 11, 2025
36f89f9
bug fixes so that comment functionality shows correctly on both index…
maddyjm Jun 11, 2025
19ba79f
seems to work
jamesdismore Jun 11, 2025
07782c1
Merge pull request #21 from jamesdismore/delete-comments
jamesdismore Jun 11, 2025
024962e
Loading with Victoria's changes.
pazzer Jun 11, 2025
6b385e4
Positioning "manage friends" arrow on /friends page.
pazzer Jun 11, 2025
5d1f7ac
layout finished but still no scrolling.
pazzer Jun 11, 2025
6b10b6b
deleted user has special avatar
jamesdismore Jun 12, 2025
7885f86
updated styling for index, applied overlay effect to profile page
vicrbsn Jun 12, 2025
adcd1d0
Added ability to move from friends search page to friends request page.
pazzer Jun 12, 2025
971bbe8
Reinstated main.css class - scrolling broken in other pages.
pazzer Jun 12, 2025
55099c7
Merged post_pictures into main.
pazzer Jun 12, 2025
05131ca
Merge pull request #23 from jamesdismore/delete_user
jamesdismore Jun 12, 2025
f71c86e
initial commit
maddyjm Jun 12, 2025
33ab5c8
Merge pull request #24 from jamesdismore/migrating-from-index
maddyjm Jun 12, 2025
14248ce
deleted index
maddyjm Jun 12, 2025
436953d
merge from main
jamesdismore Jun 12, 2025
0968fa7
profile button on friends page leads to profile apge of friend
maddyjm Jun 12, 2025
1c14557
names link to profile page, going to try images
maddyjm Jun 12, 2025
4700f01
images are also linked, now for comments
maddyjm Jun 12, 2025
8191b6d
clicking on user's names and images from posts page (posts and commen…
maddyjm Jun 12, 2025
c6f8636
the previous functionalities now also work on the profile page (for c…
maddyjm Jun 12, 2025
2ee11b2
added profile add/remove/withdraw friend buttons
jamesdismore Jun 12, 2025
04d9bbd
more html+css
vicrbsn Jun 12, 2025
12b7bd1
Merge pull request #26 from jamesdismore/fixing_buttons
maddyjm Jun 12, 2025
7bbab35
rename index
vicrbsn Jun 12, 2025
1e5684e
add/remove/withdraw button doesn't show up for yourself
jamesdismore Jun 12, 2025
df59ff1
pull from main
vicrbsn Jun 12, 2025
d657d61
Merge pull request #27 from jamesdismore/my_profile_fix
maddyjm Jun 12, 2025
ebefbba
clicking avatar in nav goes to your profile
maddyjm Jun 12, 2025
1685016
clicking Acebook in Nav redirects to Home
maddyjm Jun 12, 2025
90aa8a4
Have improved banner.
pazzer Jun 12, 2025
acef211
fix bugs from merge
vicrbsn Jun 12, 2025
9cf17bd
pull from main
vicrbsn Jun 12, 2025
61cf286
Merged in 15:00 12/6.Merge branch 'main' into profile_polish
pazzer Jun 12, 2025
da85c28
Added banner styling.
pazzer Jun 12, 2025
0958c66
fixing errors and formatting inconsistencies
vicrbsn Jun 12, 2025
762d165
Merge pull request #28 from jamesdismore/frontend3
vicrbsn Jun 12, 2025
a9b2967
Merge pull request #29 from jamesdismore/color-thief
maddyjm Jun 12, 2025
ba7215c
Color thief appears to be working.
pazzer Jun 12, 2025
2f740cf
Brought in latest changes from main.
pazzer Jun 12, 2025
2671427
Header elements aligning left again.
pazzer Jun 12, 2025
eb92dee
Minor changes to improve layout.
pazzer Jun 12, 2025
05de07d
+ Relocated buttons on profile page
pazzer Jun 12, 2025
9a666e2
Added username to profile page.
pazzer Jun 13, 2025
2fc5e59
Adjusted padding on profile page.
pazzer Jun 13, 2025
736c151
+ Added info banner to new user page.
pazzer Jun 13, 2025
44bbfd9
Removed demo html file.
pazzer Jun 13, 2025
6119df2
updating profile picture previews before updating:
maddyjm Jun 13, 2025
a698fb2
fixed class name for some mb-3 / post styling and added missing div ends
vicrbsn Jun 13, 2025
e037579
merge
vicrbsn Jun 13, 2025
3a498ef
Merged in main after Maddy and Victoria changes.
pazzer Jun 13, 2025
af1d739
Changed salmon picture.
pazzer Jun 13, 2025
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ dependency-reduced-pom.xml
.factorypath
.project
.settings/
application.yaml
application.yml
.DS_Store
29 changes: 29 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@
<properties>
<java.version>21</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.11.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
Expand All @@ -42,6 +48,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
Expand All @@ -66,6 +76,11 @@
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.11.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down Expand Up @@ -95,6 +110,11 @@
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity6</artifactId>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.11.1</version>
</dependency>
</dependencies>

<build>
Expand All @@ -118,6 +138,15 @@
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>11.7.2</version>
<configuration>
<url>jdbc:postgresql://localhost:5432/acebook_springboot_development</url>
<cleanDisabled>false</cleanDisabled>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
package com.makersacademy.acebook.config;

import com.makersacademy.acebook.model.User;
import com.makersacademy.acebook.repository.UserRepository;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import java.io.IOException;
import java.util.Optional;

import static org.springframework.security.config.Customizer.withDefaults;

@Configuration
@EnableWebSecurity
public class SecurityConfiguration {

@Autowired
private UserRepository userRepository;

@Value("${okta.oauth2.issuer}")
private String issuer;
@Value("${okta.oauth2.client-id}")
Expand All @@ -31,7 +41,7 @@ public class SecurityConfiguration {
@Bean
public SecurityFilterChain configure(HttpSecurity http) throws Exception {
http
.csrf(csrf -> csrf.disable())
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(authorize -> authorize
.requestMatchers("/", "/images/**").permitAll()
.anyRequest().authenticated()
Expand All @@ -40,7 +50,15 @@ public SecurityFilterChain configure(HttpSecurity http) throws Exception {
.successHandler(new AuthenticationSuccessHandler() {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
response.sendRedirect("/users/after-login");
DefaultOidcUser principal = (DefaultOidcUser) authentication.getPrincipal();
String username = (String) principal.getAttributes().get("email");
Optional<User> user = userRepository.findUserByUsername(username);
if (user.isEmpty()) {
response.sendRedirect("/users/after-login"); // Redirect if user is missing
} else {
response.sendRedirect("/users/after-login");
//^^ if user is not in OUR database, they get redirected
}
}
})
)
Expand All @@ -60,4 +78,4 @@ private LogoutHandler logoutHandler() {
}
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package com.makersacademy.acebook.controller;

import com.makersacademy.acebook.model.FriendRequest;
import com.makersacademy.acebook.model.Friendship;
import com.makersacademy.acebook.model.User;
import com.makersacademy.acebook.repository.FriendRequestRepository;
import com.makersacademy.acebook.repository.FriendshipRepository;
import com.makersacademy.acebook.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.thymeleaf.util.ArrayUtils;

import java.util.ArrayList;
import java.util.Objects;
import java.util.Optional;

@Controller
public class FriendsController {
@Autowired
FriendshipRepository friendshipRepository;
@Autowired
FriendRequestRepository friendRequestRepository;
@Autowired
UserRepository userRepository;

@ModelAttribute("user")
public Optional<User> getUser(Authentication authentication) {
DefaultOidcUser principal = (DefaultOidcUser) authentication.getPrincipal();
String username = (String) principal.getAttributes().get("email");
return userRepository.findUserByUsername(username);
}

@GetMapping("/friends")
public String friends(Model model, Authentication authentication){
DefaultOidcUser principal = (DefaultOidcUser) authentication.getPrincipal();
String username = (String) principal.getAttributes().get("email");
Optional<User> user = userRepository.findUserByUsername(username);
if (user.isEmpty()) {
return "redirect:/users/newUser"; // Redirect if not registered
}
Long userIdLong = user.get().getId(); // getting id from database - checking that id is connected
int userId = Math.toIntExact(userIdLong);
ArrayList<Friendship> friendsListArray = friendshipRepository.findFriendshipByUserId(userId);

// String friendsListEmails = "";
ArrayList<User> friendsUserListArray = new ArrayList<User>();


// int arrayPos = 0;
for (Friendship friendship :friendsListArray){
Optional<User> friendOptional = userRepository.findById((long) friendship.getFriendId());
User friend = friendOptional.get();
friendsUserListArray.add(friend);
}
model.addAttribute("friends",friendsUserListArray);
model.addAttribute("user",user.get());
return "friends";
}

@PostMapping("/friends/unfriend")
public String unfriend(@RequestParam("unfriendId") Long unfriendId,@RequestParam("userId") Long userId) {
int unfriendInt = unfriendId.intValue();
int userInt = userId.intValue();
ArrayList<Friendship> friendshipList = friendshipRepository.findFriendshipByUserIdAndFriendId(unfriendInt,userInt);
ArrayList<Friendship> friendshipFrom = friendshipRepository.findFriendshipByUserIdAndFriendId(userInt,unfriendInt);
friendshipList.addAll(friendshipFrom);


for (Friendship friendship : friendshipList){
friendshipRepository.delete(friendship);
}
return "redirect:/friends";
}

@PostMapping("/friends/search")
public String searchFriends(@RequestParam(value = "searchQuery", defaultValue = "") String searchQuery, Model model, Authentication authentication) {
// Get the logged-in user's details
DefaultOidcUser principal = (DefaultOidcUser) authentication.getPrincipal();
String username = (String) principal.getAttributes().get("email");
Optional<User> user = userRepository.findUserByUsername(username);

if (user.isEmpty()) {
return "redirect:/users/newUser"; // Redirect if not registered
}


// Search for users matching the query
String queryLow = searchQuery.toLowerCase();
ArrayList<User> approxOutput = userRepository.approximateUserSearch(queryLow);
ArrayList<User> exactOutput = userRepository.exactUserSearch(queryLow);
for (User userSearched : approxOutput)
{
if(!exactOutput.contains(userSearched)){
exactOutput.add(userSearched);
}
}
ArrayList<Friendship> friendshipsArray = friendshipRepository.findFriendshipByUserId(user.get().getId().intValue());
ArrayList<Long> friendIds = new ArrayList<>();
for(Friendship friendship : friendshipsArray){
friendIds.add(friendship.getFriendId());
}
exactOutput.removeIf(searchedUser -> friendIds.contains(searchedUser.getId()));
exactOutput.removeIf(searchedUser -> Objects.equals(searchedUser.getId(), user.get().getId()));

// Identifying which friends within the search have already been friended
ArrayList<FriendRequest> existingRequests = friendRequestRepository.findFriendRequestByfromUserIdAndStatus(user.get().getId(),"PENDING");
ArrayList<User> alreadyFriended = new ArrayList<>(exactOutput);


ArrayList<Long> existingRequestIds = new ArrayList<>();
for(FriendRequest friendRequest : existingRequests){
existingRequestIds.add(friendRequest.getToUserId());
}

alreadyFriended.removeIf(userFriend -> !existingRequestIds.contains(userFriend.getId()));
System.out.println(alreadyFriended);

// Add search results to the model
model.addAttribute("friends", exactOutput); // Replace friends list with search results
model.addAttribute("user", user.get());
model.addAttribute("searchQuery", searchQuery); // Keep track of the search term
model.addAttribute("alreadyFriendRequested",alreadyFriended); // The list of people who have already been sent a friend request

return "friends"; // Return the same friends page with search results
}

@PostMapping("/friends/addfriend")
public String addFriend(@RequestParam("newFriendId") Long recipientId){
return "redirect:/manageFriends?recipientId="+recipientId;
}

@PostMapping("/friends/deletefriendrequest")
public String deleteFriendRequest(@RequestParam("friendRequestFriendId") Long friendId,@RequestParam("userId") Long userId){
ArrayList<FriendRequest> requestsToDelete = friendRequestRepository.findFriendRequestByFromUserIdAndToUserIdAndStatus(userId,friendId,"PENDING");
for(FriendRequest request:requestsToDelete){
friendRequestRepository.deleteById(request.getId());
}
return "redirect:/friends";
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
package com.makersacademy.acebook.controller;

import com.makersacademy.acebook.model.User;
import com.makersacademy.acebook.repository.UserRepository;
import org.springframework.security.core.Authentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.view.RedirectView;

import java.util.Optional;

@Controller
public class HomeController {
@RequestMapping(value = "/")
public RedirectView index() {
return new RedirectView("/posts");

@Autowired
UserRepository userRepository;

@ModelAttribute("user")
Optional<User> findUser(Authentication authentication) {
DefaultOidcUser principal = (DefaultOidcUser) authentication.getPrincipal();
String username = (String) principal.getAttributes().get("email");
return userRepository.findUserByUsername(username);
}

// Routes ------

@GetMapping(value = "/")
public String index(@ModelAttribute("user") Optional<User> user) {
return user.
// map(_user -> "redirect:/posts" + _user.getUsername())
map(_user -> "redirect:/posts")
.orElse("redirect:/users/newUser");
}
}
Loading