John-sCC / jcc_backend

Eat up!
1 stars 0 forks source link

Assignment Object - Minor Functionality Update to Assignment Creation #40

Closed drewreed2005 closed 3 months ago

drewreed2005 commented 3 months ago

Overview

Because ClassPeriod names are not always necessarily unique (makes for better functionality since they are still differentiated by ID, students, teachers, etc.), Drew made minor changes that make it so that class IDs are used for Assignment creation and assignment to class period, not class period names.

Details Service Method Update

This was the main source of the update. Rather than the classPeriod name being used as the identifier in the addAssignmentToClassPeriod details service method, it has been changed to the long id of the classPeriod in the database (this is more unique):

public void addAssignmentToClass(long assignmentId, long classId) { // UPDATED PARAMETER
        Assignment assignment = assignmentJpaRepository.findById(assignmentId); // uses findById now
        if (assignment != null) {
            ClassPeriod classPeriod = classPeriodJpaRepository.findById(classId); // also uses findById
            if (classPeriod != null) {
                boolean addAssignment = true;
                // blah blah blah blah

Updated AssignmentRequest Object and POST Request

This is the new form of the object used for the /post assignment request (see comments):

    private int points;
    private int allowedSubmissions;
    private String content;
    private long[] classIds; // updated variable
    private String[] allowedFileTypes;

    public AssignmentRequest(String name, Date dateCreated, Date dateDue, String content, int points, int allowedSubmissions, long[] classIds, String[] allowedFileTypes) {
        this.name = name;
        this.dateCreated = dateCreated;
        this.dateDue = dateDue;
        this.content = content;
        this.points = points;
        this.allowedSubmissions = allowedSubmissions;
        this.classIds = classIds; // updated variable
        this.allowedFileTypes = allowedFileTypes;
    } // updated setter and getter below

In the corresponding POST method, here is the new form:

        for (long classId : request.getClassIds()) { // ITERATES THROUGH CLASS IDs NOW
            if (classService.get(classId) == null) {
                return new ResponseEntity<>("One or more classes was invalid", HttpStatus.BAD_REQUEST);
            }
        }
        // A assignment object WITHOUT ID will create a new record with default roles as student
        Assignment assignment = new Assignment(request.getName(), request.getDateCreated(), request.getDateDue(), request.getContent(), request.getPoints(), request.getAllowedSubmissions(), request.getAllowedFileTypes());
        boolean saved = false;
        for (long classId : request.getClassIds()) { // ITERATES THROUGH CLASS IDs NOW
            if (classService.get(classId).getLeaders().contains(existingPerson)) { // the "get" method is used now with IDs
                if (!(saved)) {
                    assignmentDetailsService.save(assignment);
                    saved = true;
                }
                classService.addAssignmentToClass(assignment.getId(), classId); // details service method has been updated too
            }