Skip to content

Commit

Permalink
response the student's real name
Browse files Browse the repository at this point in the history
  • Loading branch information
scglwsj committed Sep 28, 2024
1 parent f2f5301 commit 9d1ca9f
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 37 deletions.
5 changes: 2 additions & 3 deletions src/main/java/com/quemistry/quiz_ms/client/UserClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
import com.quemistry.quiz_ms.client.model.SearchStudentResponse;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import java.util.List;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(name = "user-client", url = "${service.user.url}/v1")
public interface UserClient {
@RequestMapping(value = "/v1/student/search", method = POST, produces = APPLICATION_JSON_VALUE)
List<SearchStudentResponse> searchStudents(
@RequestMapping(value = "/student/search", method = POST, produces = APPLICATION_JSON_VALUE)
SearchStudentResponse searchStudents(
SearchStudentRequest searchStudentRequest,
@RequestHeader("x-user-id") @NotBlank String tutorId,
@RequestHeader("x-user-email") @Email String tutorEmail,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.quemistry.quiz_ms.client.model;

import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -10,15 +11,33 @@
@AllArgsConstructor
@NoArgsConstructor
public class SearchStudentResponse {
private Long id;
private String statusCode;
private String statusMessage;
private String serviceName;
private List<ErrorDto> errors;
private List<StudentResponse> payload;

private String firstName;
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class StudentResponse {
private Long id;
private String firstName;
private String lastName;
private String email;
private String accountId;

private String lastName;
public static StudentResponse defaultStudent(String studentId) {
return StudentResponse.builder()
.accountId(studentId)
.firstName("Unknown")
.lastName("Unknown")
.build();
}

private String email;

private String accountId;

private String educationLevel;
public String getFullName() {
return firstName + " " + lastName;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.quemistry.quiz_ms.controller.model;

import com.quemistry.quiz_ms.client.model.MCQDto;
import com.quemistry.quiz_ms.client.model.SearchStudentResponse.StudentResponse;
import com.quemistry.quiz_ms.model.TestAttempt;
import com.quemistry.quiz_ms.model.TestEntity;
import com.quemistry.quiz_ms.model.TestStatus;
Expand All @@ -27,7 +28,11 @@ public class TestMcqAttemptResponse extends MCQDto {
private List<McqStudentAttemptResponse> attempts;

public static TestMcqAttemptResponse from(
TestEntity test, int index, MCQDto mcq, List<TestAttempt> attempts) {
TestEntity test,
int index,
MCQDto mcq,
List<TestAttempt> attempts,
List<StudentResponse> studentResponses) {
return TestMcqAttemptResponse.builder()
.id(test.getId())
.testStatus(test.getStatus())
Expand All @@ -46,7 +51,21 @@ public static TestMcqAttemptResponse from(
.publishedBy(mcq.getPublishedBy())
.closedOn(mcq.getClosedOn())
.closedBy(mcq.getClosedBy())
.attempts(attempts.stream().map(McqStudentAttemptResponse::from).toList())
.attempts(
attempts.stream()
.map(
(TestAttempt attempt) ->
McqStudentAttemptResponse.from(
attempt,
studentResponses.stream()
.filter(
studentResponse ->
studentResponse
.getAccountId()
.equals(attempt.getStudentId()))
.findFirst()
.orElse(StudentResponse.defaultStudent(attempt.getStudentId()))))
.toList())
.build();
}

Expand All @@ -60,11 +79,11 @@ public static class McqStudentAttemptResponse {
private Integer optionNo;
private Date attemptTime;

public static McqStudentAttemptResponse from(TestAttempt attempt) {
public static McqStudentAttemptResponse from(
TestAttempt attempt, StudentResponse studentResponse) {
return McqStudentAttemptResponse.builder()
.studentId(attempt.getStudentId())
// TODO: studentName should be set to the student's name
.studentName("Student " + attempt.getStudentId())
.studentName(studentResponse.getFullName())
.optionNo(attempt.getOptionNo())
.attemptTime(attempt.getAttemptTime())
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.quemistry.quiz_ms.controller.model;

import com.quemistry.quiz_ms.client.model.SearchStudentResponse;
import com.quemistry.quiz_ms.model.*;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -29,7 +30,8 @@ public static TestStudentAttemptResponse from(
List<TestMcqs> testMcqs,
List<MCQResponse> mcqs,
List<TestAttempt> attempts,
TestStudent student) {
TestStudent student,
SearchStudentResponse.StudentResponse studentResponse) {
return TestStudentAttemptResponse.builder()
.id(test.getId())
.status(test.getStatus())
Expand All @@ -38,8 +40,7 @@ public static TestStudentAttemptResponse from(
.createdOn(test.getCreatedOn())
.updatedOn(test.getUpdatedOn())
.studentId(student.getStudentId())
// TODO: studentName should be set to the student's name
.studentName("Student " + student.getStudentId())
.studentName(studentResponse.getFullName())
.points(student.getPoints())
.mcqs(
testMcqs.stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.quemistry.quiz_ms.controller.model;

import com.quemistry.quiz_ms.client.model.MCQDto;
import com.quemistry.quiz_ms.client.model.SearchStudentResponse.StudentResponse;
import com.quemistry.quiz_ms.model.*;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -28,7 +29,8 @@ public static TestStudentDetailResponse from(
List<TestMcqs> testMcqs,
List<MCQResponse> mcqs,
List<TestAttempt> attempts,
List<TestStudent> students) {
List<TestStudent> students,
List<StudentResponse> studentResponses) {
return TestStudentDetailResponse.builder()
.id(test.getId())
.status(test.getStatus())
Expand All @@ -43,6 +45,14 @@ public static TestStudentDetailResponse from(
student ->
TestStudentResponse.from(
student,
studentResponses.stream()
.filter(
studentResponse ->
studentResponse
.getAccountId()
.equals(student.getStudentId()))
.findFirst()
.orElse(StudentResponse.defaultStudent(student.getStudentId())),
attempts.stream()
.filter(
attempt ->
Expand All @@ -66,11 +76,13 @@ public static class TestStudentResponse {
private int correctMcqCount;

public static TestStudentResponse from(
TestStudent student, List<TestAttempt> attempts, List<MCQResponse> mcqs) {
TestStudent student,
StudentResponse studentResponse,
List<TestAttempt> attempts,
List<MCQResponse> mcqs) {
return TestStudentResponse.builder()
.studentId(student.getStudentId())
// TODO: studentName should be set to the student's name
.studentName("Student " + student.getStudentId())
.studentName(studentResponse.getFullName())
.points(student.getPoints())
.attemptMcqCount(
attempts.stream().filter(attempt -> attempt.getOptionNo() != null).toList().size())
Expand Down
35 changes: 26 additions & 9 deletions src/main/java/com/quemistry/quiz_ms/service/TestService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.quemistry.quiz_ms.client.QuestionClient;
import com.quemistry.quiz_ms.client.UserClient;
import com.quemistry.quiz_ms.client.model.*;
import com.quemistry.quiz_ms.client.model.SearchStudentResponse.StudentResponse;
import com.quemistry.quiz_ms.controller.model.*;
import com.quemistry.quiz_ms.exception.*;
import com.quemistry.quiz_ms.mapper.MCQMapper;
Expand Down Expand Up @@ -155,12 +156,17 @@ public TestStudentDetailResponse getTestStudentDetail(Long testId, UserContext u
var testData = getTestData(testId, userContext);
List<TestStudent> testStudents = testStudentRepository.findByTestId(testId);

List<StudentResponse> studentResponses =
self.searchStudent(
testId, testStudents.stream().map(TestStudent::getStudentId).toList(), userContext);

return TestStudentDetailResponse.from(
testData.getValue0(),
testData.getValue1(),
testData.getValue2(),
testData.getValue3(),
testStudents);
testStudents,
studentResponses);
}

public TestStudentAttemptResponse getTestStudentAttempts(
Expand All @@ -172,12 +178,17 @@ public TestStudentAttemptResponse getTestStudentAttempts(
if (student.isEmpty()) {
throw new NotFoundException("Student not found");
}

StudentResponse studentResponses =
self.searchStudent(testId, List.of(studentId), userContext).getFirst();

return TestStudentAttemptResponse.from(
testMcqDetail.getValue0(),
testMcqDetail.getValue1(),
testMcqDetail.getValue2(),
attempts,
student.get());
student.get(),
studentResponses);
}

public TestStudentAttemptResponse getMyTestStudentAttempts(Long testId, UserContext userContext) {
Expand Down Expand Up @@ -220,8 +231,12 @@ public TestMcqAttemptResponse getTestMcqAttempts(
}

List<TestAttempt> attempts = testAttemptRepository.findByTestIdAndMcqId(testId, mcqId);
List<StudentResponse> studentResponses =
self.searchStudent(
testId, attempts.stream().map(TestAttempt::getStudentId).toList(), userContext);

return TestMcqAttemptResponse.from(test, testMcq.get().getIndex(), mcq.get(), attempts);
return TestMcqAttemptResponse.from(
test, testMcq.get().getIndex(), mcq.get(), attempts, studentResponses);
}

public void startTest(Long testId, UserContext userContext) {
Expand Down Expand Up @@ -290,13 +305,15 @@ public RetrieveMCQResponse getTestMcqs(Long testId, UserContext userContext, Lis
}

@Cacheable(value = "students", key = "#testId")
public List<SearchStudentResponse> searchStudent(
public List<StudentResponse> searchStudent(
Long testId, List<String> studentIds, UserContext userContext) {
return userClient.searchStudents(
SearchStudentRequest.from(studentIds),
userContext.getUserId(),
userContext.getUserEmail(),
userContext.getUserRoles());
return userClient
.searchStudents(
SearchStudentRequest.from(studentIds),
userContext.getUserId(),
userContext.getUserEmail(),
userContext.getUserRoles())
.getPayload();
}

private void saveTestMcqAndStudentAndAttempt(TestRequest testRequest, Long testId) {
Expand Down
27 changes: 25 additions & 2 deletions src/test/java/com/quemistry/quiz_ms/fixture/TestFixture.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.github.jsonzou.jmockdata.MockConfig;
import com.quemistry.quiz_ms.client.model.MCQDto;
import com.quemistry.quiz_ms.client.model.RetrieveMCQResponse;
import com.quemistry.quiz_ms.client.model.SearchStudentResponse;
import com.quemistry.quiz_ms.client.model.SearchStudentResponse.StudentResponse;
import com.quemistry.quiz_ms.model.*;
import java.util.List;

Expand All @@ -18,6 +20,9 @@ public class TestFixture {
public static final Integer CURRENT_OPTION_NO = 1;

public static final String STUDENT_ID = "student Id";
public static final String STUDENT_FIRST_NAME = "Ce";
public static final String STUDENT_LAST_NAME = "Shi Ren";
public static final String STUDENT_NAME = "Ce Shi Ren";
public static final String TUTOR_ID = "tutor Id";
public static final String TEST_TITLE = "test title";

Expand All @@ -29,8 +34,26 @@ public class TestFixture {

public static final UserContext studentContext =
new UserContext(STUDENT_ID, "[email protected]", "student");
public static final UserContext tutorContext =
new UserContext(TUTOR_ID, "[email protected]", "tutor");
public static final StudentResponse STUDENT_RESPONSE =
StudentResponse.builder()
.id(1L)
.firstName(STUDENT_FIRST_NAME)
.lastName(STUDENT_LAST_NAME)
.email("[email protected]")
.accountId(STUDENT_ID)
.build();

public static final SearchStudentResponse SEARCH_STUDENT_RESPONSE =
SearchStudentResponse.builder()
.statusCode("200")
.statusMessage("Success")
.serviceName("quiz-service")
.payload(List.of(STUDENT_RESPONSE))
.build();

public static final String TUTOR_EMAIL = "[email protected]";
public static final String TUTOR_ROLE = "tutor";
public static final UserContext tutorContext = new UserContext(TUTOR_ID, TUTOR_EMAIL, TUTOR_ROLE);

public static final TestEntity testEntity =
TestEntity.builder().id(TEST_ID).tutorId(TUTOR_ID).status(DRAFT).title(TEST_TITLE).build();
Expand Down
Loading

0 comments on commit 9d1ca9f

Please sign in to comment.