From 1957e74f6851bbf17ddcfb0584de14581ee7a1ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADza=20O=2E=20Rosa?= Date: Fri, 7 Jun 2024 10:21:25 -0300 Subject: [PATCH 1/5] add get answer status of all users --- .../educai/controllers/ClassworkController.java | 10 ++++++++++ .../java/api/educai/dto/UserAnswerStatusDTO.java | 9 +++++++++ .../api/educai/repositories/AnswerRepository.java | 3 +++ .../api/educai/services/ClassroomService.java | 10 ++++++++++ .../api/educai/services/ClassworkService.java | 15 +++++++++++++++ 5 files changed, 47 insertions(+) create mode 100644 src/main/java/api/educai/dto/UserAnswerStatusDTO.java diff --git a/src/main/java/api/educai/controllers/ClassworkController.java b/src/main/java/api/educai/controllers/ClassworkController.java index ddcd31f..a936b3f 100644 --- a/src/main/java/api/educai/controllers/ClassworkController.java +++ b/src/main/java/api/educai/controllers/ClassworkController.java @@ -2,6 +2,7 @@ import api.educai.dto.AnswerDTO; import api.educai.dto.ClassworkDetailsDTO; +import api.educai.dto.UserAnswerStatusDTO; import api.educai.entities.Answer; import api.educai.entities.Classwork; import api.educai.services.ClassworkService; @@ -49,6 +50,15 @@ public ResponseEntity addAnswer( return status(201).build(); } + @Operation(summary = "Retorna uma lista de usuários e o status da resposta para uma atividade") + @Secured("ROLE_TEACHER") + @GetMapping("/{id}/answers/status") + public ResponseEntity> getUserAnswerStatus( + @PathVariable ObjectId id) { + List userAnswerStatusList = classworkService.getUserAnswerStatus(id); + return status(200).body(userAnswerStatusList); + } + @Operation(summary = "Retorna uma atividade via id") @GetMapping("/{id}") public ResponseEntity getClassworkById(@PathVariable ObjectId id) { diff --git a/src/main/java/api/educai/dto/UserAnswerStatusDTO.java b/src/main/java/api/educai/dto/UserAnswerStatusDTO.java new file mode 100644 index 0000000..d28a8a6 --- /dev/null +++ b/src/main/java/api/educai/dto/UserAnswerStatusDTO.java @@ -0,0 +1,9 @@ +package api.educai.dto; + +import lombok.Data; + +@Data +public class UserAnswerStatusDTO { + private String userId; + private boolean hasAnswered; +} \ No newline at end of file diff --git a/src/main/java/api/educai/repositories/AnswerRepository.java b/src/main/java/api/educai/repositories/AnswerRepository.java index 85037c0..21e8028 100644 --- a/src/main/java/api/educai/repositories/AnswerRepository.java +++ b/src/main/java/api/educai/repositories/AnswerRepository.java @@ -4,10 +4,13 @@ import org.bson.types.ObjectId; import org.springframework.data.mongodb.repository.MongoRepository; +import java.util.List; + public interface AnswerRepository extends MongoRepository { Answer findById(ObjectId id); boolean existsAnswerByUserIdAndClassworkId(ObjectId userId, ObjectId classworkId); + List findAllByClassworkId(ObjectId classworkId); } diff --git a/src/main/java/api/educai/services/ClassroomService.java b/src/main/java/api/educai/services/ClassroomService.java index 668e811..2ee908f 100644 --- a/src/main/java/api/educai/services/ClassroomService.java +++ b/src/main/java/api/educai/services/ClassroomService.java @@ -185,4 +185,14 @@ public List getLeaderBoard(ObjectId classroomId) { return mapper.map(usersScore, new TypeToken>(){}.getType()); } + + public Classroom getClassroomByClassworkId(ObjectId classworkId) { + List classrooms = classroomRepository.findAll(); + return classrooms.stream() + .filter(classroom -> classroom.getClassworks().stream() + .anyMatch(classwork -> classwork.getId().equals(classworkId))) + .findFirst() + .orElseThrow(() -> new ResponseStatusException(HttpStatusCode.valueOf(404), "Classroom not found!")); + } + } diff --git a/src/main/java/api/educai/services/ClassworkService.java b/src/main/java/api/educai/services/ClassworkService.java index 177fb0b..45ce448 100644 --- a/src/main/java/api/educai/services/ClassworkService.java +++ b/src/main/java/api/educai/services/ClassworkService.java @@ -3,6 +3,7 @@ import api.educai.dto.AnswerDTO; import api.educai.dto.ClassworkDetailsDTO; import api.educai.dto.QuestionAnswerDTO; +import api.educai.dto.UserAnswerStatusDTO; import api.educai.entities.*; import api.educai.repositories.AnswerRepository; import api.educai.repositories.ClassroomRepository; @@ -133,4 +134,18 @@ public int getAnswerScore(Answer answer) { } + public List getUserAnswerStatus(ObjectId classworkId) { + Classwork classwork = getClassworkById(classworkId); + Classroom classroom = classroomService.getClassroomByClassworkId(classworkId); + List 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()); + } + } From 03f6737ea6b55c44df7504b8b23a0a3b55b87187 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADza=20O=2E=20Rosa?= Date: Fri, 7 Jun 2024 10:32:24 -0300 Subject: [PATCH 2/5] refactor dto package --- .../controllers/ClassroomController.java | 18 +++++++++++++++--- .../controllers/ClassworkController.java | 6 +++--- .../controllers/DictionaryController.java | 2 +- .../api/educai/controllers/PostController.java | 6 +++--- .../api/educai/controllers/UserController.java | 13 ++++++------- .../api/educai/dto/{ => answer}/AnswerDTO.java | 3 ++- .../api/educai/dto/{ => answer}/OptionDTO.java | 2 +- .../dto/{ => answer}/QuestionAnswerDTO.java | 2 +- .../educai/dto/{ => answer}/QuestionDTO.java | 3 +-- .../dto/{ => answer}/UserAnswerStatusDTO.java | 2 +- .../api/educai/dto/{ => auth}/AuthDTO.java | 2 +- .../educai/dto/{ => auth}/InvalidTokenDTO.java | 2 +- .../api/educai/dto/{ => auth}/LoginDTO.java | 2 +- .../api/educai/dto/{ => auth}/TokenDTO.java | 2 +- .../AddStudentInClassroomDTO.java | 2 +- .../dto/{ => classroom}/ClassroomInfoDTO.java | 5 ++++- .../ClassroomParticipantsDTO.java | 2 +- .../PatchClassroomTitleAndCourse.java | 3 +-- .../dto/{ => classroom}/UserScoreDTO.java | 3 +-- .../dto/{ => classwork}/ClassworkDTO.java | 2 +- .../{ => classwork}/ClassworkDetailsDTO.java | 4 ++-- .../{ => dictionary}/GetWordDefinitionDTO.java | 2 +- .../dto/{ => dictionary}/MeaningDTO.java | 2 +- .../{ => dictionary}/WordDefinitionDTO.java | 2 +- .../api/educai/dto/{ => post}/NewPostDTO.java | 2 +- .../api/educai/dto/{ => post}/PatchPost.java | 2 +- .../api/educai/dto/{ => post}/PostDTO.java | 2 +- .../dto/{ => user}/NewStudentEmailDTO.java | 2 +- .../dto/{ => user}/PatchUserEmailAndName.java | 2 +- .../api/educai/dto/{ => user}/ReportDTO.java | 2 +- .../dto/{ => user}/StudentClassroomsDTO.java | 3 ++- .../dto/{ => user}/TeacherClassroomsDTO.java | 3 ++- .../api/educai/dto/{ => user}/UserDTO.java | 2 +- .../educai/dto/{ => user}/UserDetailsDTO.java | 2 +- src/main/java/api/educai/entities/Answer.java | 2 +- .../api/educai/entities/TokenBlacklist.java | 2 +- .../java/api/educai/filter/TokenFilter.java | 2 +- .../educai/services/AuthenticationService.java | 2 +- .../api/educai/services/ClassroomService.java | 10 +++++++++- .../api/educai/services/ClassworkService.java | 8 ++++---- .../api/educai/services/DictionaryService.java | 6 +++--- .../java/api/educai/services/PostService.java | 4 ++-- .../java/api/educai/services/token/IToken.java | 2 +- .../educai/services/token/RefreshToken.java | 2 +- .../java/api/educai/services/token/Token.java | 2 +- .../api/educai/utils/email/EmailService.java | 4 ++-- 46 files changed, 91 insertions(+), 69 deletions(-) rename src/main/java/api/educai/dto/{ => answer}/AnswerDTO.java (88%) rename src/main/java/api/educai/dto/{ => answer}/OptionDTO.java (81%) rename src/main/java/api/educai/dto/{ => answer}/QuestionAnswerDTO.java (90%) rename src/main/java/api/educai/dto/{ => answer}/QuestionDTO.java (81%) rename src/main/java/api/educai/dto/{ => answer}/UserAnswerStatusDTO.java (80%) rename src/main/java/api/educai/dto/{ => auth}/AuthDTO.java (92%) rename src/main/java/api/educai/dto/{ => auth}/InvalidTokenDTO.java (93%) rename src/main/java/api/educai/dto/{ => auth}/LoginDTO.java (91%) rename src/main/java/api/educai/dto/{ => auth}/TokenDTO.java (82%) rename src/main/java/api/educai/dto/{ => classroom}/AddStudentInClassroomDTO.java (92%) rename src/main/java/api/educai/dto/{ => classroom}/ClassroomInfoDTO.java (82%) rename src/main/java/api/educai/dto/{ => classroom}/ClassroomParticipantsDTO.java (94%) rename src/main/java/api/educai/dto/{ => classroom}/PatchClassroomTitleAndCourse.java (75%) rename src/main/java/api/educai/dto/{ => classroom}/UserScoreDTO.java (75%) rename src/main/java/api/educai/dto/{ => classwork}/ClassworkDTO.java (96%) rename src/main/java/api/educai/dto/{ => classwork}/ClassworkDetailsDTO.java (87%) rename src/main/java/api/educai/dto/{ => dictionary}/GetWordDefinitionDTO.java (96%) rename src/main/java/api/educai/dto/{ => dictionary}/MeaningDTO.java (97%) rename src/main/java/api/educai/dto/{ => dictionary}/WordDefinitionDTO.java (98%) rename src/main/java/api/educai/dto/{ => post}/NewPostDTO.java (95%) rename src/main/java/api/educai/dto/{ => post}/PatchPost.java (92%) rename src/main/java/api/educai/dto/{ => post}/PostDTO.java (93%) rename src/main/java/api/educai/dto/{ => user}/NewStudentEmailDTO.java (88%) rename src/main/java/api/educai/dto/{ => user}/PatchUserEmailAndName.java (91%) rename src/main/java/api/educai/dto/{ => user}/ReportDTO.java (85%) rename src/main/java/api/educai/dto/{ => user}/StudentClassroomsDTO.java (80%) rename src/main/java/api/educai/dto/{ => user}/TeacherClassroomsDTO.java (80%) rename src/main/java/api/educai/dto/{ => user}/UserDTO.java (95%) rename src/main/java/api/educai/dto/{ => user}/UserDetailsDTO.java (98%) diff --git a/src/main/java/api/educai/controllers/ClassroomController.java b/src/main/java/api/educai/controllers/ClassroomController.java index d0e9274..69a8870 100644 --- a/src/main/java/api/educai/controllers/ClassroomController.java +++ b/src/main/java/api/educai/controllers/ClassroomController.java @@ -1,10 +1,15 @@ package api.educai.controllers; -import api.educai.dto.*; +import api.educai.dto.classroom.ClassroomInfoDTO; +import api.educai.dto.classroom.PatchClassroomTitleAndCourse; +import api.educai.dto.classroom.UserScoreDTO; +import api.educai.dto.classwork.ClassworkDTO; +import api.educai.dto.post.PostDTO; +import api.educai.dto.user.ReportDTO; +import api.educai.dto.user.UserDTO; import api.educai.entities.Classroom; import api.educai.entities.Post; import api.educai.services.ClassroomService; -import api.educai.services.PostService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.servlet.http.HttpServletRequest; @@ -21,7 +26,6 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.server.ResponseStatusException; -import java.io.IOException; import java.util.List; import static org.springframework.http.ResponseEntity.*; @@ -74,6 +78,14 @@ public ResponseEntity> getClassworksByClassroom(@PathVariable return classworks.isEmpty() ? status(204).build() : status(200).body(classworks); } + @Operation(summary = "Retorna atividades de uma sala de aula com o status de resposta do usuário") + @Secured("ROLE_STUDENT") + @GetMapping("/{id}/classworks/{userId}") + public ResponseEntity> getClassworksByClassroom(@PathVariable ObjectId id, @PathVariable ObjectId userId) { + List classworks = classroomService.getClassworks(id); + return classworks.isEmpty() ? status(204).build() : status(200).body(classworks); + } + @Operation(summary = "Retorna posts de uma sala de aula") @GetMapping("/{id}/posts") public ResponseEntity> getPostsByClassroom(@PathVariable ObjectId id){ diff --git a/src/main/java/api/educai/controllers/ClassworkController.java b/src/main/java/api/educai/controllers/ClassworkController.java index a936b3f..8fc8c12 100644 --- a/src/main/java/api/educai/controllers/ClassworkController.java +++ b/src/main/java/api/educai/controllers/ClassworkController.java @@ -1,8 +1,8 @@ package api.educai.controllers; -import api.educai.dto.AnswerDTO; -import api.educai.dto.ClassworkDetailsDTO; -import api.educai.dto.UserAnswerStatusDTO; +import api.educai.dto.answer.AnswerDTO; +import api.educai.dto.classwork.ClassworkDetailsDTO; +import api.educai.dto.answer.UserAnswerStatusDTO; import api.educai.entities.Answer; import api.educai.entities.Classwork; import api.educai.services.ClassworkService; diff --git a/src/main/java/api/educai/controllers/DictionaryController.java b/src/main/java/api/educai/controllers/DictionaryController.java index 2e08352..bb4f76a 100644 --- a/src/main/java/api/educai/controllers/DictionaryController.java +++ b/src/main/java/api/educai/controllers/DictionaryController.java @@ -1,6 +1,6 @@ package api.educai.controllers; -import api.educai.dto.WordDefinitionDTO; +import api.educai.dto.dictionary.WordDefinitionDTO; import api.educai.services.DictionaryService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; diff --git a/src/main/java/api/educai/controllers/PostController.java b/src/main/java/api/educai/controllers/PostController.java index 87d3669..39af7fd 100644 --- a/src/main/java/api/educai/controllers/PostController.java +++ b/src/main/java/api/educai/controllers/PostController.java @@ -1,8 +1,8 @@ package api.educai.controllers; -import api.educai.dto.NewPostDTO; -import api.educai.dto.PatchPost; -import api.educai.dto.PostDTO; +import api.educai.dto.post.NewPostDTO; +import api.educai.dto.post.PatchPost; +import api.educai.dto.post.PostDTO; import api.educai.entities.Post; import api.educai.services.PostService; import io.swagger.v3.oas.annotations.Operation; diff --git a/src/main/java/api/educai/controllers/UserController.java b/src/main/java/api/educai/controllers/UserController.java index 459b5d2..2a87a08 100644 --- a/src/main/java/api/educai/controllers/UserController.java +++ b/src/main/java/api/educai/controllers/UserController.java @@ -1,11 +1,11 @@ package api.educai.controllers; -import api.educai.dto.UserDTO; -import api.educai.dto.ClassroomInfoDTO; -import api.educai.dto.AuthDTO; -import api.educai.dto.LoginDTO; -import api.educai.dto.PatchUserEmailAndName; -import api.educai.dto.TokenDTO; +import api.educai.dto.user.UserDTO; +import api.educai.dto.classroom.ClassroomInfoDTO; +import api.educai.dto.auth.AuthDTO; +import api.educai.dto.auth.LoginDTO; +import api.educai.dto.user.PatchUserEmailAndName; +import api.educai.dto.auth.TokenDTO; import api.educai.entities.User; import api.educai.services.UserService; import io.swagger.v3.oas.annotations.Operation; @@ -22,7 +22,6 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; -import java.io.IOException; import java.net.URISyntaxException; import java.util.List; diff --git a/src/main/java/api/educai/dto/AnswerDTO.java b/src/main/java/api/educai/dto/answer/AnswerDTO.java similarity index 88% rename from src/main/java/api/educai/dto/AnswerDTO.java rename to src/main/java/api/educai/dto/answer/AnswerDTO.java index 053eb8d..a663d6d 100644 --- a/src/main/java/api/educai/dto/AnswerDTO.java +++ b/src/main/java/api/educai/dto/answer/AnswerDTO.java @@ -1,5 +1,6 @@ -package api.educai.dto; +package api.educai.dto.answer; +import api.educai.dto.classroom.ClassroomParticipantsDTO; import api.educai.entities.Answer; import jakarta.validation.constraints.NotNull; import lombok.Data; diff --git a/src/main/java/api/educai/dto/OptionDTO.java b/src/main/java/api/educai/dto/answer/OptionDTO.java similarity index 81% rename from src/main/java/api/educai/dto/OptionDTO.java rename to src/main/java/api/educai/dto/answer/OptionDTO.java index 7a2ad9f..79d10df 100644 --- a/src/main/java/api/educai/dto/OptionDTO.java +++ b/src/main/java/api/educai/dto/answer/OptionDTO.java @@ -1,4 +1,4 @@ -package api.educai.dto; +package api.educai.dto.answer; import lombok.Getter; diff --git a/src/main/java/api/educai/dto/QuestionAnswerDTO.java b/src/main/java/api/educai/dto/answer/QuestionAnswerDTO.java similarity index 90% rename from src/main/java/api/educai/dto/QuestionAnswerDTO.java rename to src/main/java/api/educai/dto/answer/QuestionAnswerDTO.java index 15d7779..795970c 100644 --- a/src/main/java/api/educai/dto/QuestionAnswerDTO.java +++ b/src/main/java/api/educai/dto/answer/QuestionAnswerDTO.java @@ -1,4 +1,4 @@ -package api.educai.dto; +package api.educai.dto.answer; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; diff --git a/src/main/java/api/educai/dto/QuestionDTO.java b/src/main/java/api/educai/dto/answer/QuestionDTO.java similarity index 81% rename from src/main/java/api/educai/dto/QuestionDTO.java rename to src/main/java/api/educai/dto/answer/QuestionDTO.java index 94fb414..4dd035f 100644 --- a/src/main/java/api/educai/dto/QuestionDTO.java +++ b/src/main/java/api/educai/dto/answer/QuestionDTO.java @@ -1,6 +1,5 @@ -package api.educai.dto; +package api.educai.dto.answer; -import api.educai.entities.Option; import lombok.Getter; import java.util.ArrayList; diff --git a/src/main/java/api/educai/dto/UserAnswerStatusDTO.java b/src/main/java/api/educai/dto/answer/UserAnswerStatusDTO.java similarity index 80% rename from src/main/java/api/educai/dto/UserAnswerStatusDTO.java rename to src/main/java/api/educai/dto/answer/UserAnswerStatusDTO.java index d28a8a6..ddb1535 100644 --- a/src/main/java/api/educai/dto/UserAnswerStatusDTO.java +++ b/src/main/java/api/educai/dto/answer/UserAnswerStatusDTO.java @@ -1,4 +1,4 @@ -package api.educai.dto; +package api.educai.dto.answer; import lombok.Data; diff --git a/src/main/java/api/educai/dto/AuthDTO.java b/src/main/java/api/educai/dto/auth/AuthDTO.java similarity index 92% rename from src/main/java/api/educai/dto/AuthDTO.java rename to src/main/java/api/educai/dto/auth/AuthDTO.java index dc2c6c0..5b1342b 100644 --- a/src/main/java/api/educai/dto/AuthDTO.java +++ b/src/main/java/api/educai/dto/auth/AuthDTO.java @@ -1,4 +1,4 @@ -package api.educai.dto; +package api.educai.dto.auth; public class AuthDTO { private String token; diff --git a/src/main/java/api/educai/dto/InvalidTokenDTO.java b/src/main/java/api/educai/dto/auth/InvalidTokenDTO.java similarity index 93% rename from src/main/java/api/educai/dto/InvalidTokenDTO.java rename to src/main/java/api/educai/dto/auth/InvalidTokenDTO.java index ce4f6d2..cedf5bc 100644 --- a/src/main/java/api/educai/dto/InvalidTokenDTO.java +++ b/src/main/java/api/educai/dto/auth/InvalidTokenDTO.java @@ -1,4 +1,4 @@ -package api.educai.dto; +package api.educai.dto.auth; import java.util.Date; diff --git a/src/main/java/api/educai/dto/LoginDTO.java b/src/main/java/api/educai/dto/auth/LoginDTO.java similarity index 91% rename from src/main/java/api/educai/dto/LoginDTO.java rename to src/main/java/api/educai/dto/auth/LoginDTO.java index 453aeac..e934ed7 100644 --- a/src/main/java/api/educai/dto/LoginDTO.java +++ b/src/main/java/api/educai/dto/auth/LoginDTO.java @@ -1,4 +1,4 @@ -package api.educai.dto; +package api.educai.dto.auth; import jakarta.validation.constraints.Email; import jakarta.validation.constraints.Size; diff --git a/src/main/java/api/educai/dto/TokenDTO.java b/src/main/java/api/educai/dto/auth/TokenDTO.java similarity index 82% rename from src/main/java/api/educai/dto/TokenDTO.java rename to src/main/java/api/educai/dto/auth/TokenDTO.java index 191069b..0588f97 100644 --- a/src/main/java/api/educai/dto/TokenDTO.java +++ b/src/main/java/api/educai/dto/auth/TokenDTO.java @@ -1,4 +1,4 @@ -package api.educai.dto; +package api.educai.dto.auth; import lombok.AllArgsConstructor; import lombok.Getter; diff --git a/src/main/java/api/educai/dto/AddStudentInClassroomDTO.java b/src/main/java/api/educai/dto/classroom/AddStudentInClassroomDTO.java similarity index 92% rename from src/main/java/api/educai/dto/AddStudentInClassroomDTO.java rename to src/main/java/api/educai/dto/classroom/AddStudentInClassroomDTO.java index c91fb60..4b935a5 100644 --- a/src/main/java/api/educai/dto/AddStudentInClassroomDTO.java +++ b/src/main/java/api/educai/dto/classroom/AddStudentInClassroomDTO.java @@ -1,4 +1,4 @@ -package api.educai.dto; +package api.educai.dto.classroom; public class AddStudentInClassroomDTO { private String student_email; diff --git a/src/main/java/api/educai/dto/ClassroomInfoDTO.java b/src/main/java/api/educai/dto/classroom/ClassroomInfoDTO.java similarity index 82% rename from src/main/java/api/educai/dto/ClassroomInfoDTO.java rename to src/main/java/api/educai/dto/classroom/ClassroomInfoDTO.java index 4e03c10..7dd244f 100644 --- a/src/main/java/api/educai/dto/ClassroomInfoDTO.java +++ b/src/main/java/api/educai/dto/classroom/ClassroomInfoDTO.java @@ -1,5 +1,8 @@ -package api.educai.dto; +package api.educai.dto.classroom; +import api.educai.dto.classwork.ClassworkDTO; +import api.educai.dto.post.PostDTO; +import api.educai.dto.user.UserDTO; import api.educai.entities.Classroom; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.Size; diff --git a/src/main/java/api/educai/dto/ClassroomParticipantsDTO.java b/src/main/java/api/educai/dto/classroom/ClassroomParticipantsDTO.java similarity index 94% rename from src/main/java/api/educai/dto/ClassroomParticipantsDTO.java rename to src/main/java/api/educai/dto/classroom/ClassroomParticipantsDTO.java index 51c3f3e..cdebcc2 100644 --- a/src/main/java/api/educai/dto/ClassroomParticipantsDTO.java +++ b/src/main/java/api/educai/dto/classroom/ClassroomParticipantsDTO.java @@ -1,4 +1,4 @@ -package api.educai.dto; +package api.educai.dto.classroom; import api.educai.entities.User; import jakarta.validation.constraints.Email; diff --git a/src/main/java/api/educai/dto/PatchClassroomTitleAndCourse.java b/src/main/java/api/educai/dto/classroom/PatchClassroomTitleAndCourse.java similarity index 75% rename from src/main/java/api/educai/dto/PatchClassroomTitleAndCourse.java rename to src/main/java/api/educai/dto/classroom/PatchClassroomTitleAndCourse.java index 7ccff00..ee631ff 100644 --- a/src/main/java/api/educai/dto/PatchClassroomTitleAndCourse.java +++ b/src/main/java/api/educai/dto/classroom/PatchClassroomTitleAndCourse.java @@ -1,6 +1,5 @@ -package api.educai.dto; +package api.educai.dto.classroom; -import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.Size; import lombok.Getter; diff --git a/src/main/java/api/educai/dto/UserScoreDTO.java b/src/main/java/api/educai/dto/classroom/UserScoreDTO.java similarity index 75% rename from src/main/java/api/educai/dto/UserScoreDTO.java rename to src/main/java/api/educai/dto/classroom/UserScoreDTO.java index 2a44790..28d72bc 100644 --- a/src/main/java/api/educai/dto/UserScoreDTO.java +++ b/src/main/java/api/educai/dto/classroom/UserScoreDTO.java @@ -1,7 +1,6 @@ -package api.educai.dto; +package api.educai.dto.classroom; import lombok.Getter; -import org.bson.types.ObjectId; @Getter public class UserScoreDTO { diff --git a/src/main/java/api/educai/dto/ClassworkDTO.java b/src/main/java/api/educai/dto/classwork/ClassworkDTO.java similarity index 96% rename from src/main/java/api/educai/dto/ClassworkDTO.java rename to src/main/java/api/educai/dto/classwork/ClassworkDTO.java index a409703..f72f68d 100644 --- a/src/main/java/api/educai/dto/ClassworkDTO.java +++ b/src/main/java/api/educai/dto/classwork/ClassworkDTO.java @@ -1,4 +1,4 @@ -package api.educai.dto; +package api.educai.dto.classwork; import api.educai.entities.Classwork; import jakarta.validation.constraints.NotNull; diff --git a/src/main/java/api/educai/dto/ClassworkDetailsDTO.java b/src/main/java/api/educai/dto/classwork/ClassworkDetailsDTO.java similarity index 87% rename from src/main/java/api/educai/dto/ClassworkDetailsDTO.java rename to src/main/java/api/educai/dto/classwork/ClassworkDetailsDTO.java index 23c2d45..d591908 100644 --- a/src/main/java/api/educai/dto/ClassworkDetailsDTO.java +++ b/src/main/java/api/educai/dto/classwork/ClassworkDetailsDTO.java @@ -1,6 +1,6 @@ -package api.educai.dto; +package api.educai.dto.classwork; -import api.educai.entities.Question; +import api.educai.dto.answer.QuestionDTO; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; import lombok.Getter; diff --git a/src/main/java/api/educai/dto/GetWordDefinitionDTO.java b/src/main/java/api/educai/dto/dictionary/GetWordDefinitionDTO.java similarity index 96% rename from src/main/java/api/educai/dto/GetWordDefinitionDTO.java rename to src/main/java/api/educai/dto/dictionary/GetWordDefinitionDTO.java index 00037bd..3968cb3 100644 --- a/src/main/java/api/educai/dto/GetWordDefinitionDTO.java +++ b/src/main/java/api/educai/dto/dictionary/GetWordDefinitionDTO.java @@ -1,4 +1,4 @@ -package api.educai.dto; +package api.educai.dto.dictionary; import lombok.Getter; diff --git a/src/main/java/api/educai/dto/MeaningDTO.java b/src/main/java/api/educai/dto/dictionary/MeaningDTO.java similarity index 97% rename from src/main/java/api/educai/dto/MeaningDTO.java rename to src/main/java/api/educai/dto/dictionary/MeaningDTO.java index 19eb9f1..fd5c20d 100644 --- a/src/main/java/api/educai/dto/MeaningDTO.java +++ b/src/main/java/api/educai/dto/dictionary/MeaningDTO.java @@ -1,4 +1,4 @@ -package api.educai.dto; +package api.educai.dto.dictionary; import api.educai.utils.ListObject; import lombok.Getter; diff --git a/src/main/java/api/educai/dto/WordDefinitionDTO.java b/src/main/java/api/educai/dto/dictionary/WordDefinitionDTO.java similarity index 98% rename from src/main/java/api/educai/dto/WordDefinitionDTO.java rename to src/main/java/api/educai/dto/dictionary/WordDefinitionDTO.java index 895d132..5a3fcb8 100644 --- a/src/main/java/api/educai/dto/WordDefinitionDTO.java +++ b/src/main/java/api/educai/dto/dictionary/WordDefinitionDTO.java @@ -1,4 +1,4 @@ -package api.educai.dto; +package api.educai.dto.dictionary; import api.educai.utils.ListObject; import lombok.Getter; diff --git a/src/main/java/api/educai/dto/NewPostDTO.java b/src/main/java/api/educai/dto/post/NewPostDTO.java similarity index 95% rename from src/main/java/api/educai/dto/NewPostDTO.java rename to src/main/java/api/educai/dto/post/NewPostDTO.java index 86d703e..a53af58 100644 --- a/src/main/java/api/educai/dto/NewPostDTO.java +++ b/src/main/java/api/educai/dto/post/NewPostDTO.java @@ -1,4 +1,4 @@ -package api.educai.dto; +package api.educai.dto.post; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; diff --git a/src/main/java/api/educai/dto/PatchPost.java b/src/main/java/api/educai/dto/post/PatchPost.java similarity index 92% rename from src/main/java/api/educai/dto/PatchPost.java rename to src/main/java/api/educai/dto/post/PatchPost.java index 849a0ec..64e9f19 100644 --- a/src/main/java/api/educai/dto/PatchPost.java +++ b/src/main/java/api/educai/dto/post/PatchPost.java @@ -1,4 +1,4 @@ -package api.educai.dto; +package api.educai.dto.post; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.Size; diff --git a/src/main/java/api/educai/dto/PostDTO.java b/src/main/java/api/educai/dto/post/PostDTO.java similarity index 93% rename from src/main/java/api/educai/dto/PostDTO.java rename to src/main/java/api/educai/dto/post/PostDTO.java index 7ebbfb1..9ef29d4 100644 --- a/src/main/java/api/educai/dto/PostDTO.java +++ b/src/main/java/api/educai/dto/post/PostDTO.java @@ -1,4 +1,4 @@ -package api.educai.dto; +package api.educai.dto.post; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.Size; diff --git a/src/main/java/api/educai/dto/NewStudentEmailDTO.java b/src/main/java/api/educai/dto/user/NewStudentEmailDTO.java similarity index 88% rename from src/main/java/api/educai/dto/NewStudentEmailDTO.java rename to src/main/java/api/educai/dto/user/NewStudentEmailDTO.java index 3e4736e..18f743b 100644 --- a/src/main/java/api/educai/dto/NewStudentEmailDTO.java +++ b/src/main/java/api/educai/dto/user/NewStudentEmailDTO.java @@ -1,4 +1,4 @@ -package api.educai.dto; +package api.educai.dto.user; import lombok.AllArgsConstructor; import lombok.Getter; diff --git a/src/main/java/api/educai/dto/PatchUserEmailAndName.java b/src/main/java/api/educai/dto/user/PatchUserEmailAndName.java similarity index 91% rename from src/main/java/api/educai/dto/PatchUserEmailAndName.java rename to src/main/java/api/educai/dto/user/PatchUserEmailAndName.java index fc06b22..16c4f9c 100644 --- a/src/main/java/api/educai/dto/PatchUserEmailAndName.java +++ b/src/main/java/api/educai/dto/user/PatchUserEmailAndName.java @@ -1,4 +1,4 @@ -package api.educai.dto; +package api.educai.dto.user; import jakarta.validation.constraints.Email; import jakarta.validation.constraints.NotBlank; diff --git a/src/main/java/api/educai/dto/ReportDTO.java b/src/main/java/api/educai/dto/user/ReportDTO.java similarity index 85% rename from src/main/java/api/educai/dto/ReportDTO.java rename to src/main/java/api/educai/dto/user/ReportDTO.java index 676ab3e..e70950c 100644 --- a/src/main/java/api/educai/dto/ReportDTO.java +++ b/src/main/java/api/educai/dto/user/ReportDTO.java @@ -1,4 +1,4 @@ -package api.educai.dto; +package api.educai.dto.user; import lombok.AllArgsConstructor; import lombok.Getter; diff --git a/src/main/java/api/educai/dto/StudentClassroomsDTO.java b/src/main/java/api/educai/dto/user/StudentClassroomsDTO.java similarity index 80% rename from src/main/java/api/educai/dto/StudentClassroomsDTO.java rename to src/main/java/api/educai/dto/user/StudentClassroomsDTO.java index 18b842f..a760ce2 100644 --- a/src/main/java/api/educai/dto/StudentClassroomsDTO.java +++ b/src/main/java/api/educai/dto/user/StudentClassroomsDTO.java @@ -1,5 +1,6 @@ -package api.educai.dto; +package api.educai.dto.user; +import api.educai.dto.classroom.ClassroomInfoDTO; import api.educai.entities.Classroom; import lombok.Getter; diff --git a/src/main/java/api/educai/dto/TeacherClassroomsDTO.java b/src/main/java/api/educai/dto/user/TeacherClassroomsDTO.java similarity index 80% rename from src/main/java/api/educai/dto/TeacherClassroomsDTO.java rename to src/main/java/api/educai/dto/user/TeacherClassroomsDTO.java index c5c4ba4..ca368a7 100644 --- a/src/main/java/api/educai/dto/TeacherClassroomsDTO.java +++ b/src/main/java/api/educai/dto/user/TeacherClassroomsDTO.java @@ -1,5 +1,6 @@ -package api.educai.dto; +package api.educai.dto.user; +import api.educai.dto.classroom.ClassroomInfoDTO; import api.educai.entities.Classroom; import lombok.Getter; diff --git a/src/main/java/api/educai/dto/UserDTO.java b/src/main/java/api/educai/dto/user/UserDTO.java similarity index 95% rename from src/main/java/api/educai/dto/UserDTO.java rename to src/main/java/api/educai/dto/user/UserDTO.java index e72ae60..956dc1c 100644 --- a/src/main/java/api/educai/dto/UserDTO.java +++ b/src/main/java/api/educai/dto/user/UserDTO.java @@ -1,4 +1,4 @@ -package api.educai.dto; +package api.educai.dto.user; import api.educai.entities.User; import api.educai.enums.Role; diff --git a/src/main/java/api/educai/dto/UserDetailsDTO.java b/src/main/java/api/educai/dto/user/UserDetailsDTO.java similarity index 98% rename from src/main/java/api/educai/dto/UserDetailsDTO.java rename to src/main/java/api/educai/dto/user/UserDetailsDTO.java index 5404cfe..78bbe0b 100644 --- a/src/main/java/api/educai/dto/UserDetailsDTO.java +++ b/src/main/java/api/educai/dto/user/UserDetailsDTO.java @@ -1,4 +1,4 @@ -package api.educai.dto; +package api.educai.dto.user; import api.educai.entities.User; import api.educai.enums.Role; diff --git a/src/main/java/api/educai/entities/Answer.java b/src/main/java/api/educai/entities/Answer.java index 4a0bba8..96ed2cf 100644 --- a/src/main/java/api/educai/entities/Answer.java +++ b/src/main/java/api/educai/entities/Answer.java @@ -1,6 +1,6 @@ package api.educai.entities; -import api.educai.dto.QuestionAnswerDTO; +import api.educai.dto.answer.QuestionAnswerDTO; import jakarta.validation.constraints.NotNull; import lombok.Data; import org.bson.types.ObjectId; diff --git a/src/main/java/api/educai/entities/TokenBlacklist.java b/src/main/java/api/educai/entities/TokenBlacklist.java index 45051d5..ae11d59 100644 --- a/src/main/java/api/educai/entities/TokenBlacklist.java +++ b/src/main/java/api/educai/entities/TokenBlacklist.java @@ -1,6 +1,6 @@ package api.educai.entities; -import api.educai.dto.InvalidTokenDTO; +import api.educai.dto.auth.InvalidTokenDTO; import lombok.Data; import org.bson.types.ObjectId; import org.springframework.data.mongodb.core.mapping.Document; diff --git a/src/main/java/api/educai/filter/TokenFilter.java b/src/main/java/api/educai/filter/TokenFilter.java index 73c90c0..54593c7 100644 --- a/src/main/java/api/educai/filter/TokenFilter.java +++ b/src/main/java/api/educai/filter/TokenFilter.java @@ -1,6 +1,6 @@ package api.educai.filter; -import api.educai.dto.UserDetailsDTO; +import api.educai.dto.user.UserDetailsDTO; import api.educai.enums.Role; import api.educai.repositories.UserRespository; import api.educai.services.token.Token; diff --git a/src/main/java/api/educai/services/AuthenticationService.java b/src/main/java/api/educai/services/AuthenticationService.java index ccb3830..577183a 100644 --- a/src/main/java/api/educai/services/AuthenticationService.java +++ b/src/main/java/api/educai/services/AuthenticationService.java @@ -1,6 +1,6 @@ package api.educai.services; -import api.educai.dto.UserDetailsDTO; +import api.educai.dto.user.UserDetailsDTO; import api.educai.entities.User; import api.educai.repositories.UserRespository; import org.springframework.beans.factory.annotation.Autowired; diff --git a/src/main/java/api/educai/services/ClassroomService.java b/src/main/java/api/educai/services/ClassroomService.java index 2ee908f..54c9f7f 100644 --- a/src/main/java/api/educai/services/ClassroomService.java +++ b/src/main/java/api/educai/services/ClassroomService.java @@ -1,6 +1,14 @@ package api.educai.services; -import api.educai.dto.*; +import api.educai.dto.classroom.AddStudentInClassroomDTO; +import api.educai.dto.classroom.ClassroomInfoDTO; +import api.educai.dto.classroom.PatchClassroomTitleAndCourse; +import api.educai.dto.classroom.UserScoreDTO; +import api.educai.dto.classwork.ClassworkDTO; +import api.educai.dto.post.PostDTO; +import api.educai.dto.user.NewStudentEmailDTO; +import api.educai.dto.user.ReportDTO; +import api.educai.dto.user.UserDTO; import api.educai.entities.Answer; import api.educai.entities.Classroom; import api.educai.entities.Post; diff --git a/src/main/java/api/educai/services/ClassworkService.java b/src/main/java/api/educai/services/ClassworkService.java index 45ce448..226e0ca 100644 --- a/src/main/java/api/educai/services/ClassworkService.java +++ b/src/main/java/api/educai/services/ClassworkService.java @@ -1,9 +1,9 @@ package api.educai.services; -import api.educai.dto.AnswerDTO; -import api.educai.dto.ClassworkDetailsDTO; -import api.educai.dto.QuestionAnswerDTO; -import api.educai.dto.UserAnswerStatusDTO; +import api.educai.dto.answer.AnswerDTO; +import api.educai.dto.classwork.ClassworkDetailsDTO; +import api.educai.dto.answer.QuestionAnswerDTO; +import api.educai.dto.answer.UserAnswerStatusDTO; import api.educai.entities.*; import api.educai.repositories.AnswerRepository; import api.educai.repositories.ClassroomRepository; diff --git a/src/main/java/api/educai/services/DictionaryService.java b/src/main/java/api/educai/services/DictionaryService.java index 9df0ead..0ee7b23 100644 --- a/src/main/java/api/educai/services/DictionaryService.java +++ b/src/main/java/api/educai/services/DictionaryService.java @@ -1,8 +1,8 @@ package api.educai.services; -import api.educai.dto.GetWordDefinitionDTO; -import api.educai.dto.MeaningDTO; -import api.educai.dto.WordDefinitionDTO; +import api.educai.dto.dictionary.GetWordDefinitionDTO; +import api.educai.dto.dictionary.MeaningDTO; +import api.educai.dto.dictionary.WordDefinitionDTO; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.http.HttpStatusCode; diff --git a/src/main/java/api/educai/services/PostService.java b/src/main/java/api/educai/services/PostService.java index 714c76b..7d640c8 100644 --- a/src/main/java/api/educai/services/PostService.java +++ b/src/main/java/api/educai/services/PostService.java @@ -1,7 +1,7 @@ package api.educai.services; -import api.educai.dto.NewPostDTO; -import api.educai.dto.PatchPost; +import api.educai.dto.post.NewPostDTO; +import api.educai.dto.post.PatchPost; import api.educai.entities.Classroom; import api.educai.entities.Post; import api.educai.repositories.ClassroomRepository; diff --git a/src/main/java/api/educai/services/token/IToken.java b/src/main/java/api/educai/services/token/IToken.java index c761194..999b3b4 100644 --- a/src/main/java/api/educai/services/token/IToken.java +++ b/src/main/java/api/educai/services/token/IToken.java @@ -1,6 +1,6 @@ package api.educai.services.token; -import api.educai.dto.UserDetailsDTO; +import api.educai.dto.user.UserDetailsDTO; import org.bson.types.ObjectId; import java.util.Date; diff --git a/src/main/java/api/educai/services/token/RefreshToken.java b/src/main/java/api/educai/services/token/RefreshToken.java index 6c29653..a718fd7 100644 --- a/src/main/java/api/educai/services/token/RefreshToken.java +++ b/src/main/java/api/educai/services/token/RefreshToken.java @@ -1,6 +1,6 @@ package api.educai.services.token; -import api.educai.dto.UserDetailsDTO; +import api.educai.dto.user.UserDetailsDTO; import api.educai.entities.TokenBlacklist; import api.educai.repositories.TokenBlacklistRepository; import com.auth0.jwt.JWT; diff --git a/src/main/java/api/educai/services/token/Token.java b/src/main/java/api/educai/services/token/Token.java index 4456922..5898c63 100644 --- a/src/main/java/api/educai/services/token/Token.java +++ b/src/main/java/api/educai/services/token/Token.java @@ -1,6 +1,6 @@ package api.educai.services.token; -import api.educai.dto.UserDetailsDTO; +import api.educai.dto.user.UserDetailsDTO; import api.educai.entities.TokenBlacklist; import api.educai.enums.Role; import api.educai.repositories.TokenBlacklistRepository; diff --git a/src/main/java/api/educai/utils/email/EmailService.java b/src/main/java/api/educai/utils/email/EmailService.java index ba1b430..8bce58d 100644 --- a/src/main/java/api/educai/utils/email/EmailService.java +++ b/src/main/java/api/educai/utils/email/EmailService.java @@ -1,7 +1,7 @@ package api.educai.utils.email; -import api.educai.dto.AddStudentInClassroomDTO; -import api.educai.dto.NewStudentEmailDTO; +import api.educai.dto.classroom.AddStudentInClassroomDTO; +import api.educai.dto.user.NewStudentEmailDTO; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.http.HttpStatusCode; import org.springframework.web.server.ResponseStatusException; From c9db2f58ee095f2cdb6aba36c0eaa8065135df28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADza=20O=2E=20Rosa?= Date: Fri, 7 Jun 2024 10:32:51 -0300 Subject: [PATCH 3/5] refactor package user service --- src/main/java/api/educai/services/UserService.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/api/educai/services/UserService.java b/src/main/java/api/educai/services/UserService.java index 8cb7afc..08cf714 100644 --- a/src/main/java/api/educai/services/UserService.java +++ b/src/main/java/api/educai/services/UserService.java @@ -1,6 +1,10 @@ package api.educai.services; -import api.educai.dto.*; +import api.educai.dto.auth.AuthDTO; +import api.educai.dto.auth.LoginDTO; +import api.educai.dto.auth.TokenDTO; +import api.educai.dto.classroom.ClassroomInfoDTO; +import api.educai.dto.user.*; import api.educai.entities.Classwork; import api.educai.entities.TokenBlacklist; import api.educai.entities.User; From b555d4238775645322ff2c30c2124c7a2b9db615 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADza=20O=2E=20Rosa?= Date: Fri, 7 Jun 2024 10:52:43 -0300 Subject: [PATCH 4/5] add endpoint for user classworks with status --- .../controllers/ClassroomController.java | 6 ++- .../dto/classwork/ClassworkUserDTO.java | 37 +++++++++++++++++++ .../api/educai/services/ClassroomService.java | 12 ++++++ 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 src/main/java/api/educai/dto/classwork/ClassworkUserDTO.java diff --git a/src/main/java/api/educai/controllers/ClassroomController.java b/src/main/java/api/educai/controllers/ClassroomController.java index 69a8870..be56dad 100644 --- a/src/main/java/api/educai/controllers/ClassroomController.java +++ b/src/main/java/api/educai/controllers/ClassroomController.java @@ -4,6 +4,7 @@ import api.educai.dto.classroom.PatchClassroomTitleAndCourse; import api.educai.dto.classroom.UserScoreDTO; import api.educai.dto.classwork.ClassworkDTO; +import api.educai.dto.classwork.ClassworkUserDTO; import api.educai.dto.post.PostDTO; import api.educai.dto.user.ReportDTO; import api.educai.dto.user.UserDTO; @@ -72,6 +73,7 @@ public ResponseEntity> getClassroomParticipants(@PathVariable Obje } @Operation(summary = "Retorna atividades de uma sala de aula") + @Secured("ROLE_TEACHER") @GetMapping("/{id}/classworks") public ResponseEntity> getClassworksByClassroom(@PathVariable ObjectId id) { List classworks = classroomService.getClassworks(id); @@ -81,8 +83,8 @@ public ResponseEntity> getClassworksByClassroom(@PathVariable @Operation(summary = "Retorna atividades de uma sala de aula com o status de resposta do usuário") @Secured("ROLE_STUDENT") @GetMapping("/{id}/classworks/{userId}") - public ResponseEntity> getClassworksByClassroom(@PathVariable ObjectId id, @PathVariable ObjectId userId) { - List classworks = classroomService.getClassworks(id); + public ResponseEntity> getClassworksByClassroom(@PathVariable ObjectId id, @PathVariable ObjectId userId) { + List classworks = classroomService.getClassworksByUser(id, userId); return classworks.isEmpty() ? status(204).build() : status(200).body(classworks); } diff --git a/src/main/java/api/educai/dto/classwork/ClassworkUserDTO.java b/src/main/java/api/educai/dto/classwork/ClassworkUserDTO.java new file mode 100644 index 0000000..ba83e4f --- /dev/null +++ b/src/main/java/api/educai/dto/classwork/ClassworkUserDTO.java @@ -0,0 +1,37 @@ +package api.educai.dto.classwork; + +import api.educai.entities.Classwork; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.time.LocalDate; + +@Data +@NoArgsConstructor +public class ClassworkUserDTO { + + private String id; + @Size(max = 100) + private String title; + @NotNull + private LocalDate datePosting; + @NotNull + private LocalDate endDate; + @Size(max = 200) + private String description; + @NotNull + private Integer totalQuestions; + private boolean hasAnswered; + + public ClassworkUserDTO(Classwork classwork) { + this.id = String.valueOf(classwork.getId()); + this.title = classwork.getTitle(); + this.datePosting = classwork.getDatePosting(); + this.endDate = classwork.getEndDate(); + this.description = classwork.getDescription(); + this.totalQuestions = classwork.getQuestions().size(); + this.hasAnswered = false; + } +} diff --git a/src/main/java/api/educai/services/ClassroomService.java b/src/main/java/api/educai/services/ClassroomService.java index 54c9f7f..ec01fe2 100644 --- a/src/main/java/api/educai/services/ClassroomService.java +++ b/src/main/java/api/educai/services/ClassroomService.java @@ -5,6 +5,7 @@ import api.educai.dto.classroom.PatchClassroomTitleAndCourse; import api.educai.dto.classroom.UserScoreDTO; import api.educai.dto.classwork.ClassworkDTO; +import api.educai.dto.classwork.ClassworkUserDTO; import api.educai.dto.post.PostDTO; import api.educai.dto.user.NewStudentEmailDTO; import api.educai.dto.user.ReportDTO; @@ -134,6 +135,17 @@ public List getClassworks(ObjectId id) { return classroom.getClassworks().stream().map(ClassworkDTO::new).toList(); } + public List getClassworksByUser(ObjectId id, ObjectId userId) { + Classroom classroom = getClassroomById(id); + User user = userService.getUserById(userId); + return classroom.getClassworks().stream().map(classwork -> { + ClassworkUserDTO classworkUserDTO = new ClassworkUserDTO(classwork); + classworkUserDTO.setHasAnswered(classwork.getAnswers().stream() + .anyMatch(answer -> answer.getUser().equals(user))); + return classworkUserDTO; + }).toList(); + } + public ReportDTO getUserReport(ObjectId classroomId, ObjectId userId) { User user = userService.getUserById(userId); Classroom classroom = getClassroomById(classroomId); From 1f04c86b47634ebd9ceae31063952a2d00086f73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADza=20O=2E=20Rosa?= Date: Fri, 7 Jun 2024 11:05:22 -0300 Subject: [PATCH 5/5] add role check and return UserDTO --- .../dto/answer/UserAnswerStatusDTO.java | 3 ++- .../api/educai/services/ClassworkService.java | 20 +++++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/main/java/api/educai/dto/answer/UserAnswerStatusDTO.java b/src/main/java/api/educai/dto/answer/UserAnswerStatusDTO.java index ddb1535..da796c2 100644 --- a/src/main/java/api/educai/dto/answer/UserAnswerStatusDTO.java +++ b/src/main/java/api/educai/dto/answer/UserAnswerStatusDTO.java @@ -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; } \ No newline at end of file diff --git a/src/main/java/api/educai/services/ClassworkService.java b/src/main/java/api/educai/services/ClassworkService.java index 226e0ca..7c8f0ae 100644 --- a/src/main/java/api/educai/services/ClassworkService.java +++ b/src/main/java/api/educai/services/ClassworkService.java @@ -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; @@ -135,17 +137,19 @@ public int getAnswerScore(Answer answer) { } public List getUserAnswerStatus(ObjectId classworkId) { - Classwork classwork = getClassworkById(classworkId); + Classroom classroom = classroomService.getClassroomByClassworkId(classworkId); List 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(); } }