John-sCC / jcc_backend

Eat up!
1 stars 0 forks source link

Assignment Object - Implementing Assignment Submission Uploads #33

Closed drewreed2005 closed 6 months ago

drewreed2005 commented 7 months ago

Involved Scrum Members

These Scrum team members were responsible for contributions to this portion of the Assignment object:

Submission Code

AssignmentSubmission Object

The object is just a basic POJO stored as an entity. Originally, in this commit, it wasn't an SQL entity, as Drew wasn't originally intending for that. AJ emphasized making it an entity so that it could be related to Assignment and Person, so we reimplemented it in this commit.

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Convert(attributeName ="assignmentSubmission", converter = JsonType.class)
public class AssignmentSubmission {
    // automatic unique identifier for Person record
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ManyToOne(fetch = FetchType.EAGER)
    private Person submitter;

    @NonNull
    private String filePath;

    @Temporal(TemporalType.TIMESTAMP)
    private Date timeSubmitted;

    private int submissionNumber;
}

Upload Method

Raymond's upload method was the foundation for the submission endpoint method, implemented in this commit.

Comments throughout the method explain its functional, as below:

File tempDirectory = new File(tempUploadDir);
if (!tempDirectory.exists()) {
    tempDirectory.mkdirs();
}

// Save the file to the temporary upload directory
String tempFilePath = tempUploadDir + File.separator + file.getOriginalFilename();
file.transferTo(new File(tempFilePath));

// Move the file to the final destination
String finalFilePath = uploadDir + File.separator + file.getOriginalFilename();
new File(tempFilePath).renameTo(new File(finalFilePath));

return ResponseEntity.ok("File uploaded successfully");

Submit Method

This method utilizes the functionality of the upload method, but implements the AssignmentSubmission object and the JWT for user data. Some unique code is shown below:

@PostMapping("/submit/{id}/{submissionTimeString}")
public ResponseEntity<Object> handleFileUpload(@CookieValue("jwt") String jwtToken,
                                               @RequestPart("file") MultipartFile file,
                                               @PathVariable long id,
                                               @PathVariable String submissionTimeString) {
    // jwt processing for user info
    if (jwtToken.isEmpty()) {
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }
    // getting user data
    String userEmail = tokenUtil.getUsernameFromToken(jwtToken);
    Person existingPerson = personRepository.findByEmail(userEmail);
    if (existingPerson == null) {
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }
    // determining the relevant assignment and its relation to user
    boolean userInClasses = false;
    Assignment submittedAssignment = assignmentDetailsService.get(id); // getting requested assignment
    List<ClassPeriod> classesWithAssignment = classService.getClassPeriodsByAssignment(submittedAssignment); // classes with assignment
    for (ClassPeriod classPeriod : classesWithAssignment) {
        for (Person student : classPeriod.getStudents()) {
            if (student.getEmail().equals(existingPerson.getEmail())) {
                userInClasses = true; // user is in a class period with the given assignment
            }
        }
    }
    if (!userInClasses) { // if the user is not in the necessary class
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }
    // checking the submission number
    int submissionNumber = 1;
    for (AssignmentSubmission submission : submittedAssignment.getSubmissions()) {
        if (submission.getSubmitter().getEmail().equals(existingPerson.getEmail())) {
            if (submission.getSubmissionNumber() >= submissionNumber) {
                submissionNumber = submission.getSubmissionNumber() + 1;
            }
        }
    }

Demo

OBS wasn't working, so we have some screenshot demos.

Initial SQL Table

Screen Shot 2024-04-18 at 1 12 18 AM

File Upload

Signed in as "jm1021@gmail.com", the file "python code image.png" will be uploaded to the assignment "Teddy's Big Bready":

Screen Shot 2024-04-18 at 1 18 31 AM

SQL Tables After Upload

This is the AssignmentSubmission successfully uploaded:

Screen Shot 2024-04-18 at 1 19 20 AM