Skip to content

Commit

Permalink
add role check and return UserDTO
Browse files Browse the repository at this point in the history
  • Loading branch information
luuh-oliveira committed Jun 7, 2024
1 parent b555d42 commit 1f04c86
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/main/java/api/educai/dto/answer/UserAnswerStatusDTO.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package api.educai.dto.answer;

import api.educai.dto.user.UserDTO;
import lombok.Data;

@Data
public class UserAnswerStatusDTO {
private String userId;
private UserDTO user;
private boolean hasAnswered;
}
20 changes: 12 additions & 8 deletions src/main/java/api/educai/services/ClassworkService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import api.educai.dto.classwork.ClassworkDetailsDTO;
import api.educai.dto.answer.QuestionAnswerDTO;
import api.educai.dto.answer.UserAnswerStatusDTO;
import api.educai.dto.user.UserDTO;
import api.educai.entities.*;
import api.educai.enums.Role;
import api.educai.repositories.AnswerRepository;
import api.educai.repositories.ClassroomRepository;
import api.educai.repositories.ClassworkRepository;
Expand Down Expand Up @@ -135,17 +137,19 @@ public int getAnswerScore(Answer answer) {
}

public List<UserAnswerStatusDTO> getUserAnswerStatus(ObjectId classworkId) {
Classwork classwork = getClassworkById(classworkId);

Classroom classroom = classroomService.getClassroomByClassworkId(classworkId);
List<User> classroomUsers = classroom.getParticipants();

return classroomUsers.stream().map(user -> {
UserAnswerStatusDTO userAnswerStatus = new UserAnswerStatusDTO();
userAnswerStatus.setUserId(String.valueOf(user.getId()));
boolean hasAnswered = answerRepository.existsAnswerByUserIdAndClassworkId(user.getId(), classworkId);
userAnswerStatus.setHasAnswered(hasAnswered);
return userAnswerStatus;
}).collect(Collectors.toList());
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();
}

}

0 comments on commit 1f04c86

Please sign in to comment.