Skip to content

Commit

Permalink
Merge pull request #37 from InnoTutor/develop
Browse files Browse the repository at this point in the history
/my-students response includes info about enroller and a card #20
  • Loading branch information
SMore-Napi committed Dec 24, 2021
2 parents c470703 + 3d31cd2 commit 23a612c
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package innotutor.innotutor_backend.controller;

import innotutor.innotutor_backend.dto.enrollment.RequestedStudentsListDTO;
import innotutor.innotutor_backend.dto.enrollment.RequestedStudentsListInfoDTO;
import innotutor.innotutor_backend.security.CustomPrincipal;
import innotutor.innotutor_backend.service.CardEnrollService;
import innotutor.innotutor_backend.service.StudentsService;
Expand All @@ -26,8 +26,8 @@ public MyStudentsController(final StudentsService studentsService, final CardEnr
}

@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<RequestedStudentsListDTO> getUserStudentsList(@AuthenticationPrincipal final CustomPrincipal user) {
final RequestedStudentsListDTO students = studentsService.getUserStudentsList(userService.getUserId(user));
public ResponseEntity<RequestedStudentsListInfoDTO> getUserStudentsList(@AuthenticationPrincipal final CustomPrincipal user) {
final RequestedStudentsListInfoDTO students = studentsService.getUserStudentsListFullInfo(userService.getUserId(user));
return students == null
? new ResponseEntity<>(HttpStatus.NOT_FOUND)
: new ResponseEntity<>(students, HttpStatus.OK);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package innotutor.innotutor_backend.dto.enrollment;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class MyStudentDTO {
private Long enrollmentId;
private Long studentId;
private String studentName;
private String studentSurname;
private String studentEmail;
private String studentContacts;
private String studentDescription;
private String studentPicture;
private Long cardId;
private String subject;
private Double rating;
private Integer countVoted;
private String description;
private boolean hidden;
private List<String> sessionFormat;
private List<String> sessionType;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package innotutor.innotutor_backend.dto.enrollment;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class RequestedStudentsListInfoDTO {
private List<MyStudentDTO> newStudents;
private List<MyStudentDTO> acceptedStudents;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package innotutor.innotutor_backend.service;

import innotutor.innotutor_backend.dto.UserDTO;
import innotutor.innotutor_backend.dto.card.CardDTO;
import innotutor.innotutor_backend.dto.enrollment.EnrollmentDTO;
import innotutor.innotutor_backend.dto.enrollment.MyStudentDTO;
import innotutor.innotutor_backend.dto.enrollment.RequestedStudentsListDTO;
import innotutor.innotutor_backend.dto.enrollment.RequestedStudentsListInfoDTO;
import innotutor.innotutor_backend.entity.card.Card;
import innotutor.innotutor_backend.entity.card.enrollment.CardEnroll;
import innotutor.innotutor_backend.entity.user.Request;
Expand All @@ -18,14 +22,18 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class StudentsService {
private final UserRepository userRepository;
private final RequestRepository requestRepository;
private final EnrollmentStatusRepository enrollmentStatusRepository;
private final UserService userService;
private final CardService cardService;

public RequestedStudentsListDTO getUserStudentsList(final Long userId) {
final Optional<User> userOptional = userRepository.findById(userId);
Expand All @@ -41,6 +49,50 @@ public RequestedStudentsListDTO getUserStudentsList(final Long userId) {
return null;
}

public RequestedStudentsListInfoDTO getUserStudentsListFullInfo(final Long userId) {
RequestedStudentsListDTO students = this.getUserStudentsList(userId);
if (students != null) {
List<MyStudentDTO> newStudents = students.getNewStudents().stream()
.map(this::convertEnrollmentDTOToEnrollmentInfoDTO)
.filter(Objects::nonNull)
.collect(Collectors.toList());
List<MyStudentDTO> acceptedStudents = students.getAcceptedStudents().stream()
.map(this::convertEnrollmentDTOToEnrollmentInfoDTO)
.filter(Objects::nonNull)
.collect(Collectors.toList());
return new RequestedStudentsListInfoDTO(newStudents, acceptedStudents);
}
return null;
}

private MyStudentDTO convertEnrollmentDTOToEnrollmentInfoDTO(EnrollmentDTO enrollmentDTO) {
Long enrollerId = enrollmentDTO.getEnrollerId();
Long cardId = enrollmentDTO.getCardId();
UserDTO enroller = userService.getUserById(enrollerId);
CardDTO card = cardService.getCardById(cardId, enrollerId);
if (enroller != null && card != null) {
return new MyStudentDTO(
enrollmentDTO.getEnrollmentId(),
enrollerId,
enroller.getName(),
enroller.getSurname(),
enroller.getEmail(),
enroller.getContacts(),
enroller.getDescription(),
enroller.getPicture(),
cardId,
card.getSubject(),
card.getRating(),
card.getCountVoted(),
card.getDescription(),
card.isHidden(),
enrollmentDTO.getSessionFormat(),
enrollmentDTO.getSessionType()
);
}
return null;
}

private List<EnrollmentDTO> getStudentsListByStatusId(final User user, final Long statusId) {
final List<EnrollmentDTO> studentsList = new ArrayList<>();
user.getServicesByUserId().forEach(service -> service.getCardByCardId().getCardEnrollsByCardId()
Expand Down

0 comments on commit 23a612c

Please sign in to comment.