Skip to content

Commit

Permalink
Merge pull request #36 from CodeURJC-DAW-2022-23/feat/upVote-downVote…
Browse files Browse the repository at this point in the history
…-new

Feat/up vote down vote new
  • Loading branch information
Kr4ll authored Mar 13, 2023
2 parents d8825fc + 9aff954 commit 24392a5
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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<Post> newPage = postService.getPosts(page);
model.addAttribute("posts", newPage);
return "post";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +27,9 @@ public class TopListController {
@Autowired
private PostService postService;

@Autowired
VotesService votesService;

private User userProfile;
private Post post;

Expand All @@ -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<Comment> comments = post.getComments();
model.addAttribute("comments", comments);

Expand Down
48 changes: 48 additions & 0 deletions back/src/main/java/net/daw/alist/controllers/VotesController.java
Original file line number Diff line number Diff line change
@@ -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}";
}


}
23 changes: 21 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 @@ -21,13 +21,19 @@ public class Post {
private Date date;
private String title;

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

@OneToMany
@ManyToMany

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

private int numUpvotes = upVotes.size();

private int numDownvotes = downVotes.size();

private int votes;

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

Expand All @@ -52,6 +58,7 @@ public Post(
this.title = title;
this.items = items;
this.topics = topics;
updateVotes();
author.addPost(this);
}

Expand Down Expand Up @@ -104,6 +111,11 @@ public Set<User> getUpVotes() {
public Set<User> getDownVotes() {
return downVotes;
}
public void updateVotes(){
numDownvotes=downVotes.size();
numUpvotes=upVotes.size();
votes = numUpvotes-numDownvotes;
}

public List<Topic> getTopics() {
return topics;
Expand All @@ -120,5 +132,12 @@ public List<Comment> 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); }


}
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,20 @@ 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.getDownVotes().add(cr7Lover);
firstPost.getUpVotes().add(manolo);
thirdPost.getUpVotes().add(peepo);
thirdPost.getUpVotes().add(cr7Lover);
thirdPost.getUpVotes().add(manolo);

firstPost.updateVotes();
thirdPost.updateVotes();
postRepository.save(firstPost);
postRepository.save(secondPost);
postRepository.save(thirdPost);
Expand Down
3 changes: 2 additions & 1 deletion back/src/main/java/net/daw/alist/services/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,7 +22,7 @@ public class PostService {
private final int pageSize = 2;

public Page<Post> getPosts(int pageNumber) {
return postRepository.findAll(PageRequest.of(pageNumber, pageSize));
return postRepository.findAll(PageRequest.of(pageNumber, pageSize,Sort.by("numUpvotes").descending()));
}

public Page<Post> getUserPosts(int pageNumber, int user_id) {
Expand Down
62 changes: 62 additions & 0 deletions back/src/main/java/net/daw/alist/services/VotesService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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.security.core.Authentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.transaction.Transactional;

@Service
@AllArgsConstructor
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();
if(post.getUpVotes().contains(user))
post.removeUpVote(user);
else{
post.addUpVote(user);
if (post.getDownVotes().contains(user))
post.removeDownVote(user);
}
post.updateVotes();
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);
}
post.updateVotes();
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));
}


}
28 changes: 25 additions & 3 deletions back/src/main/resources/templates/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,38 @@ <h1 class="topName">{{title}}</h1>
<li class="ms-0 me-auto">
{{description}}
</li>
<img src="{{id}}/image" alt="image" width="45" height="45" class="itemImage rounded-circle ms-auto me-2 mt-auto mb-auto">
<img src="{{id}}/image" alt="image" width="45" height="45"
class="itemImage rounded-circle ms-auto me-2 mt-auto mb-auto">
</div>
{{/items}}
</ol>
</div>
</div>
<div class="list-group-item d-flex z-0">
<div class="w-auto m-auto">
<img class="upvoteIcon" src="/images/upvoteIcon.svg" alt="" width="35" height="35">
<img class="downvoteIcon" src="/images/downvoteIcon.svg" alt="" width="35" height="35">
<h6 class="pe-1 d-inline">
{{upVotes.size}}
</h6>

<a class="btn btn-primary " type="button" href="/post/{{id}}/upvote-action">
{{^upVoted}}
<i class="fa-regular fa-circle-up fa-xl"></i>
{{/upVoted}}
{{#upVoted}}
<i class="fa-solid fa-circle-up fa-xl"></i>
{{/upVoted}}
</a>
<h6 class="pe-1 d-inline">
{{downVotes.size}}
</h6>
<a class="btn btn-primary" type="button" href="/post/{{id}}/downvote-action">
{{^downVoted}}
<i class="fa-regular fa-circle-down fa-xl"></i>
{{/downVoted}}
{{#downVoted}}
<i class="fa-solid fa-circle-down fa-xl"></i>
{{/downVoted}}
</a>
</div>
<img class="shareIcon m-auto" src="/images/shareIcon.svg" alt="" width="35" height="35">
</div>
Expand Down

0 comments on commit 24392a5

Please sign in to comment.