Skip to content

Commit

Permalink
Merge pull request #46 from CodeURJC-DAW-2022-23/feat/angular-home
Browse files Browse the repository at this point in the history
Feat: home and following page
  • Loading branch information
franchescoURJC authored Apr 21, 2023
2 parents fec747a + a0f2ba1 commit a5f57b4
Show file tree
Hide file tree
Showing 41 changed files with 1,342 additions and 73 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,4 @@ Webpack/*
*.iws
*.iml
*.ipr
.metals/metals.lock.db
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class AjaxRestController {
}),
@ApiResponse(responseCode = "404", description = "No new posts found", content = @Content)
})
@GetMapping("/")
@GetMapping("")
public Page<Post> getNewPosts(Authentication authentication, @RequestParam int page, @RequestParam Optional<Boolean> filter) {
boolean validPage = page <= (int) Math.ceil(postService.count()/2);
boolean filterPosts = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ public ResponseEntity<AuthResponse> logOut(HttpServletRequest request, HttpServl
})
@GetMapping("/logged-user")
public ResponseEntity<User> userLogged(HttpServletRequest request) {
String username = request.getUserPrincipal().getName();
return getUserResponseEntity(username, userService);
java.security.Principal principal = request.getUserPrincipal();
if (principal == null) return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
return getUserResponseEntity(principal.getName(), userService);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -113,41 +114,47 @@ public ResponseEntity<Post> deletePost(Authentication auth, @PathVariable long p
@Operation(summary = "Current user upvote a post")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "User found and upvoted the post", content = {
@Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = Post.class)))
@Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = ArrayList.class)))
}),
@ApiResponse(responseCode = "403", description = "Current user not logged in", content = @Content),
@ApiResponse(responseCode = "404", description = "Post not found", content = @Content)
})
@PutMapping("/{postId}/upvotes")
public ResponseEntity<Post> upvotePost(Authentication authentication, @PathVariable long postId) {
@PutMapping("/upvotes/{postId}")
public ResponseEntity<ArrayList> upvotePost(Authentication authentication, @PathVariable long postId) {
Optional<Post> optionalPost = postService.findByID(postId);
if(optionalPost.isPresent()) {
User userSession = (User) userService.loadUserByUsername(((User) authentication
.getPrincipal())
.getUsername());
votesService.actionUpVote(postId, userSession);
return new ResponseEntity<>(optionalPost.get(), HttpStatus.OK);
ArrayList<Integer> upvotesAndDownvotes = new ArrayList<>();
upvotesAndDownvotes.add(optionalPost.get().getNumUpvotes());
upvotesAndDownvotes.add(optionalPost.get().getNumDownvotes());
return new ResponseEntity<>(upvotesAndDownvotes, HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

@Operation(summary = "Current user downvote a post")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "User found and downvoted the post", content = {
@Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = Post.class)))
@Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = ArrayList.class)))
}),
@ApiResponse(responseCode = "403", description = "Current user not logged in", content = @Content),
@ApiResponse(responseCode = "404", description = "Post not found", content = @Content)
})
@PutMapping("/{postId}/downvotes")
public ResponseEntity<Post> downvotePost(Authentication authentication, @PathVariable long postId) {
@PutMapping("/downvotes/{postId}")
public ResponseEntity<ArrayList> downvotePost(Authentication authentication, @PathVariable long postId) {
Optional<Post> optionalPost = postService.findByID(postId);
if (optionalPost.isPresent()) {
User userSession = (User) userService.loadUserByUsername(((User) authentication
.getPrincipal())
.getUsername());
votesService.actionDownVote(postId, userSession);
return new ResponseEntity<>(optionalPost.get(), HttpStatus.OK);
ArrayList<Integer> upvotesAndDownvotes = new ArrayList<>();
upvotesAndDownvotes.add(optionalPost.get().getNumUpvotes());
upvotesAndDownvotes.add(optionalPost.get().getNumDownvotes());
return new ResponseEntity<>(upvotesAndDownvotes, HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import net.daw.alist.models.User;
import net.daw.alist.services.UserService;
import net.daw.alist.utils.Utils;
Expand All @@ -14,6 +17,9 @@
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
Expand All @@ -38,6 +44,17 @@ public ResponseEntity<User> getUser(@PathVariable String username) {
return getUserResponseEntity(username, userService);
}

public static ResponseEntity<User> getUserResponseEntity(String username, UserService userService) {
Optional<User> userPrincipal = userService.findByUsername(username);
if (userPrincipal.isPresent()) {
User user = userPrincipal.get();
user.setFollowingCount(user.getFollowing().size());
user.setFollowersCount(user.getFollowers().size());
return new ResponseEntity<>(user, HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

@Operation(summary = "Edit user by id")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "User found and banned/unbanned state changed", content = {
Expand Down Expand Up @@ -85,11 +102,11 @@ public ResponseEntity<User> userOperation(
@ApiResponse(responseCode = "403", description = "Current user not logged in", content = @Content),
@ApiResponse(responseCode = "404", description = "User not found", content = @Content)
})
@PutMapping("/followers/{username}")
public ResponseEntity<User> follow(Authentication authentication, @PathVariable String username){
Optional<User> optionalUser = userService.findByUsername(username);
@PutMapping("/{username}/follow")
public ResponseEntity<User> follow(Authentication authentication, @PathVariable String username) {
Optional<User> optionalUser = userService.findByUsername(username);
User userSession = (User) authentication.getPrincipal();
userSession = userService.findByID(userSession.getId()).orElseThrow();
userSession = userService.findByID(userSession.getId()).orElseThrow();
if (optionalUser.isPresent()) {
User userProfile = optionalUser.get();
if (userProfile.getFollowers().contains(userSession))
Expand All @@ -102,13 +119,81 @@ public ResponseEntity<User> follow(Authentication authentication, @PathVariable
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

public static ResponseEntity<User> getUserResponseEntity(String username, UserService userService) {
Optional<User> userPrincipal = userService.findByUsername(username);
if (userPrincipal.isPresent()) {
User user = userPrincipal.get();
return new ResponseEntity<>(user, HttpStatus.OK);
@Operation(summary = "Profile user following")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "User found", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = Follow.class))
}),
@ApiResponse(responseCode = "404", description = "User not found", content = @Content),
@ApiResponse(responseCode = "400", description = "User logged in but not found", content = @Content)
})
@GetMapping("{username}/following")
public ResponseEntity<Follow> following(HttpServletRequest request, @PathVariable String username) {
return checkFollowingFollow(request, username, true);
}

@Operation(summary = "Profile user followers")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "User found", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = Follow.class))
}),
@ApiResponse(responseCode = "404", description = "User not found", content = @Content),
@ApiResponse(responseCode = "400", description = "User logged in but not found", content = @Content)
})
@GetMapping("{username}/followers")
public ResponseEntity<Follow> followers(HttpServletRequest request, @PathVariable String username) {
return checkFollowingFollow(request, username, false);
}

private ResponseEntity<Follow> checkFollowingFollow(
HttpServletRequest request,
String username,
boolean following
) {
Optional<User> optionalUserProfile = userService.findByUsername(username);
if (optionalUserProfile.isPresent()) {
User userProfile = optionalUserProfile.get();
List<User> followUsers;
if (following) followUsers = userProfile.getFollowing();
else followUsers = userProfile.getFollowers();
List<FollowUser> followUserFollows = new ArrayList<>();
boolean userLoggedIn = request.getUserPrincipal() != null;
if (userLoggedIn) {
String userSessionUsername = request.getUserPrincipal().getName();
Optional<User> optionalUserSession = userService.findByUsername(userSessionUsername);
if (optionalUserSession.isPresent()) {
User userSession = optionalUserSession.get();
for (User user: followUsers) {
FollowUser followUser = new FollowUser(user.getUsername(), userSession.getFollowing().contains(user));
followUserFollows.add(followUser);
}
} else new ResponseEntity<>(HttpStatus.BAD_REQUEST);
} else {
for (User user: followUsers) {
FollowUser followUser = new FollowUser(user.getUsername(), false);
followUserFollows.add(followUser);
}
}
Follow follow = new Follow(followUsers.size(), followUserFollows);
return new ResponseEntity<>(follow, HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

@AllArgsConstructor
@Getter
@EqualsAndHashCode
private static class Follow {
private final int count;
private final List<FollowUser> users;
}

@AllArgsConstructor
@Getter
@EqualsAndHashCode
private static class FollowUser {
private final String username;
private final boolean follow;
}

}
49 changes: 47 additions & 2 deletions back/src/main/java/net/daw/alist/models/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,86 @@
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

import java.util.*;
import java.util.stream.Collectors;

import javax.persistence.*;

@Entity
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@Id")
public class Post {

public Long getId() {
return id;
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@JsonIgnore
@ManyToOne
private User author;
private String authorName;

public String getAuthorName() {
return authorName = author.getUsername();
}

private Date date;
private String title;

@JsonIgnore
@ManyToMany
private Set<User> upVotes = new HashSet<>();

public Set<String> getUpVotesUsernames() {
return upVotesUsernames;
}

public Set<String> getDownVotesUsernames() {
return downVotesUsernames;
}

@ElementCollection
private Set<String> upVotesUsernames = new HashSet<>();

@JsonIgnore
@ManyToMany

private Set<User> downVotes = new HashSet<>();

@ElementCollection
private Set<String> downVotesUsernames = new HashSet<>();

public int getNumUpvotes() {
return numUpvotes;
}

public int getNumDownvotes() {
return numDownvotes;
}

private int numUpvotes = upVotes.size();

private int numDownvotes = downVotes.size();

private int votes;

@JsonIgnore
@ManyToMany
private List<Topic> topics = new ArrayList<>();


public List<String> getTopicNames() {
return topicNames;
}

@ElementCollection
private List<String> topicNames = new ArrayList<>();

@OneToMany (orphanRemoval = true)
private List<PostItem> items = new ArrayList<>();


@JsonIgnore
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private List<Comment> comments = new ArrayList<>();

Expand All @@ -58,9 +99,11 @@ public Post(
) {
this.date = new Date();
this.author = author;
authorName = author.getUsername();
this.title = title;
this.items = items;
this.topics = topics;
topicNames = topics.stream().map( topic -> topic.getName()).collect(Collectors.toList());
updateVotes();
author.addPost(this);
}
Expand Down Expand Up @@ -118,6 +161,8 @@ public void updateVotes(){
numDownvotes=downVotes.size();
numUpvotes=upVotes.size();
votes = numUpvotes-numDownvotes;
downVotesUsernames = downVotes.stream().map( downvote -> downvote.getUsername()).collect(Collectors.toSet());
upVotesUsernames = upVotes.stream().map( upvote -> upvote.getUsername()).collect(Collectors.toSet());
}

public List<Topic> getTopics() {
Expand Down
6 changes: 5 additions & 1 deletion back/src/main/java/net/daw/alist/models/PostItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@Id")
public class PostItem {

@Id
public Long getId() {
return id;
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

Expand Down
Loading

0 comments on commit a5f57b4

Please sign in to comment.