Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented reputationPoints flow. #643

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ protected void configure() {
skip(destination.getPassword());
skip(destination.getFollowers());
skip(destination.getFollowing());

}
});
modelMapper.addMappings(new PropertyMap<User, SelfProfileResponseDto>() {
@Override
protected void configure() {
skip(destination.getQuestions());
skip(destination.getAnswers());
map(source.getReputationPoints(), destination.getReputationPoints());
}
});
modelMapper.addMappings(new PropertyMap <UserProfileResponseDto,User>() {
Expand All @@ -50,6 +52,7 @@ protected void configure() {
@Override
protected void configure() {
skip(destination.getFollowedTags());
map(source.getReputationPoints(), destination.getReputationPoints());

}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public ResponseEntity<GenericApiResponse<SelfProfileResponseDto>> getUser() {
selfProfileResponseDto.setFollowedTags(
tagService.getFollowedTags(user.getId())
);
selfProfileResponseDto.setReputationPoints(userService.calculateReputation(user));
selfProfileResponseDto.setQuestionCount((long) questions.size());
selfProfileResponseDto.setQuestions(
questions);
Expand Down Expand Up @@ -81,6 +82,7 @@ public ResponseEntity<GenericApiResponse<UserProfileResponseDto>> getUserById(
if (user.isPresent()) {
UserProfileResponseDto userProfileResponseDto = modelMapper.map(user.get(),
UserProfileResponseDto.class);
userProfileResponseDto.setReputationPoints(userService.calculateReputation(user.get()));
userProfileResponseDto.setSelfFollowing(userService.selfFollowing(user.get()));
userProfileResponseDto.setFollowedTags(
tagService.getFollowedTags(user.get().getId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class SelfProfileResponseDto {
private Long answerCount;
private int followersCount;
private int followingCount;
private int reputationPoints;
private Long reputationPoints;
private Long questionCount;
private ExperienceLevel experienceLevel;
private List<QuestionSummaryDto> questions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class UserProfileResponseDto {
private int followersCount;
private int followingCount;
private boolean selfFollowing;
private int reputationPoints;
private Long reputationPoints;
private ExperienceLevel experienceLevel;
@Builder.Default
private List<SelfProfileResponseDto.FollowedTags> followedTags = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public Long getDownvoteCount() {
return votes.stream().filter(vote -> !vote.isUpvote()).count();
}

public Long getVoteDifference() {
return Math.max(this.getUpvoteCount() - this.getDownvoteCount(), 0);
}

public Integer getRating() {
return (int)(this.getUpvoteCount() - this.getDownvoteCount());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,8 @@ public Long getUpvoteCount() {
public Long getDownvoteCount() {
return votes.stream().filter(vote -> !vote.isUpvote()).count();
}
public Long getVoteDifference() {
return Math.max(getUpvoteCount() - getDownvoteCount(), 0);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.group1.programminglanguagesforum.Entities.DifficultyLevel;
import com.group1.programminglanguagesforum.Entities.Question;
import com.group1.programminglanguagesforum.Entities.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -10,6 +11,7 @@
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface QuestionRepository extends JpaRepository<Question, Long> {
Expand All @@ -28,6 +30,11 @@ Page<Question> searchQuestions(
@Param("tagIds") List<Long> tagIds,
@Param("difficulty") DifficultyLevel difficulty,
Pageable pageable);

@Query("SELECT q FROM Question q WHERE q.askedBy.id = :author")
List<Question> findByAuthorId(@Param("author") Long authorId);

@Query("SELECT q.askedBy FROM Question q WHERE q.id = :id")
Optional<User> findQuestionOwner(@Param("id") Long questionId);
}

Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.group1.programminglanguagesforum.Services;

import com.group1.programminglanguagesforum.DTOs.Requests.UserProfileUpdateRequestDto;
import com.group1.programminglanguagesforum.Entities.Answer;
import com.group1.programminglanguagesforum.Entities.Question;
import com.group1.programminglanguagesforum.Entities.User;
import com.group1.programminglanguagesforum.Exceptions.UnauthorizedAccessException;
import com.group1.programminglanguagesforum.Exceptions.UserNotFoundException;
import com.group1.programminglanguagesforum.Repositories.AnswerRepository;
import com.group1.programminglanguagesforum.Repositories.QuestionRepository;
import com.group1.programminglanguagesforum.Repositories.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
Expand All @@ -20,11 +24,24 @@
public class UserService {
private final UserRepository userRepository;
private final UserContextService userContextService;
private final AnswerRepository answerRepository;
private final QuestionRepository questionRepository;

public Optional<User> getUserById(Long id) {
return userRepository.findById(id);
}

public Long calculateReputation(User user) {
List<Question> questions = questionRepository.findByAuthorId(user.getId());
long questionCount = questions.size();
Long questionVoteDifference = questions.stream().map(Question::getVoteDifference).reduce(0L, Long::sum);
List< Answer> answers= answerRepository.findByAnsweredBy(user.getId());
long answerCount = answers.size();
Long answerVoteDifference = answers.stream().map(Answer::getVoteDifference).reduce(0L, Long::sum);
return (questionCount * 10 + answerCount * 15 + questionVoteDifference * 25 + answerVoteDifference * 30);

}

public User updateUser(User user, UserProfileUpdateRequestDto userProfileUpdateRequestDto)
throws UserNotFoundException {
Optional<User> userOptional = userRepository.findById(user.getId());
Expand Down
Loading