Skip to content

Commit

Permalink
chore: fix coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
kiki0518 authored and YukinaMochizuki committed Dec 2, 2024
1 parent 9d5f625 commit 10e452e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
public interface ViewpointReactionRepository extends JpaRepository<ViewpointReactionEntity, ViewpointReactionId> {
Optional<ViewpointReactionEntity> findById(ViewpointReactionId id);

// @Modifying
//@Query("UPDATE ViewpointEntity v SET v.likeCount = v.likeCount - 1 WHERE v.id = :viewpointId AND v.likeCount > 0")
//void decrementLikeCount(@Param("viewpointId") Long viewpointId);
@Query("SELECT v.reaction FROM ViewpointReactionEntity v WHERE v.id = :id")
Optional<Reaction> findReactionById(ViewpointReactionId id);

// need to query who liked the viewpoint

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,30 +58,22 @@ public void deleteViewpoint(UUID id) {
@Transactional
public ViewpointReactionEntity reactToViewpoint(String email, UUID viewpointId, String reaction) {

// check if the user and viewpoint exist in the database
Long userId = userRepository.findIdByEmail(email).orElseThrow(
() -> new EntityNotFoundException("User", "email", email)).getId();;

ViewpointReactionId viewpointReactionId = new ViewpointReactionId(userId, viewpointId);

// check if the user and viewpoint exist in the database
UserEntity userEntity = userRepository.findById(userId).orElseThrow(
() -> new EntityNotFoundException("User", "id", valueOf(userId)));
ViewpointEntity viewpointEntity = viewpointRepository.findViewpointEntityById(viewpointId).orElseThrow(
() -> new EntityNotFoundException("Viewpoint", "id", viewpointId.toString()));

// check if the viewpointReactionEntity exists in the database, if not means the user has not reacted to the viewpoint
ViewpointReactionEntity viewpointReactionEntity = viewpointReactionRepository.findById(viewpointReactionId).orElseGet(() -> {
ViewpointReactionEntity newViewpointReactionEntity = new ViewpointReactionEntity();
// create a new ViewpointReactionEntity with the given userId and viewpointId
newViewpointReactionEntity.setId(viewpointReactionId);
newViewpointReactionEntity.setReaction(Reaction.NONE);
return newViewpointReactionEntity;
Reaction previousReaction = viewpointReactionRepository.findReactionById(viewpointReactionId).orElseGet(() -> {
return Reaction.NONE;
});

// test
// return viewpointReactionEntity;

Reaction previousReaction = viewpointReactionEntity.getReaction();
switch (previousReaction) {
case NONE:
break;
Expand All @@ -98,7 +90,11 @@ public ViewpointReactionEntity reactToViewpoint(String email, UUID viewpointId,
throw new IllegalArgumentException("Invalid reaction: " + previousReaction);
}

ViewpointReactionEntity viewpointReactionEntity = new ViewpointReactionEntity();
// create a new ViewpointReactionEntity with the given userId and viewpointId
viewpointReactionEntity.setId(viewpointReactionId);
viewpointReactionEntity.setReaction(Reaction.valueOf(reaction));

switch (Reaction.valueOf(reaction)) {
case NONE: // delete the viewpointReactionEntity if the reaction is NONE
viewpointReactionRepository.delete(viewpointReactionEntity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public class ViewpointResponse {
private UUID authorId;
private String authorName;
private URI authorAvatar;
private ViewpointReactionEntity userReaction;
private Integer likeCount;
private Integer reasonableCount;
private Integer dislikeCount;
Expand Down

0 comments on commit 10e452e

Please sign in to comment.