Skip to content

Commit

Permalink
Merge pull request #69 from educ-ai-org/feat/return-answer-status
Browse files Browse the repository at this point in the history
add correct percentage
  • Loading branch information
luuh-oliveira authored Jun 9, 2024
2 parents 59b7b67 + b1f6416 commit a56cd7e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
public class UserAnswerStatusDTO {
private UserDTO user;
private boolean hasAnswered;
private Double correctPercentage;
}
6 changes: 6 additions & 0 deletions src/main/java/api/educai/repositories/AnswerRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.data.mongodb.repository.MongoRepository;

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

public interface AnswerRepository extends MongoRepository <Answer, Long> {

Expand All @@ -13,4 +14,9 @@ public interface AnswerRepository extends MongoRepository <Answer, Long> {
boolean existsAnswerByUserIdAndClassworkId(ObjectId userId, ObjectId classworkId);

List<Answer> findAllByClassworkId(ObjectId classworkId);

Optional<Answer> findByUserIdAndClassworkId(ObjectId userId, ObjectId classworkId);

Double findCorrectPercentageByUserIdAndClassworkId(ObjectId id, ObjectId classworkId);

}
26 changes: 16 additions & 10 deletions src/main/java/api/educai/services/ClassworkService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
Expand Down Expand Up @@ -94,7 +95,7 @@ public void addAnswer(Answer answer, ObjectId userId, ObjectId classworkId) {
public Classwork getClassworkById(ObjectId id) {
Classwork classwork = classworkRepository.findById(id);

if(classwork == null) {
if (classwork == null) {
throw new ResponseStatusException(HttpStatusCode.valueOf(404), "Classwork not found!");
}

Expand All @@ -104,7 +105,7 @@ public Classwork getClassworkById(ObjectId id) {
public ClassworkDetailsDTO getClassworkDetailsById(ObjectId id) {
Classwork classwork = classworkRepository.findById(id);

if(classwork == null) {
if (classwork == null) {
throw new ResponseStatusException(HttpStatusCode.valueOf(404), "Classwork not found!");
}

Expand Down Expand Up @@ -142,14 +143,19 @@ public List<UserAnswerStatusDTO> getUserAnswerStatus(ObjectId classworkId) {
List<User> classroomUsers = classroom.getParticipants();

return classroomUsers.stream()
.filter(user -> user.getRole().equals(Role.STUDENT))
.map(user -> {
UserAnswerStatusDTO userAnswerStatus = new UserAnswerStatusDTO();
userAnswerStatus.setUser(mapper.map(user, UserDTO.class));
boolean hasAnswered = answerRepository.existsAnswerByUserIdAndClassworkId(user.getId(), classworkId);
userAnswerStatus.setHasAnswered(hasAnswered);
return userAnswerStatus;
}).toList();
.filter(user -> user.getRole().equals(Role.STUDENT))
.map(user -> {
UserAnswerStatusDTO userAnswerStatus = new UserAnswerStatusDTO();
userAnswerStatus.setUser(mapper.map(user, UserDTO.class));
Optional<Answer> answer = answerRepository.findByUserIdAndClassworkId(user.getId(), classworkId);
if (answer.isEmpty()) {
userAnswerStatus.setHasAnswered(false);
} else {
userAnswerStatus.setHasAnswered(true);
userAnswerStatus.setCorrectPercentage(answer.get().getCorrectPercentage());
}
return userAnswerStatus;
}).toList();
}

}

0 comments on commit a56cd7e

Please sign in to comment.