Skip to content

Commit

Permalink
feat: profile follow display
Browse files Browse the repository at this point in the history
Co-authored-by: Vicente <v.gonzalez.2020@alumnos.urjc.es>
  • Loading branch information
skuzow and Vicente1215 committed Mar 5, 2023
1 parent 3a3008b commit cd930a7
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public class ProfileController {
@Autowired
UserRepository userRepository;

private User user;
private User userSession;

@GetMapping("/user/{username}")
public String profile(
Model model,
Expand All @@ -30,12 +33,20 @@ public String profile(
) throws IOException {
Optional<User> optionalUser = userRepository.findByUsername(username);
if (optionalUser.isPresent()) {
user = optionalUser.get();
// TODO: guest
if (isLoggedUser(username, authentication)) {
model.addAttribute("ownProfile", true);
} else {
/* bad bc userSession = null
if (isFollowed()) {
model.addAttribute("followed", true);
} else {
model.addAttribute("followed", false);
}
*/
model.addAttribute("ownProfile", false);
}
User user = optionalUser.get();
model.addAttribute("user", user);
return "profile";
}
Expand All @@ -45,8 +56,12 @@ public String profile(

private boolean isLoggedUser(String username, Authentication authentication) {
if (authentication == null) return false;
User userSession = (User) authentication.getPrincipal();
userSession = (User) authentication.getPrincipal();
return Objects.equals(username, userSession.getUsername());
}

private boolean isFollowed() {
return userSession.getFollowing().contains(user);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.http.HttpResponse;
import java.sql.SQLException;
import java.util.Optional;

@AllArgsConstructor
@Controller
Expand All @@ -28,15 +26,14 @@ public String register() {
@PostMapping("/register")
public String registerPost(Model model, RegistrationRequest request) throws SQLException, IOException {
String result = registrationService.register(request);
if (result.equals("Success")){
if (result.equals("Success")) {
model.addAttribute("notVerified", true);
model.addAttribute("verifyMSG", "Please check your inbox to verify your email address");
return "/register";
} else{
} else {
model.addAttribute("error", true);
model.addAttribute("errorMSG", result);
return "/register";
}
return "/register";
}

@GetMapping("/registration/confirm")
Expand Down
30 changes: 23 additions & 7 deletions back/src/main/java/net/daw/alist/models/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ public void setLocked(boolean locked) {
private Blob image;
private String imagePath;

@OneToMany
private List<User> follows = new ArrayList<>();
@ManyToMany(fetch=FetchType.EAGER)
@JsonIgnore
private List<User> following = new ArrayList<>();

@OneToMany
@ManyToMany(mappedBy="following")
@JsonIgnore
private List<User> followers = new ArrayList<>();

@OneToMany(mappedBy = "author", cascade = CascadeType.ALL, orphanRemoval = true)
Expand Down Expand Up @@ -114,8 +116,8 @@ public void setImage(String imagePath) throws IOException, SQLException {
this.imagePath = imagePath.replace("static", "");
}

public void setFollows(List<User> follows) {
this.follows = follows;
public void setFollowing(List<User> following) {
this.following = following;
}

public void setFollowers(List<User> followers) {
Expand Down Expand Up @@ -164,8 +166,8 @@ public String getImagePath() {
return imagePath;
}

public List<User> getFollows() {
return follows;
public List<User> getFollowing() {
return following;
}

public List<User> getFollowers() {
Expand Down Expand Up @@ -215,4 +217,18 @@ public void addPost(Post post){
posts.add(post);
}

public void follow(User user) {
if (user != this && !following.contains(user)) {
following.add(user);
user.getFollowers().add(this);
}
}

public void unFollow(User user) {
if (user != this && following.contains(user)) {
following.remove(user);
user.getFollowers().remove(this);
}
}

}
12 changes: 12 additions & 0 deletions back/src/main/java/net/daw/alist/services/DatabaseInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ public void init() throws IOException, SQLException {
userRepository.save(cr7Lover);
userRepository.save(admin);

admin.follow(shanks);
admin.follow(peepo);
peepo.follow(shanks);
peepo.follow(admin);
shanks.follow(admin);
cr7Lover.follow(admin);

userRepository.save(peepo);
userRepository.save(shanks);
userRepository.save(cr7Lover);
userRepository.save(admin);

Topic sports = new Topic("Sports", "General topic about sports");
Topic football = new Topic("Football", "Topic about football teams, players, etc");
Topic basketball = new Topic("Basketball", "Topic about basketball teams, players, etc");
Expand Down
7 changes: 6 additions & 1 deletion back/src/main/resources/templates/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,18 @@
{{/ownProfile}}
{{^ownProfile}}
<div class="col">
{{#followed}}
<button class="btn btn-primary btn-round" type="button">Following</button>
{{/followed}}
{{^followed}}
<button class="btn btn-primary btn-round" type="button">Follow</button>
{{/followed}}
</div>
{{/ownProfile}}
</div>
<div class="row fs-5">
<div class="col">
{{user.follows.size}} <br><small>Following</small>
{{user.following.size}} <br><small>Following</small>
</div>
<div class="col">
{{user.followers.size}} <br><small>Followers</small>
Expand Down

0 comments on commit cd930a7

Please sign in to comment.