-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
V1.0 #2
base: main
Are you sure you want to change the base?
V1.0 #2
Changes from 40 commits
ca7dd5e
9d2a12c
4b9a801
6085a75
4b8ea37
b423af0
96c6122
807efda
0e482ca
9f15ef1
af9ffc4
49004b2
06a4419
f1107ad
edb92f0
2d865a6
aa712a1
56f1e47
5296158
725ca6e
08fed4b
dd6c879
30fc5ac
f1008ab
3c829a5
17d1029
be3be3d
f3df813
9025885
8a77a73
b097807
7853321
c2db4c9
1abf2c8
9e06e3e
52be679
c17cc47
ec5e118
d3a44bf
8917c3f
8481400
fb3758b
63ffd34
b69fa61
f1bdd3e
dc939ea
27a54a3
a854968
a99a976
6980bb3
ecdfa2f
f692052
e72a87a
27dd024
e4f8f02
c1ae009
65d1049
b843bf2
4b51709
ee94026
d6177b8
470e3b5
df15a31
8e66d10
039c19a
1456932
a86663a
f09918a
dbe152b
499fd3b
e319f36
71143bc
72695e5
7e9574a
e1ca600
699aefb
6ead2e4
eaa6625
94dfc44
9558efc
4719689
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,37 +16,81 @@ | |
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.sql.Date; | ||
|
||
@RestController | ||
@CrossOrigin(origins = "http://localhost:3000") | ||
public class AssignmentController { | ||
|
||
@Autowired | ||
AssignmentRepository assignmentRepository; | ||
@Autowired | ||
CourseRepository courseRepository; | ||
@Autowired | ||
SectionRepository sectionRepository; | ||
@Autowired | ||
EnrollmentRepository enrollmentRepository; | ||
@Autowired | ||
GradeRepository gradeRepository; | ||
|
||
// instructor lists assignments for a section. Assignments ordered by due date. | ||
// logged in user must be the instructor for the section | ||
@GetMapping("/sections/{secNo}/assignments") | ||
public List<AssignmentDTO> getAssignments( | ||
@PathVariable("secNo") int secNo) { | ||
|
||
// TODO remove the following line when done | ||
List<Assignment> assignments = assignmentRepository.findBySectionNoOrderByDueDate(secNo); | ||
if (assignments.isEmpty()) { | ||
throw new ResponseStatusException( HttpStatus.NOT_FOUND, "section not found "); | ||
} | ||
List<AssignmentDTO> dto_list = new ArrayList<>(); | ||
for(Assignment a:assignments){ | ||
dto_list.add(new AssignmentDTO( | ||
a.getAssignmentId(), | ||
a.getTitle(), | ||
a.getDueDate().toString(), | ||
a.getSection().getCourse().getCourseId(), | ||
a.getSection().getSecId(), | ||
a.getSection().getSectionNo() | ||
)); | ||
} | ||
|
||
// hint: use the assignment repository method | ||
// findBySectionNoOrderByDueDate to return | ||
// a list of assignments | ||
|
||
return null; | ||
return dto_list; | ||
} | ||
|
||
// add assignment | ||
// user must be instructor of the section | ||
// return AssignmentDTO with assignmentID generated by database | ||
@PostMapping("/assignments") | ||
public AssignmentDTO createAssignment( | ||
@RequestBody AssignmentDTO dto) { | ||
public AssignmentDTO createAssignment(@RequestBody AssignmentDTO dto) { | ||
|
||
// TODO remove the following line when done | ||
Section s = sectionRepository.findById(dto.secId()).orElse(null); | ||
if (s==null) { | ||
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "section not found " + dto.secId()); | ||
} | ||
|
||
return null; | ||
// TODO remove the following line when done | ||
Assignment a = new Assignment(); | ||
a.setAssignmentId(dto.id()); | ||
a.setTitle(dto.title()); | ||
//convert string -> date | ||
Date date = Date.valueOf(dto.dueDate()); | ||
a.setDueDate(date); | ||
a.setSection(s); | ||
assignmentRepository.save(a); | ||
|
||
return new AssignmentDTO( | ||
a.getAssignmentId(), | ||
a.getTitle(), | ||
a.getDueDate().toString(), | ||
a.getSection().getCourse().getCourseId(), | ||
a.getSection().getSecId(), | ||
a.getSection().getSectionNo() | ||
); | ||
} | ||
|
||
// update assignment for a section. Only title and dueDate may be changed. | ||
|
@@ -55,17 +99,36 @@ public AssignmentDTO createAssignment( | |
@PutMapping("/assignments") | ||
public AssignmentDTO updateAssignment(@RequestBody AssignmentDTO dto) { | ||
|
||
// TODO remove the following line when done | ||
|
||
return null; | ||
Assignment a = assignmentRepository.findById(dto.id()).orElse(null); | ||
if(a==null){ | ||
throw new ResponseStatusException( HttpStatus.NOT_FOUND, "Assignment not found "+dto.id()); | ||
} | ||
|
||
a.setTitle(dto.title()); | ||
//convert string -> date | ||
Date date = Date.valueOf(dto.dueDate()); | ||
a.setDueDate(date); | ||
assignmentRepository.save(a); | ||
return new AssignmentDTO( | ||
a.getAssignmentId(), | ||
a.getTitle(), | ||
a.getDueDate().toString(), | ||
a.getSection().getCourse().getCourseId(), | ||
a.getSection().getSecId(), | ||
a.getSection().getSectionNo() | ||
); | ||
} | ||
|
||
// delete assignment for a section | ||
// logged in user must be instructor of the section | ||
@DeleteMapping("/assignments/{assignmentId}") | ||
public void deleteAssignment(@PathVariable("assignmentId") int assignmentId) { | ||
|
||
// TODO | ||
Assignment a = assignmentRepository.findById(assignmentId).orElse(null); | ||
//if assignment doesn't exist, do nothing | ||
if(a!=null){ | ||
assignmentRepository.delete(a); | ||
} | ||
} | ||
|
||
// instructor gets grades for assignment ordered by student name | ||
|
@@ -74,27 +137,56 @@ public void deleteAssignment(@PathVariable("assignmentId") int assignmentId) { | |
public List<GradeDTO> getAssignmentGrades(@PathVariable("assignmentId") int assignmentId) { | ||
|
||
// TODO remove the following line when done | ||
Assignment a = assignmentRepository.findById(assignmentId).orElse(null); | ||
if (a==null) { | ||
throw new ResponseStatusException( HttpStatus.NOT_FOUND, "assignment not found "+assignmentId); | ||
} | ||
|
||
// get the list of enrollments for the section related to this assignment. | ||
// hint: use te enrollment repository method findEnrollmentsBySectionOrderByStudentName. | ||
// hint: use the enrollment repository method findEnrollmentsBySectionOrderByStudentName. | ||
List<Enrollment> enrollments = enrollmentRepository.findEnrollmentsBySectionNoOrderByStudentName(a.getSection().getSectionNo()); | ||
|
||
// for each enrollment, get the grade related to the assignment and enrollment | ||
// hint: use the gradeRepository findByEnrollmentIdAndAssignmentId method. | ||
// if the grade does not exist, create a grade entity and set the score to NULL | ||
// and then save the new entity | ||
|
||
return null; | ||
List<GradeDTO> gradeDTOList = new ArrayList<>(); | ||
for(Enrollment e:enrollments){ | ||
// if the grade does not exist, create a grade entity and set the score to NULL | ||
Grade g = gradeRepository.findByEnrollmentIdAndAssignmentId(e.getEnrollmentId(), assignmentId); | ||
if(g==null){ | ||
g = new Grade(); | ||
g.setScore(null); | ||
} | ||
// and then save the new entity | ||
gradeRepository.save(g); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you tested this with postman, and is it working correctly? I think you will need to set a few more properties of the grade entity, and then return the grade with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added properties and reran There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM |
||
|
||
gradeDTOList.add(new GradeDTO( | ||
g.getGradeId(), | ||
e.getStudent().getName(), | ||
e.getStudent().getEmail(), | ||
a.getTitle(), | ||
e.getSection().getCourse().getCourseId(), | ||
e.getSection().getSecId(), | ||
g.getScore() | ||
)); | ||
} | ||
return gradeDTOList; | ||
} | ||
|
||
// instructor uploads grades for assignment | ||
// user must be instructor for the section | ||
@PutMapping("/grades") | ||
public void updateGrades(@RequestBody List<GradeDTO> dlist) { | ||
|
||
// TODO | ||
|
||
// for each grade in the GradeDTO list, retrieve the grade entity | ||
// update the score and save the entity | ||
|
||
for(GradeDTO dto:dlist){ | ||
Grade g = gradeRepository.findById(dto.gradeId()).orElse(null); | ||
if(g==null){ | ||
throw new ResponseStatusException( HttpStatus.NOT_FOUND, "Grade not found "+dto.gradeId()); | ||
} | ||
// update the score and save the entity | ||
g.setScore(dto.score()); | ||
gradeRepository.save(g); | ||
} | ||
} | ||
|
||
|
||
|
@@ -108,11 +200,29 @@ public List<AssignmentStudentDTO> getStudentAssignments( | |
@RequestParam("semester") String semester) { | ||
|
||
// TODO remove the following line when done | ||
List<AssignmentStudentDTO> returnDtos = new ArrayList<>(); | ||
List<Enrollment> enrollments = enrollmentRepository.findByYearAndSemesterOrderByCourseId(year, semester, studentId); | ||
List<Assignment> assignments = new ArrayList<>(); | ||
for (Enrollment e:enrollments){ | ||
assignments = assignmentRepository.findBySectionNoOrderByDueDate(e.getSection().getSectionNo()); | ||
for(Assignment a:assignments){ | ||
Grade grade = gradeRepository.findByEnrollmentIdAndAssignmentId(e.getEnrollmentId(), a.getAssignmentId()); | ||
returnDtos.add(new AssignmentStudentDTO( | ||
a.getAssignmentId(), | ||
a.getTitle(), | ||
a.getDueDate(), | ||
e.getSection().getCourse().getCourseId(), | ||
e.getSection().getSecId(), | ||
(grade!=null) ? grade.getScore() : null | ||
)); | ||
} | ||
} | ||
|
||
|
||
// return a list of assignments and (if they exist) the assignment grade | ||
// for all sections that the student is enrolled for the given year and semester | ||
// hint: use the assignment repository method findByStudentIdAndYearAndSemesterOrderByDueDate | ||
// hint: use the assignment repository method findByStudentIdAndYearAndSemesterOrderByDueDate | ||
|
||
return null; | ||
return returnDtos; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,62 @@ | ||
package com.cst438.controller; | ||
|
||
|
||
import com.cst438.domain.Enrollment; | ||
import com.cst438.domain.EnrollmentRepository; | ||
import com.cst438.dto.EnrollmentDTO; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@RestController | ||
@CrossOrigin(origins = "http://localhost:3000") | ||
public class EnrollmentController { | ||
|
||
@Autowired | ||
private EnrollmentRepository enrollmentRepository; | ||
|
||
// instructor downloads student enrollments for a section, ordered by student name | ||
// user must be instructor for the section | ||
@GetMapping("/sections/{sectionNo}/enrollments") | ||
public List<EnrollmentDTO> getEnrollments( | ||
@PathVariable("sectionNo") int sectionNo ) { | ||
|
||
// TODO | ||
// hint: use enrollment repository findEnrollmentsBySectionNoOrderByStudentName method | ||
// remove the following line when done | ||
return null; | ||
public List<EnrollmentDTO> getEnrollments(@PathVariable("sectionNo") int sectionNo) { | ||
List<Enrollment> enrollments = enrollmentRepository.findEnrollmentsBySectionNoOrderByStudentName(sectionNo); | ||
if(enrollments == null) { | ||
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Section number is invalid"); | ||
} | ||
|
||
return enrollments.stream() | ||
.map(enrollment -> new EnrollmentDTO( | ||
enrollment.getEnrollmentId(), | ||
enrollment.getGrade(), | ||
enrollment.getStudent().getId(), | ||
enrollment.getStudent().getName(), | ||
enrollment.getStudent().getEmail(), | ||
enrollment.getSection().getCourse().getCourseId(), | ||
enrollment.getSection().getSecId(), | ||
enrollment.getSection().getSectionNo(), | ||
enrollment.getSection().getBuilding(), | ||
enrollment.getSection().getRoom(), | ||
enrollment.getSection().getTimes(), | ||
enrollment.getSection().getCourse().getCredits(), | ||
enrollment.getSection().getTerm().getYear(), | ||
enrollment.getSection().getTerm().getSemester())) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
// instructor uploads enrollments with the final grades for the section | ||
// user must be instructor for the section | ||
@PutMapping("/enrollments") | ||
public void updateEnrollmentGrade(@RequestBody List<EnrollmentDTO> dlist) { | ||
|
||
// TODO | ||
|
||
// For each EnrollmentDTO in the list | ||
// find the Enrollment entity using enrollmentId | ||
// update the grade and save back to database | ||
|
||
public void updateEnrollmentGrade(@RequestBody List<EnrollmentDTO> dtoList) { | ||
for (EnrollmentDTO dto : dtoList) { | ||
Enrollment enrollment = enrollmentRepository.findById(dto.enrollmentId()) | ||
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Enrollment not found")); | ||
|
||
// Update the grade and save back to the database | ||
enrollment.setGrade(dto.grade()); | ||
enrollmentRepository.save(enrollment); | ||
} | ||
} | ||
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is making changes in the
pom.xml
file necessary for this PR?