Skip to content

Commit

Permalink
add getStudentsThatPassedCourse (cursada y final) endpoint for a cour…
Browse files Browse the repository at this point in the history
…se (#108)
  • Loading branch information
gibarsin authored Feb 5, 2017
1 parent e810b20 commit b7ba13b
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import ar.edu.itba.paw.models.Course;
import ar.edu.itba.paw.models.FinalInscription;
import ar.edu.itba.paw.models.Grade;
import ar.edu.itba.paw.models.users.Student;
import ar.edu.itba.paw.shared.CourseFilter;

import java.util.List;
import java.util.Map;

public interface CourseDao {

Expand Down Expand Up @@ -153,7 +155,7 @@ public interface CourseDao {
* @return The list of students that passed the course
*/

List<Student> getStudentsThatPassedCourse(String courseId);
Map<Student, Grade> getStudentsThatPassedCourse(String courseId);


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import ar.edu.itba.paw.models.Course;
import ar.edu.itba.paw.models.FinalInscription;
import ar.edu.itba.paw.models.Grade;
import ar.edu.itba.paw.models.users.Student;
import ar.edu.itba.paw.shared.CourseFilter;
import ar.edu.itba.paw.shared.StudentFilter;

import java.util.List;
import java.util.Map;
import java.util.Set;

public interface CourseService {
Expand Down Expand Up @@ -149,7 +151,7 @@ public interface CourseService {
* @param studentFilter The list of filters to apply
* @return The list of students that passed the course and complies with the given filters
*/
List<Student> getStudentsThatPassedCourse(String courseId, StudentFilter studentFilter);
Map<Student, Grade> getStudentsThatPassedCourse(String courseId, StudentFilter studentFilter);

/**
* Get the final inscriptions corresponding to a course.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ public Integer getTotalPlanCredits() {
}

@Override
public List<Student> getStudentsThatPassedCourse(final String courseId) {
final List<Student> studentsPassed = new LinkedList<>();
public Map<Student, Grade> getStudentsThatPassedCourse(final String courseId) {
final Map<Student, Grade> studentsPassed = new HashMap<>();

final TypedQuery<Grade> query = em.createQuery("select gr from Grade as gr where gr.course.courseId = :id and gr.grade >= 4", Grade.class);
query.setParameter("id", courseId);
Expand All @@ -295,7 +295,7 @@ public List<Student> getStudentsThatPassedCourse(final String courseId) {
for(Grade grade : grades){
for(FinalGrade finalGrade : grade.getFinalGrades()){
if(BigDecimal.valueOf(4).compareTo(finalGrade.getGrade()) <= 0){
studentsPassed.add(grade.getStudent());
studentsPassed.put(grade.getStudent(), grade);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@
import ar.edu.itba.paw.interfaces.StudentService;
import ar.edu.itba.paw.models.Course;
import ar.edu.itba.paw.models.FinalInscription;
import ar.edu.itba.paw.models.Grade;
import ar.edu.itba.paw.models.users.Student;
import ar.edu.itba.paw.shared.CourseFilter;
import ar.edu.itba.paw.shared.StudentFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

@Service
Expand Down Expand Up @@ -287,15 +285,14 @@ public List<Course> getAvailableAddCorrelatives(final String courseId, final Cou

@Transactional
@Override
public List<Student> getStudentsThatPassedCourse(final String courseId, final StudentFilter studentFilter) {
final List<Student> passedStudents = courseDao.getStudentsThatPassedCourse(courseId);
final List<Student> result = new LinkedList<>();
public Map<Student, Grade> getStudentsThatPassedCourse(final String courseId, final StudentFilter studentFilter) {
final Map<Student, Grade> passedStudents = courseDao.getStudentsThatPassedCourse(courseId);
final Map<Student, Grade> result = new HashMap<>();

final List<Student> studentsFilter = studentService.getByFilter(studentFilter);
for (Student student : studentsFilter) {
if (passedStudents.contains(student)) {
student.setGrades(student.getGrades());
result.add(student);
if (passedStudents.get(student) != null) {
result.put(student, passedStudents.get(student));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ar.edu.itba.paw.interfaces.CourseService;
import ar.edu.itba.paw.models.Course;
import ar.edu.itba.paw.models.FinalInscription;
import ar.edu.itba.paw.models.Grade;
import ar.edu.itba.paw.models.users.Student;
import ar.edu.itba.paw.shared.CourseFilter;
import ar.edu.itba.paw.shared.StudentFilter;
Expand All @@ -23,6 +24,7 @@
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static javax.ws.rs.core.Response.*;
Expand Down Expand Up @@ -165,11 +167,11 @@ public Response coursesStudentsPassedIndex(@PathParam("courseId") final String c
final StudentFilter studentFilter = new StudentFilter.StudentFilterBuilder()
.docket(docket).firstName(firstName).lastName(lastName).build();

final List<Student> approvedStudents = cs.getStudentsThatPassedCourse(courseId, studentFilter);
final Map<Student, Grade> approvedStudents = cs.getStudentsThatPassedCourse(courseId, studentFilter);

final List<StudentIndexDTO> studentsList = approvedStudents.stream().map(student -> mapper.convertToStudentIndexDTO(student)).collect(Collectors.toList());
final List<StudentWithGradeDTO> studentsList = mapper.convertToStudentsWithGradeDTO(approvedStudents);

return ok(new StudentsList(studentsList)).build();
return ok(new StudentsWithGradeList(studentsList)).build();
}

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
import org.modelmapper.spi.MatchingStrategy;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

public class DTOEntityMapper {
Expand Down Expand Up @@ -140,4 +139,21 @@ StudentSessionDTO convertToStudentSessionDTO(final Student student, final Collec

return studentSessionDTO;
}

List<StudentWithGradeDTO> convertToStudentsWithGradeDTO(final Map<Student, Grade> studentsWithGrade) {
final List<StudentWithGradeDTO> studentWithGradeDTOS = new ArrayList<>(studentsWithGrade.size());

for(final Map.Entry<Student, Grade> entry : studentsWithGrade.entrySet()) {
final StudentIndexDTO studentIndexDTO = convertToStudentIndexDTO(entry.getKey());
final Grade grade = entry.getValue();
final StudentWithGradeDTO studentWithGradeDTO = new StudentWithGradeDTO();

studentWithGradeDTO.setStudent(studentIndexDTO);
studentWithGradeDTO.setGrade(grade.getGrade());

studentWithGradeDTOS.add(studentWithGradeDTO);
}

return studentWithGradeDTOS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ar.edu.itba.paw.webapp.models;

import java.math.BigDecimal;

public class StudentWithGradeDTO {
private StudentIndexDTO student;
private BigDecimal grade;

public StudentWithGradeDTO() {
}

public StudentIndexDTO getStudent() {
return student;
}

public void setStudent(StudentIndexDTO student) {
this.student = student;
}

public BigDecimal getGrade() {
return grade;
}

public void setGrade(BigDecimal grade) {
this.grade = grade;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ar.edu.itba.paw.webapp.models;

import java.util.List;


public class StudentsWithGradeList {

private List<StudentWithGradeDTO> students;

public StudentsWithGradeList() {
}

public StudentsWithGradeList(final List<StudentWithGradeDTO> studentWithGradeDTOS) {
this.students = studentWithGradeDTOS;
}

public List<StudentWithGradeDTO> getStudents() {
return students;
}

public void setStudents(final List<StudentWithGradeDTO> students) {
this.students = students;
}
}

0 comments on commit b7ba13b

Please sign in to comment.