Skip to content

Commit

Permalink
Merge pull request #34 from AcebookRedPanda/add_friends
Browse files Browse the repository at this point in the history
add-friends list
  • Loading branch information
Umi007 authored Jun 14, 2024
2 parents 14d8e17 + 0769547 commit 3dc1716
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,21 @@


import com.makersacademy.acebook.model.Friend;
import com.makersacademy.acebook.model.Post;
import com.makersacademy.acebook.model.User;
import com.makersacademy.acebook.repository.FriendRepository;
import com.makersacademy.acebook.repository.UserRepository;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
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.springframework.web.servlet.view.RedirectView;

import java.security.Principal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Optional;


@Controller
Expand All @@ -33,50 +28,45 @@ public class FriendsController {
@Autowired
UserRepository userRepository;


// Endpoint to list friends
// @GetMapping("/friends")
// public String listFriends(Model model, Principal principal) {
//
//
// User currentUser = userRepository.findByUsername(principal.getName());
// List<Friend> friends = friendRepository.findByUser(currentUser);
//// model.addAttribute("currentUser", currentUser);
// model.addAttribute("friends", currentUser.getFriends());
// return "users/friendList"; // Returns the friendlist.html view
// }

@GetMapping("/friends")
public String listFriends(Model model, Principal principal) {
User currentUser = userRepository.findByUsername(principal.getName());
List<User> friends = new ArrayList<>(currentUser.getFriends()); // Retrieve friends from the current user
model.addAttribute("currentUser", currentUser);
model.addAttribute("friends", friends);
return "users/friendList"; // Returns the friendList.html view
public String showFriends(Model model) {
User currentUser = getCurrentUser();
if (currentUser != null) {
List<User> friends = friendRepository.findFriendsByUser(currentUser);
model.addAttribute("friends", friends);
}
return "users/friendList";
}


// Method to add friends
@PostMapping("/addfriend")
public RedirectView create(@RequestParam String friendUsername, @AuthenticationPrincipal Principal principal, RedirectAttributes redirectAttributes) {

User currentUser = userRepository.findByUsername(principal.getName());
User friendUser = userRepository.findByUsername(friendUsername);
if (friendUser != null && !currentUser.equals(friendUser)) {
Friend friend = new Friend();
friend.setUser(currentUser);
friend.setFriend(friendUser);
// friend.setCreatedAt(new Date());
friendRepository.save(friend);
redirectAttributes.addFlashAttribute("message", "Friend added successfully.");
} else {
redirectAttributes.addFlashAttribute("error", "Invalid friend username.");
@PostMapping("/friends/add")
public RedirectView addFriend(@RequestParam Long friendId) {
User currentUser = getCurrentUser();
User friend = userRepository.findById(friendId).orElse(null);

if (friend != null && !currentUser.equals(friend)) {
currentUser.getFriends().add(friend);
friend.getFriends().add(currentUser);
userRepository.save(currentUser);
userRepository.save(friend);
}

return new RedirectView("/friends");
}



private User getCurrentUser() {
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String username;
if (principal instanceof UserDetails) {
username = ((UserDetails) principal).getUsername();
} else {
username = principal.toString();
}
return userRepository.findByUsername(username);
}

// Endpoint to list all users
@GetMapping("/all-users")
public String listAllUsers(Model model, Principal principal) {
Expand All @@ -86,10 +76,4 @@ public String listAllUsers(Model model, Principal principal) {
model.addAttribute("allUsers", allUsers);
return "users/all-users"; // Returns the all-users.html view
}






}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,14 @@ private User getCurrentUser() {
return userRepository.findByUsername(username);
}

// @GetMapping("/all")
// public String getAllUsers(@RequestParam Long currentUserId, Model model) {
// List<User> users = (List<User>) userRepository.findAll();
//
// // Fetch friends of the current user
// Arrays friendEntities = friendRepository.findByUserId(currentUserId);
// Set<Long> friendIds = friendEntities.stream()
// .map(friend -> friend.getFriend().getId())
// .collect(Collectors.toSet());
//
// // Add attributes to the model
// model.addAttribute("users", users);
// model.addAttribute("friendIds", friendIds);
// model.addAttribute("currentUserId", currentUserId);
//
// return "all-users";

// @GetMapping("/friends")
// public String showFriends(Model model) {
// User currentUser = getCurrentUser();
// if (currentUser != null) {
// model.addAttribute("friends", currentUser.getFriends());
// }
// return "users/friendList";
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import com.makersacademy.acebook.model.Friend;
import com.makersacademy.acebook.model.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface FriendRepository extends CrudRepository<Friend, Long>{

List<Friend> findByUser(User currentUser);
List<Friend> findByUser(User user);

List<Friend> findByFriend(User friend);
@Query("SELECT f.friend FROM Friend f WHERE f.user = :user")
List<User> findFriendsByUser(User user);
}
4 changes: 2 additions & 2 deletions src/main/resources/templates/users/all-users.html
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ <h1>All Users</h1>
<tr th:each="user : ${allUsers}">
<td th:text="${user.username}"></td>
<td>
<form th:action="@{users/friendList}" method="post">
<input type="hidden" name="friendUsername" th:value="${user.username}"/>
<form th:action="@{/friends/add}" method="post">
<input type="hidden" name="friendId" th:value="${user.id}"/>
<button type="submit">Add Friend</button>
</form>
</td>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/users/friendList.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
<h1>My Friends</h1>
<div id="friends-list">
<ul>
<li th:each="friend : ${friends}" th:text="${friend.username}"></li>
<li th:each="friend : ${friends}" th:text="${friend.username}">Friend Username</li>
</ul>
</div>
</div>
Expand Down

0 comments on commit 3dc1716

Please sign in to comment.