Skip to content

Commit 24392a5

Browse files
authored
Merge pull request #36 from CodeURJC-DAW-2022-23/feat/upVote-downVote-new
Feat/up vote down vote new
2 parents d8825fc + 9aff954 commit 24392a5

File tree

8 files changed

+178
-9
lines changed

8 files changed

+178
-9
lines changed

back/src/main/java/net/daw/alist/controllers/PostController.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import net.daw.alist.models.User;
55
import net.daw.alist.services.PostService;
66
import net.daw.alist.services.UserService;
7+
import net.daw.alist.services.VotesService;
78
import net.daw.alist.utils.Utils;
89
import org.springframework.beans.factory.annotation.Autowired;
910
import org.springframework.stereotype.Controller;
@@ -22,6 +23,9 @@ public class PostController {
2223
@Autowired
2324
private PostService postService;
2425

26+
@Autowired
27+
private VotesService votesService;
28+
2529
@GetMapping({"/","/followed-users/"})
2630
public String home(Model model) {
2731
Utils utils = new Utils(userService, postService);
@@ -30,7 +34,7 @@ public String home(Model model) {
3034
}
3135

3236
@GetMapping("/posts")
33-
public String getNewPosts(Model model, @RequestParam int page) {
37+
public String getNewPosts(Model model, @RequestParam int page, Authentication authentication) {
3438
Page<Post> newPage = postService.getPosts(page);
3539
model.addAttribute("posts", newPage);
3640
return "post";

back/src/main/java/net/daw/alist/controllers/TopListController.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import net.daw.alist.models.User;
66
import net.daw.alist.services.PostService;
77
import net.daw.alist.services.UserService;
8+
import net.daw.alist.services.VotesService;
89
import net.daw.alist.utils.Utils;
910
import org.springframework.beans.factory.annotation.Autowired;
1011
import org.springframework.security.core.Authentication;
@@ -26,6 +27,9 @@ public class TopListController {
2627
@Autowired
2728
private PostService postService;
2829

30+
@Autowired
31+
VotesService votesService;
32+
2933
private User userProfile;
3034
private Post post;
3135

@@ -43,11 +47,11 @@ public String topList(Model model, Authentication authentication, @PathVariable
4347
}
4448

4549
post = postService.findByID(id).orElseThrow();
46-
4750
model.addAttribute("id", id);
4851
model.addAttribute("posts", post);
4952
model.addAttribute("title", post.getTitle());
5053

54+
votesService.isVoted(model,post, authentication);
5155
List<Comment> comments = post.getComments();
5256
model.addAttribute("comments", comments);
5357

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package net.daw.alist.controllers;
2+
3+
import net.daw.alist.models.User;
4+
import net.daw.alist.services.UserService;
5+
import net.daw.alist.services.VotesService;
6+
import org.springframework.security.core.Authentication;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.stereotype.Controller;
9+
import org.springframework.web.bind.annotation.*;
10+
11+
@Controller
12+
public class VotesController {
13+
14+
@Autowired
15+
VotesService votesService;
16+
@Autowired
17+
UserService userService;
18+
19+
@GetMapping ("/post/{id}/upvote-action")
20+
public String actionUpVote(Authentication authentication, @PathVariable Long id){
21+
Long postID = id;
22+
if(!(authentication == null)) {
23+
User userSession = (User) userService.loadUserByUsername(((User) authentication
24+
.getPrincipal())
25+
.getUsername());
26+
votesService.actionUpVote(postID, userSession);
27+
}
28+
else
29+
return "redirect:/sign-in";
30+
return "redirect:/top-list/{id}";
31+
}
32+
33+
@GetMapping("/post/{id}/downvote-action")
34+
public String actionDownVote(Authentication authentication, @PathVariable Long id){
35+
Long postID = id;
36+
if(!(authentication == null)) {
37+
User userSession = (User) userService.loadUserByUsername(((User) authentication
38+
.getPrincipal())
39+
.getUsername());
40+
votesService.actionDownVote(postID, userSession);
41+
}
42+
else
43+
return "redirect:/sign-in";
44+
return "redirect:/top-list/{id}";
45+
}
46+
47+
48+
}

back/src/main/java/net/daw/alist/models/Post.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,19 @@ public class Post {
2121
private Date date;
2222
private String title;
2323

24-
@OneToMany
24+
@ManyToMany
2525
private Set<User> upVotes = new HashSet<>();
2626

27-
@OneToMany
27+
@ManyToMany
2828

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

31+
private int numUpvotes = upVotes.size();
32+
33+
private int numDownvotes = downVotes.size();
34+
35+
private int votes;
36+
3137
@ManyToMany
3238
private List<Topic> topics = new ArrayList<>();
3339

@@ -52,6 +58,7 @@ public Post(
5258
this.title = title;
5359
this.items = items;
5460
this.topics = topics;
61+
updateVotes();
5562
author.addPost(this);
5663
}
5764

@@ -104,6 +111,11 @@ public Set<User> getUpVotes() {
104111
public Set<User> getDownVotes() {
105112
return downVotes;
106113
}
114+
public void updateVotes(){
115+
numDownvotes=downVotes.size();
116+
numUpvotes=upVotes.size();
117+
votes = numUpvotes-numDownvotes;
118+
}
107119

108120
public List<Topic> getTopics() {
109121
return topics;
@@ -120,5 +132,12 @@ public List<Comment> getComments() {
120132
public void addComment(Comment comment){
121133
this.comments.add(comment);
122134
}
135+
public void addUpVote(User user) { this.upVotes.add(user);}
136+
public void removeUpVote(User user) {this.upVotes.remove(user);}
137+
138+
public void addDownVote(User user){ this.downVotes.add(user); }
139+
140+
public void removeDownVote(User user) { this.downVotes.remove(user); }
141+
123142

124143
}

back/src/main/java/net/daw/alist/services/DatabaseInitializer.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,20 @@ public void init() throws IOException, SQLException {
150150
postItemRepository.save(cr74);
151151
postItemRepository.save(cr75);
152152

153-
Post firstPost = new Post(cr7Lover, "CR7 is the best", asList(sports, football), asList(cr71, cr72, cr73, cr74, cr75));
154153
Post secondPost = new Post(shanks, "Best animes", asList(anime, series), asList(myLittlePony, inazumaEleven, onePiece, fullMetalAlchemist, attackOnTitan));
154+
Post firstPost = new Post(cr7Lover, "CR7 is the best", asList(sports, football), asList(cr71, cr72, cr73, cr74, cr75));
155155
Post thirdPost = new Post(peepo, "Best LaLiga teams", asList(sports, football, laLiga), asList(getafe, rayoVayecano, betis, realMadrid, barcelona));
156156
Post fourthPost = new Post(manolo, "Best NBA players", asList(sports, basketball, nba), asList(lebronJames, kevinDurant, giannisAntetokounmpo, stephenCurry, antonyDavis));
157157

158+
firstPost.getUpVotes().add(peepo);
159+
firstPost.getDownVotes().add(cr7Lover);
160+
firstPost.getUpVotes().add(manolo);
161+
thirdPost.getUpVotes().add(peepo);
162+
thirdPost.getUpVotes().add(cr7Lover);
163+
thirdPost.getUpVotes().add(manolo);
164+
165+
firstPost.updateVotes();
166+
thirdPost.updateVotes();
158167
postRepository.save(firstPost);
159168
postRepository.save(secondPost);
160169
postRepository.save(thirdPost);

back/src/main/java/net/daw/alist/services/PostService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.springframework.beans.factory.annotation.Autowired;
77
import org.springframework.data.domain.Page;
88
import org.springframework.data.domain.PageRequest;
9+
import org.springframework.data.domain.Sort;
910
import org.springframework.stereotype.Service;
1011

1112
import java.util.List;
@@ -21,7 +22,7 @@ public class PostService {
2122
private final int pageSize = 2;
2223

2324
public Page<Post> getPosts(int pageNumber) {
24-
return postRepository.findAll(PageRequest.of(pageNumber, pageSize));
25+
return postRepository.findAll(PageRequest.of(pageNumber, pageSize,Sort.by("numUpvotes").descending()));
2526
}
2627

2728
public Page<Post> getUserPosts(int pageNumber, int user_id) {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package net.daw.alist.services;
2+
3+
4+
import lombok.AllArgsConstructor;
5+
import net.daw.alist.models.Post;
6+
import net.daw.alist.models.User;
7+
import net.daw.alist.repositories.PostRepository;
8+
import org.springframework.security.core.Authentication;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.stereotype.Service;
11+
import org.springframework.ui.Model;
12+
13+
import javax.transaction.Transactional;
14+
15+
@Service
16+
@AllArgsConstructor
17+
public class VotesService {
18+
@Autowired
19+
private final PostRepository postRepository;
20+
21+
@Autowired
22+
private UserService userService;
23+
24+
25+
@Transactional
26+
public void actionUpVote(Long postID, User user){
27+
Post post = postRepository.findById(postID).orElseThrow();
28+
if(post.getUpVotes().contains(user))
29+
post.removeUpVote(user);
30+
else{
31+
post.addUpVote(user);
32+
if (post.getDownVotes().contains(user))
33+
post.removeDownVote(user);
34+
}
35+
post.updateVotes();
36+
postRepository.save(post);
37+
}
38+
@Transactional
39+
public void actionDownVote(Long postID, User user){
40+
Post post = postRepository.findById(postID).orElseThrow();
41+
if (post.getDownVotes().contains(user))
42+
post.removeDownVote(user);
43+
else {
44+
post.addDownVote(user);
45+
if (post.getUpVotes().contains(user))
46+
post.removeUpVote(user);
47+
}
48+
post.updateVotes();
49+
postRepository.save(post);
50+
}
51+
52+
public void isVoted(Model model, Post post, Authentication authentication)
53+
{
54+
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
55+
.getPrincipal())
56+
.getUsername());
57+
model.addAttribute("upVoted",post.getUpVotes().contains(userProfile));
58+
model.addAttribute("downVoted",post.getDownVotes().contains(userProfile));
59+
}
60+
61+
62+
}

back/src/main/resources/templates/post.html

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,38 @@ <h1 class="topName">{{title}}</h1>
3131
<li class="ms-0 me-auto">
3232
{{description}}
3333
</li>
34-
<img src="{{id}}/image" alt="image" width="45" height="45" class="itemImage rounded-circle ms-auto me-2 mt-auto mb-auto">
34+
<img src="{{id}}/image" alt="image" width="45" height="45"
35+
class="itemImage rounded-circle ms-auto me-2 mt-auto mb-auto">
3536
</div>
3637
{{/items}}
3738
</ol>
3839
</div>
3940
</div>
4041
<div class="list-group-item d-flex z-0">
4142
<div class="w-auto m-auto">
42-
<img class="upvoteIcon" src="/images/upvoteIcon.svg" alt="" width="35" height="35">
43-
<img class="downvoteIcon" src="/images/downvoteIcon.svg" alt="" width="35" height="35">
43+
<h6 class="pe-1 d-inline">
44+
{{upVotes.size}}
45+
</h6>
46+
47+
<a class="btn btn-primary " type="button" href="/post/{{id}}/upvote-action">
48+
{{^upVoted}}
49+
<i class="fa-regular fa-circle-up fa-xl"></i>
50+
{{/upVoted}}
51+
{{#upVoted}}
52+
<i class="fa-solid fa-circle-up fa-xl"></i>
53+
{{/upVoted}}
54+
</a>
55+
<h6 class="pe-1 d-inline">
56+
{{downVotes.size}}
57+
</h6>
58+
<a class="btn btn-primary" type="button" href="/post/{{id}}/downvote-action">
59+
{{^downVoted}}
60+
<i class="fa-regular fa-circle-down fa-xl"></i>
61+
{{/downVoted}}
62+
{{#downVoted}}
63+
<i class="fa-solid fa-circle-down fa-xl"></i>
64+
{{/downVoted}}
65+
</a>
4466
</div>
4567
<img class="shareIcon m-auto" src="/images/shareIcon.svg" alt="" width="35" height="35">
4668
</div>

0 commit comments

Comments
 (0)