-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from CodeURJC-DAW-2022-23/feat/upVote-downVote…
…-new Feat/up vote down vote new
- Loading branch information
Showing
8 changed files
with
178 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
back/src/main/java/net/daw/alist/controllers/VotesController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
back/src/main/java/net/daw/alist/services/VotesService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters