Search code examples
jsonrestrestful-urlrestful-architecture

RESTFUL API retrieving elements collection


I am designing a RESTFUL API and I have a dude with one of my operations:

The client app needs to make a request to the server to retrieve a JSON with available questions to the user (based on certain restrictions).

The problem is that:

  • Every user belongs to a client, meaning that client only will see questions that belongs to client's users.

  • A priori, I don't know the id's of such questions.

  • A user can need a question that other user already have.

I have thought of using: v1/questions/next/{numberOfQuestions}

The numberOfQuestions parameters would be optional (1 by default) and the client will be got in server.

Which approach would be the better one?

Thanks!


Solution

  • Get all questions for a user

    GET /v1/clients/{clientId}/users/{userId}/questions
    

    returns:

    {"questions":
      [
        {
          "id": 1,
          "title": "What is brown and barks?",
          "answered": false
        },
        {
          "id": 1,
          "title": "What is brown and makes Moo?",
          "answered": false
        },
        {
          "id": 1,
          "title": "What is brown and makes Meow?",
          "answered": true
        }
      ]
    }
    

    Get only unanswered questions for a user

    GET /v1/clients/{clientId}/users/{userId}/questions?answered=false
    

    returns:

    {"questions":
      [
        {
          "id": 1,
          "title": "What is brown and barks?",
          "answered": false
        },
        {
          "id": 1,
          "title": "What is brown and makes Moo?",
          "answered": false
        }
      ]
    }
    

    Get only one unanswered question for a user

    GET /v1/clients/{clientId}/users/{userId}/questions?answered=false&limit=1
    

    returns:

    {"questions":
      [
        {
          "id": 1,
          "title": "What is brown and barks?",
          "answered": false
        }
      ]
    }