Skip to content

Commit

Permalink
Merge pull request #54 from educ-ai-org/fix/postId
Browse files Browse the repository at this point in the history
Fix/returnID
  • Loading branch information
luuh-oliveira authored Jun 1, 2024
2 parents 6f1811f + 6266212 commit 48b0365
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/main/java/api/educai/controllers/ClassworkController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api.educai.controllers;

import api.educai.dto.AnswerDTO;
import api.educai.dto.ClassworkDetailsDTO;
import api.educai.entities.Answer;
import api.educai.entities.Classwork;
import api.educai.services.ClassworkService;
Expand Down Expand Up @@ -50,8 +51,8 @@ public ResponseEntity<Void> addAnswer(

@Operation(summary = "Retorna uma atividade via id")
@GetMapping("/{id}")
public ResponseEntity<Classwork> getClassworkById(@PathVariable ObjectId id) {
Classwork classwork = classworkService.getClassworkById(id);
public ResponseEntity<ClassworkDetailsDTO> getClassworkById(@PathVariable ObjectId id) {
ClassworkDetailsDTO classwork = classworkService.getClassworkDetailsById(id);
return status(200).body(classwork);
}

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/api/educai/dto/AnswerDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@
import api.educai.entities.Answer;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.util.List;

@Data
@NoArgsConstructor
public class AnswerDTO {

private String id;
@NotNull
private ClassroomParticipantsDTO user;

@NotNull
private LocalDate datePosting;
private List<QuestionAnswerDTO> questionAnswers;

public AnswerDTO(Answer answer) {
this.id = String.valueOf(answer.getId());
this.user = new ClassroomParticipantsDTO(answer.getUser());
this.datePosting = answer.getDatePosting();
this.questionAnswers = answer.getQuestionAnswers();
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/api/educai/dto/ClassroomInfoDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@
import api.educai.entities.Classroom;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.Getter;
import lombok.Data;
import lombok.NoArgsConstructor;

@Getter
import java.util.List;

@Data
@NoArgsConstructor
public class ClassroomInfoDTO {

private String id;
@NotBlank
@Size(max = 100)
private String title;
@NotBlank
@Size(max = 50)
private String course;
private List<PostDTO> posts;
private List<ClassworkDTO> classworks;
private List<UserDTO> participants;

public ClassroomInfoDTO(Classroom classroom) {
this.id = classroom.getId().toString();
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/api/educai/dto/ClassroomParticipantsDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class ClassroomParticipantsDTO {

private String id;
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/api/educai/dto/ClassworkDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;

@Data
@NoArgsConstructor
public class ClassworkDTO {

private String id;
@Size(max = 100)
private String title;
@NotNull
Expand All @@ -20,12 +23,15 @@ public class ClassworkDTO {
private String description;
@NotNull
private Integer totalQuestions;
private Integer totalAnswers;

public ClassworkDTO(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.totalAnswers = classwork.getAnswers().size();
}
}
26 changes: 26 additions & 0 deletions src/main/java/api/educai/dto/ClassworkDetailsDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package api.educai.dto;

import api.educai.entities.Question;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Getter;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Getter
public class ClassworkDetailsDTO {

private String id;
@Size(max = 100)
private String title;
@NotNull
private LocalDate datePosting;
@NotNull
private LocalDate endDate;
@Size(max = 200)
private String description;
private List<QuestionDTO> questions = new ArrayList<>();

}
12 changes: 12 additions & 0 deletions src/main/java/api/educai/dto/OptionDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package api.educai.dto;

import lombok.Getter;

@Getter
public class OptionDTO {

private String id;
private String key;
private String description;

}
17 changes: 17 additions & 0 deletions src/main/java/api/educai/dto/QuestionDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package api.educai.dto;

import api.educai.entities.Option;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

@Getter
public class QuestionDTO {

private String id;
private String description;
private List<OptionDTO> options = new ArrayList<>();
private String correctAnswerKey;

}
4 changes: 4 additions & 0 deletions src/main/java/api/educai/dto/UserDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
import api.educai.entities.User;
import api.educai.enums.Role;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class UserDTO {
private String id;
private String name;
private String email;
private Role role;

public UserDTO(User user) {
this.id = String.valueOf(user.getId());
this.name = user.getName();
this.email = user.getEmail();
this.role = user.getRole();
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/api/educai/services/ClassroomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ public ClassroomInfoDTO createClassroom(Classroom classroom, ObjectId ownerId) {
public ClassroomInfoDTO getClassroomDataById(ObjectId id) {
Classroom classroom = getClassroomById(id);

return new ClassroomInfoDTO(classroom);
ClassroomInfoDTO classroomInfo = mapper.map(classroom, ClassroomInfoDTO.class);
classroomInfo.setPosts(mapper.map(classroom.getPosts(), new TypeToken<List<PostDTO>>() {}.getType()));
classroomInfo.setParticipants(mapper.map(classroom.getParticipants(), new TypeToken<List<UserDTO>>(){}.getType()));
classroomInfo.setClassworks(classroom.getClassworks().stream().map(ClassworkDTO::new).toList());

return classroomInfo;
}

public void inviteUser(ObjectId id, UserDTO newUser) {
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/api/educai/services/ClassworkService.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package api.educai.services;

import api.educai.dto.AnswerDTO;
import api.educai.dto.ClassworkDetailsDTO;
import api.educai.dto.QuestionAnswerDTO;
import api.educai.entities.*;
import api.educai.repositories.AnswerRepository;
import api.educai.repositories.ClassroomRepository;
import api.educai.repositories.ClassworkRepository;
import org.bson.types.ObjectId;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatusCode;
import org.springframework.stereotype.Service;
Expand All @@ -32,6 +34,9 @@ public class ClassworkService {
@Autowired
private UserService userService;

@Autowired
private ModelMapper mapper;

private final Integer CLASSWORK_SCORE = 10;

public Classwork createClasswork(Classwork classwork, ObjectId classroomId, ObjectId userId) {
Expand Down Expand Up @@ -85,6 +90,16 @@ public Classwork getClassworkById(ObjectId id) {
return classwork;
}

public ClassworkDetailsDTO getClassworkDetailsById(ObjectId id) {
Classwork classwork = classworkRepository.findById(id);

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

return mapper.map(classwork, ClassworkDetailsDTO.class);
}

public List<AnswerDTO> getAnswers(ObjectId classworkId) {
Classwork classwork = classworkRepository.findById(classworkId);
return classwork.getAnswers().stream().map(AnswerDTO::new).toList();
Expand Down

0 comments on commit 48b0365

Please sign in to comment.