From 8e59563d0ec6c4d9e092a126151906e43bf4d65a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Deken=C3=B3?= <102819481+Kr4ll@users.noreply.github.com> Date: Sun, 12 Mar 2023 20:20:40 +0100 Subject: [PATCH 1/7] feat:logic for votes implement by adding user to the vote sets --- .../alist/controllers/VotesController.java | 48 +++++++++++++++++++ .../main/java/net/daw/alist/models/Post.java | 11 ++++- .../net/daw/alist/services/VotesService.java | 45 +++++++++++++++++ back/src/main/resources/templates/post.html | 17 +++++-- 4 files changed, 116 insertions(+), 5 deletions(-) create mode 100644 back/src/main/java/net/daw/alist/controllers/VotesController.java create mode 100644 back/src/main/java/net/daw/alist/services/VotesService.java diff --git a/back/src/main/java/net/daw/alist/controllers/VotesController.java b/back/src/main/java/net/daw/alist/controllers/VotesController.java new file mode 100644 index 00000000..ee3ef55c --- /dev/null +++ b/back/src/main/java/net/daw/alist/controllers/VotesController.java @@ -0,0 +1,48 @@ +package net.daw.alist.controllers; + +import net.daw.alist.models.User; +import net.daw.alist.services.UserService; +import net.daw.alist.services.VotesService; +import org.springframework.security.core.Authentication; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +@Controller +public class VotesController { + + @Autowired + VotesService votesService; + @Autowired + UserService userService; + + @GetMapping ("/post/{id}/upvote-action") + public String actionUpVote(Authentication authentication, @PathVariable Long id){ + Long postID = id; + if(!(authentication == null)) { + User userSession = (User) userService.loadUserByUsername(((User) authentication + .getPrincipal()) + .getUsername()); + votesService.actionUpVote(postID, userSession); + } + else + return "redirect:/sign-in"; + return "redirect:/top-list/{id}"; + } + + @GetMapping("/post/{id}/downvote-action") + public String actionDownVote(Authentication authentication, @PathVariable Long id){ + Long postID = id; + if(!(authentication == null)) { + User userSession = (User) userService.loadUserByUsername(((User) authentication + .getPrincipal()) + .getUsername()); + votesService.actionDownVote(postID, userSession); + } + else + return "redirect:/sign-in"; + return "redirect:/top-list/{id}"; + } + + +} diff --git a/back/src/main/java/net/daw/alist/models/Post.java b/back/src/main/java/net/daw/alist/models/Post.java index e14ba628..cc8bb216 100644 --- a/back/src/main/java/net/daw/alist/models/Post.java +++ b/back/src/main/java/net/daw/alist/models/Post.java @@ -21,10 +21,10 @@ public class Post { private Date date; private String title; - @OneToMany + @ManyToMany private Set upVotes = new HashSet<>(); - @OneToMany + @ManyToMany private Set downVotes = new HashSet<>(); @@ -120,5 +120,12 @@ public List getComments() { public void addComment(Comment comment){ this.comments.add(comment); } + public void addUpVote(User user) { this.upVotes.add(user);} + public void removeUpVote(User user) {this.upVotes.remove(user);} + + public void addDownVote(User user){ this.downVotes.add(user); } + + public void removeDownVote(User user) { this.downVotes.remove(user); } + } diff --git a/back/src/main/java/net/daw/alist/services/VotesService.java b/back/src/main/java/net/daw/alist/services/VotesService.java new file mode 100644 index 00000000..5beb2bca --- /dev/null +++ b/back/src/main/java/net/daw/alist/services/VotesService.java @@ -0,0 +1,45 @@ +package net.daw.alist.services; + + +import lombok.AllArgsConstructor; +import net.daw.alist.models.Post; +import net.daw.alist.models.User; +import net.daw.alist.repositories.PostRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import javax.transaction.Transactional; + +@Service +@AllArgsConstructor +public class VotesService { + @Autowired + private final PostRepository postRepository; + + @Transactional + public void actionUpVote(Long postID, User user){ + Post post = postRepository.findById(postID).orElseThrow(); + if(post.getUpVotes().contains(user)) + post.removeUpVote(user); + else{ + post.addUpVote(user); + if (post.getDownVotes().contains(user)) + post.removeDownVote(user); + } + postRepository.save(post); + } + @Transactional + public void actionDownVote(Long postID, User user){ + Post post = postRepository.findById(postID).orElseThrow(); + if (post.getDownVotes().contains(user)) + post.removeDownVote(user); + else { + post.addDownVote(user); + if (post.getUpVotes().contains(user)) + post.removeUpVote(user); + } + postRepository.save(post); + } + + +} diff --git a/back/src/main/resources/templates/post.html b/back/src/main/resources/templates/post.html index b1126299..7c6c708a 100644 --- a/back/src/main/resources/templates/post.html +++ b/back/src/main/resources/templates/post.html @@ -31,7 +31,8 @@

{{title}}

  • {{description}}
  • - image + image {{/items}} @@ -39,8 +40,18 @@

    {{title}}

    - - +
    + {{upVotes.size}} +
    + + + +
    + {{downVotes.size}} +
    + + +
    From 37117586cae4ce0b43f761a08f2f30366c57f969 Mon Sep 17 00:00:00 2001 From: gutche Date: Sun, 12 Mar 2023 21:08:56 +0100 Subject: [PATCH 2/7] feat: sort home page feed by upvotes --- back/src/main/java/net/daw/alist/services/PostService.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/back/src/main/java/net/daw/alist/services/PostService.java b/back/src/main/java/net/daw/alist/services/PostService.java index ec0c22ba..9ff0d335 100644 --- a/back/src/main/java/net/daw/alist/services/PostService.java +++ b/back/src/main/java/net/daw/alist/services/PostService.java @@ -6,6 +6,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import java.util.List; @@ -21,7 +22,7 @@ public class PostService { private final int pageSize = 2; public Page getPosts(int pageNumber) { - return postRepository.findAll(PageRequest.of(pageNumber, pageSize)); + return postRepository.findAll(PageRequest.of(pageNumber, pageSize,Sort.by("numUpvotes").descending())); } public Page getUserPosts(int pageNumber, int user_id) { From 0bb095af2c6a6d339494be7e46e040a82868ca09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Deken=C3=B3?= <102819481+Kr4ll@users.noreply.github.com> Date: Sun, 12 Mar 2023 22:50:38 +0100 Subject: [PATCH 3/7] fix: Colored votes when voted --- .../daw/alist/controllers/PostController.java | 6 +++++- .../alist/controllers/TopListController.java | 6 +++++- .../main/java/net/daw/alist/models/Post.java | 8 ++++++++ .../alist/services/DatabaseInitializer.java | 7 +++++++ .../net/daw/alist/services/VotesService.java | 15 +++++++++++++++ back/src/main/resources/templates/post.html | 19 +++++++++++++++---- 6 files changed, 55 insertions(+), 6 deletions(-) diff --git a/back/src/main/java/net/daw/alist/controllers/PostController.java b/back/src/main/java/net/daw/alist/controllers/PostController.java index 1c8436e7..4fd9ac93 100644 --- a/back/src/main/java/net/daw/alist/controllers/PostController.java +++ b/back/src/main/java/net/daw/alist/controllers/PostController.java @@ -4,6 +4,7 @@ import net.daw.alist.models.User; import net.daw.alist.services.PostService; import net.daw.alist.services.UserService; +import net.daw.alist.services.VotesService; import net.daw.alist.utils.Utils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @@ -22,6 +23,9 @@ public class PostController { @Autowired private PostService postService; + @Autowired + private VotesService votesService; + @GetMapping({"/","/followed-users/"}) public String home(Model model) { Utils utils = new Utils(userService, postService); @@ -30,7 +34,7 @@ public String home(Model model) { } @GetMapping("/posts") - public String getNewPosts(Model model, @RequestParam int page) { + public String getNewPosts(Model model, @RequestParam int page, Authentication authentication) { Page newPage = postService.getPosts(page); model.addAttribute("posts", newPage); return "post"; diff --git a/back/src/main/java/net/daw/alist/controllers/TopListController.java b/back/src/main/java/net/daw/alist/controllers/TopListController.java index cc836dac..d52c1827 100644 --- a/back/src/main/java/net/daw/alist/controllers/TopListController.java +++ b/back/src/main/java/net/daw/alist/controllers/TopListController.java @@ -5,6 +5,7 @@ import net.daw.alist.models.User; import net.daw.alist.services.PostService; import net.daw.alist.services.UserService; +import net.daw.alist.services.VotesService; import net.daw.alist.utils.Utils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.Authentication; @@ -26,6 +27,9 @@ public class TopListController { @Autowired private PostService postService; + @Autowired + VotesService votesService; + private User userProfile; private Post post; @@ -43,11 +47,11 @@ public String topList(Model model, Authentication authentication, @PathVariable } post = postService.findByID(id).orElseThrow(); - model.addAttribute("id", id); model.addAttribute("posts", post); model.addAttribute("title", post.getTitle()); + votesService.isVoted(model,post, authentication); List comments = post.getComments(); model.addAttribute("comments", comments); diff --git a/back/src/main/java/net/daw/alist/models/Post.java b/back/src/main/java/net/daw/alist/models/Post.java index cc8bb216..79903bf8 100644 --- a/back/src/main/java/net/daw/alist/models/Post.java +++ b/back/src/main/java/net/daw/alist/models/Post.java @@ -28,6 +28,10 @@ public class Post { private Set downVotes = new HashSet<>(); + private int numUpvotes = upVotes.size(); + + private int numDownvotes = downVotes.size(); + @ManyToMany private List topics = new ArrayList<>(); @@ -104,6 +108,10 @@ public Set getUpVotes() { public Set getDownVotes() { return downVotes; } + public void updateVotes(){ + this.numDownvotes=downVotes.size(); + this.numUpvotes=upVotes.size(); + } public List getTopics() { return topics; diff --git a/back/src/main/java/net/daw/alist/services/DatabaseInitializer.java b/back/src/main/java/net/daw/alist/services/DatabaseInitializer.java index 44c0a8b1..d29a7945 100644 --- a/back/src/main/java/net/daw/alist/services/DatabaseInitializer.java +++ b/back/src/main/java/net/daw/alist/services/DatabaseInitializer.java @@ -155,6 +155,13 @@ public void init() throws IOException, SQLException { Post thirdPost = new Post(peepo, "Best LaLiga teams", asList(sports, football, laLiga), asList(getafe, rayoVayecano, betis, realMadrid, barcelona)); Post fourthPost = new Post(manolo, "Best NBA players", asList(sports, basketball, nba), asList(lebronJames, kevinDurant, giannisAntetokounmpo, stephenCurry, antonyDavis)); + firstPost.getUpVotes().add(peepo); + firstPost.getUpVotes().add(cr7Lover); + firstPost.getUpVotes().add(manolo); + thirdPost.getUpVotes().add(peepo); + thirdPost.getUpVotes().add(cr7Lover); + thirdPost.getUpVotes().add(manolo); + postRepository.save(firstPost); postRepository.save(secondPost); postRepository.save(thirdPost); diff --git a/back/src/main/java/net/daw/alist/services/VotesService.java b/back/src/main/java/net/daw/alist/services/VotesService.java index 5beb2bca..b560b37c 100644 --- a/back/src/main/java/net/daw/alist/services/VotesService.java +++ b/back/src/main/java/net/daw/alist/services/VotesService.java @@ -5,8 +5,10 @@ import net.daw.alist.models.Post; import net.daw.alist.models.User; import net.daw.alist.repositories.PostRepository; +import org.springframework.security.core.Authentication; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.ui.Model; import javax.transaction.Transactional; @@ -16,6 +18,10 @@ public class VotesService { @Autowired private final PostRepository postRepository; + @Autowired + private UserService userService; + + @Transactional public void actionUpVote(Long postID, User user){ Post post = postRepository.findById(postID).orElseThrow(); @@ -41,5 +47,14 @@ public void actionDownVote(Long postID, User user){ postRepository.save(post); } + public void isVoted(Model model, Post post, Authentication authentication) + { + User userProfile = (User) userService.loadUserByUsername(((User) authentication //authentication no devuelve el mismo objeto que hay en la base de datos. Por tanto no se puede usar la funcion contains + .getPrincipal()) + .getUsername()); + model.addAttribute("upVoted",post.getUpVotes().contains(userProfile)); + model.addAttribute("downVoted",post.getDownVotes().contains(userProfile)); + } + } diff --git a/back/src/main/resources/templates/post.html b/back/src/main/resources/templates/post.html index 7c6c708a..b0a81a88 100644 --- a/back/src/main/resources/templates/post.html +++ b/back/src/main/resources/templates/post.html @@ -43,14 +43,25 @@

    {{title}}

    {{upVotes.size}}
    - - + + + {{^upVoted}} + + {{/upVoted}} + {{#upVoted}} + + {{/upVoted}}
    {{downVotes.size}}
    - - + + {{^downVoted}} + + {{/downVoted}} + {{#downVoted}} + + {{/downVoted}} From 322240ecc620de0de2cd7bf3855381594def2ec9 Mon Sep 17 00:00:00 2001 From: franchescoURJC Date: Mon, 13 Mar 2023 09:26:53 +0100 Subject: [PATCH 4/7] feat: orders posts by upvotes-downvotes --- back/src/main/java/net/daw/alist/models/Post.java | 7 +++++-- .../java/net/daw/alist/services/DatabaseInitializer.java | 4 ++-- .../src/main/java/net/daw/alist/services/VotesService.java | 2 ++ 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/back/src/main/java/net/daw/alist/models/Post.java b/back/src/main/java/net/daw/alist/models/Post.java index 79903bf8..bbf4222a 100644 --- a/back/src/main/java/net/daw/alist/models/Post.java +++ b/back/src/main/java/net/daw/alist/models/Post.java @@ -32,6 +32,8 @@ public class Post { private int numDownvotes = downVotes.size(); + private int votes = numUpvotes-numDownvotes; + @ManyToMany private List topics = new ArrayList<>(); @@ -109,8 +111,9 @@ public Set getDownVotes() { return downVotes; } public void updateVotes(){ - this.numDownvotes=downVotes.size(); - this.numUpvotes=upVotes.size(); + numDownvotes=downVotes.size(); + numUpvotes=upVotes.size(); + votes = numUpvotes-numDownvotes; } public List getTopics() { diff --git a/back/src/main/java/net/daw/alist/services/DatabaseInitializer.java b/back/src/main/java/net/daw/alist/services/DatabaseInitializer.java index d29a7945..93c42bfb 100644 --- a/back/src/main/java/net/daw/alist/services/DatabaseInitializer.java +++ b/back/src/main/java/net/daw/alist/services/DatabaseInitializer.java @@ -150,13 +150,13 @@ public void init() throws IOException, SQLException { postItemRepository.save(cr74); postItemRepository.save(cr75); - Post firstPost = new Post(cr7Lover, "CR7 is the best", asList(sports, football), asList(cr71, cr72, cr73, cr74, cr75)); Post secondPost = new Post(shanks, "Best animes", asList(anime, series), asList(myLittlePony, inazumaEleven, onePiece, fullMetalAlchemist, attackOnTitan)); + Post firstPost = new Post(cr7Lover, "CR7 is the best", asList(sports, football), asList(cr71, cr72, cr73, cr74, cr75)); Post thirdPost = new Post(peepo, "Best LaLiga teams", asList(sports, football, laLiga), asList(getafe, rayoVayecano, betis, realMadrid, barcelona)); Post fourthPost = new Post(manolo, "Best NBA players", asList(sports, basketball, nba), asList(lebronJames, kevinDurant, giannisAntetokounmpo, stephenCurry, antonyDavis)); firstPost.getUpVotes().add(peepo); - firstPost.getUpVotes().add(cr7Lover); + firstPost.getDownVotes().add(cr7Lover); firstPost.getUpVotes().add(manolo); thirdPost.getUpVotes().add(peepo); thirdPost.getUpVotes().add(cr7Lover); diff --git a/back/src/main/java/net/daw/alist/services/VotesService.java b/back/src/main/java/net/daw/alist/services/VotesService.java index b560b37c..f51a66f4 100644 --- a/back/src/main/java/net/daw/alist/services/VotesService.java +++ b/back/src/main/java/net/daw/alist/services/VotesService.java @@ -32,6 +32,7 @@ public void actionUpVote(Long postID, User user){ if (post.getDownVotes().contains(user)) post.removeDownVote(user); } + post.updateVotes(); postRepository.save(post); } @Transactional @@ -44,6 +45,7 @@ public void actionDownVote(Long postID, User user){ if (post.getUpVotes().contains(user)) post.removeUpVote(user); } + post.updateVotes(); postRepository.save(post); } From fc6d07fd6ef45a35bd9a0122e8a598b8a8e0d1be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Deken=C3=B3?= <102819481+Kr4ll@users.noreply.github.com> Date: Mon, 13 Mar 2023 10:19:28 +0100 Subject: [PATCH 5/7] fix: initialization of variable votes --- back/src/main/java/net/daw/alist/models/Post.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/back/src/main/java/net/daw/alist/models/Post.java b/back/src/main/java/net/daw/alist/models/Post.java index bbf4222a..78d27627 100644 --- a/back/src/main/java/net/daw/alist/models/Post.java +++ b/back/src/main/java/net/daw/alist/models/Post.java @@ -32,7 +32,7 @@ public class Post { private int numDownvotes = downVotes.size(); - private int votes = numUpvotes-numDownvotes; + private int votes; @ManyToMany private List topics = new ArrayList<>(); @@ -58,6 +58,7 @@ public Post( this.title = title; this.items = items; this.topics = topics; + votes=numUpvotes-numDownvotes; author.addPost(this); } From 1fed692b8ae437d6cafb2a586e1b244398c8db1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Deken=C3=B3?= <102819481+Kr4ll@users.noreply.github.com> Date: Mon, 13 Mar 2023 11:01:18 +0100 Subject: [PATCH 6/7] fix: update votes at initialize --- back/src/main/java/net/daw/alist/models/Post.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/back/src/main/java/net/daw/alist/models/Post.java b/back/src/main/java/net/daw/alist/models/Post.java index 78d27627..49015a7f 100644 --- a/back/src/main/java/net/daw/alist/models/Post.java +++ b/back/src/main/java/net/daw/alist/models/Post.java @@ -58,7 +58,7 @@ public Post( this.title = title; this.items = items; this.topics = topics; - votes=numUpvotes-numDownvotes; + updateVotes(); author.addPost(this); } From 9aff9541e581b8d18c4da0966255fa1490889845 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Deken=C3=B3?= <102819481+Kr4ll@users.noreply.github.com> Date: Mon, 13 Mar 2023 11:04:20 +0100 Subject: [PATCH 7/7] fix:update votes at initializer --- .../main/java/net/daw/alist/services/DatabaseInitializer.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/back/src/main/java/net/daw/alist/services/DatabaseInitializer.java b/back/src/main/java/net/daw/alist/services/DatabaseInitializer.java index 93c42bfb..c199130d 100644 --- a/back/src/main/java/net/daw/alist/services/DatabaseInitializer.java +++ b/back/src/main/java/net/daw/alist/services/DatabaseInitializer.java @@ -162,6 +162,8 @@ public void init() throws IOException, SQLException { thirdPost.getUpVotes().add(cr7Lover); thirdPost.getUpVotes().add(manolo); + firstPost.updateVotes(); + thirdPost.updateVotes(); postRepository.save(firstPost); postRepository.save(secondPost); postRepository.save(thirdPost);