How to work with associative table with additional attributes in REST?

4

Imagine that you have 3 entities: Student, Discipline and Student Discipline.

The AlunoDisciplina entity, in addition to relating Aluno to Disciplina , also includes other information, such as the student's final grade. So basically we have something like this:

class AlunoDisciplina {
    Aluno aluno;
    Disciplina disciplina;
    double notaFinal;
}

As the above entity is exposed, aluno and disciplina compose a composite primary key, since a student can not be associated with the same discipline more than once.

Some operations I need to implement:

  • List of students enrolled in the course (with their notes);
  • List of courses in which the student is enrolled (with the respective grades);
  • Add / Remove a student from a course;
  • Edit the student's final grade in the course.
  • How can I expose this relationship in a RESTful way?

    What I thought so far

    For items 1 and 2, respectively, I envision the features below.

    (GET) / disciplines / {idDiscipline} /children.json

    {
        "alunosMatriculados": [{
            "aluno": {
                "id": 2,
                "nome": "Pedro"
            },
            "notaFinal": 9.5
        }, {
            "aluno": {
                "id": 6,
                "nome": "Maria"
            },
            "notaFinal": 10
        }]
    }
    

    (GET) / students / {StudentName} /disciplines.json

    {
        "matriculadoNasDisciplinas": [{
            "disciplina": {
                "id": 1,
                "nome": "Matemática"
            },
            "notaFinal": 9.5
        }, {
            "disciplina": {
                "id": 2,
                "nome": "Filosofia"
            },
            "notaFinal": 7
        }]
    }
    

    For items 3 and 4 I'm not finding the best way to solve.

    The best solution would be this same or is there any other better way?

    Do you have any suggestions for items 3 and 4?

        
    asked by anonymous 25.02.2015 / 20:53

    1 answer

    3

    I believe you can simplify the returns of methods 1 and 2, returning only one list instead of a complex object, I believe this would simplify your API

    (GET) / disciplines / {idDiscipline} /children.json

    [
        {
            "id": 2,
            "nome": "Pedro"
            "notaFinal": 9.5,
        },
        {
            "id": 6,
            "nome": "Maria"
            "notaFinal": 10
        }
    ]
    

    (GET) / students / {StudentName} /disciplines.json

    [
        {
            "id": 1,
            "nome": "Matemática",
            "notaFinal": 9.5
        },
        {
            "id": 2,
            "nome": "Filosofia",
            "notaFinal": 7
        }
    ]
    

    To remove, add one, or update a note, you can use the same URL with different Method Definitions.

    (POST) /alunos/disciplinas/
    C. Body: idAluno=?&idDisciplina=? 
    (DELETE) /alunos/disciplinas/
    C. Body: idAluno=?&idDisciplina=? 
    (PUT) /alunos/disciplinas/
    C. Body: idAluno=?&idDisciplina=?&notaFinal=?
    

    Remembering this is just a suggestion

        
    25.02.2015 / 21:06