3 RESTful Interfaces

RESTful Interfaces

Deadline: Monday, April 27, 2026.

Plagiarism is theft and is unacceptable. It undermines creativity, damages intellectual integrity, and destroys the purpose of learning. This also applies to contract cheating (opens in a new tab) and the mindless use of chatbots/agents.1

ER diagram for overall application designDeckSlideCardCardType
api
:deck
:slide
:card

Requirements

Continuing the flashcards application, use Next.js and Prisma to implement an API based on the class hierarchy and data repositories of 2 Client Rendering.

Models

Create a Next.js application, then install Prisma and initialize it with a SQLite data source provider. Define the following models in prisma/schema.prisma and use cuid(2) to generate the identifiers:

ER diagram for Deck, Slide, and Card modelsDeckid: Stringtitle: StringSlideid: Stringtitle: Stringdeck: StringCardid: Stringtype: CardTypetags: Jsondata: Jsonslide: String

Define a seeding script, prisma/seed.mjs, to create all valid card types. CardType is conceptually an enumeration but is defined as a model instead:

ER diagram for Card model and CardType enumCardtype: CardTypeCardTypetype: String

Create the database using a migration, seed it, and verify that it has been created and seeded successfully using Prisma Studio.

Endpoints

Define the following routes and methods to manage the collection of decks, slides, and cards:

MethodURLDescription
GET/apiReturn all the decks in the collection.
POST/apiCreate a deck based on the request body and return it.
DELETE/api/:deckDelete the deck having the provided identifier if empty.
GET/api/:deckReturn all the slides in a deck.
POST/api/:deckCreate a slide based on the request body and return it.
DELETE/api/:deck/:slideDelete the slide having the provided identifier if empty.
GET/api/:deck/:slideReturn all the cards in a slide.
POST/api/:deck/:slideCreate a card based on the request body and return it.
DELETE/api/:deck/:slide/:cardDelete the card having the provided identifier.
PATCH/api/:deck/:slide/:cardUpdate the card having the provided identifier based on the request body.

Do not access the database directly in the route methods. Instead, use CRUD repositories with corresponding methods for all data operations. Each route method should require only one repository method call.

Respond with the correct status code (opens in a new tab) for successful requests (2xx). For invalid requests, respond with an error status code (4xx, 5xx) and a message. Examples of invalid requests include:

  • Accessing a non-existent resource.
  • Deleting a non-empty deck or slide.
  • Deleting a card from a slide that does not contain it.

You can use the Prisma error code (opens in a new tab) to determine the type of error that occurred in the repository method and respond accordingly. Error details are provided by the repository methods (see Repositories below).

app/api/*/route.js
export async function METHOD(request, { params }) {
  try {
    const { ••• } = await params;
    const result = await •••
    if ("error" in result) {
      return Response.json(•••);
    }
    return Response.json(•••);
  } catch (e) {
    return Response.json(•••);
  }
}

Repositories

Create separate data repository modules under repos for decks, slides, and cards, respectively. These repository modules import Prisma Client and use its CRUD operations (opens in a new tab) to access the underlying database, implementing only the methods needed by the route methods.

Repository methods return the result of their database queries. When a query fails, an exception is thrown by Prisma Client with a corresponding error code (opens in a new tab). Use this code to return an error object, which the route method then uses to respond accordingly.

repos/*.js
export async function method(•••) {
  try {
    const verification = await •••
    const result = await •••
    return {
      data: •••
    };
  } catch (e) {
    return {
      error: {
        message: •••,
        status: •••
      }
    };
  }
}

Despite the fact that each deck, slide, and card is uniquely identified by its cuid, every repository method must verify ownership before executing its query:

  • Every method in repos/slides.js must verify that the slide belongs to the specified deck.
  • Every method in repos/cards.js must verify that the card belongs to the specified slide and that the slide belongs to the specified deck.

An error is returned when verification fails. This ensures that each resource is accessed using a single unique URL given our route interface.

Testing

Test all ten route methods using Postman and export the collection to postman.json.

A client application extending 2 Client Rendering with calls to the API is provided in the classroom repository (opens in a new tab) under public. Copy it to the public directory of your application and use it to test your implementation.

Guidelines

  1. Push your solution to your private repository under assignments/03-restful-interfaces.
  2. Commit often and use meaningful message summaries and descriptions.
  3. Complete your work before the deadline; no late submissions.

Codebase

The following structure should be used to organize the codebase. There is no need to create additional top-level directories/files, but more directories/files can be created under api and results.

        • ...
        • dev.db
          • migration.sql
        • migration_lock.toml
      • schema.prisma
      • seed.mjs
      • cards.js
      • decks.js
      • prisma.js
      • slides.js
    • .env
    • .gitignore
    • biome.json
    • jsconfig.json
    • next.config.mjs
    • package.json
    • postman.json
    • prisma.config.mjs
    • readme.md
  • Report

    Include screenshots of testing with Postman under results and push them along with the assignment.

    Complete the readme.md report and push it along with the assignment.

    readme.md
    # Report
     
    Xane Doe [email protected]
     
    ## 3 RESTful Interfaces
     
    | Task         | Done? | Comments             |
    | :----------- | :---- | :------------------- |
    | Schema       | [ ]   |                      |
    | Migration    | [ ]   |                      |
    | Endpoints    | [ ]   |                      |
    | Validation   | [ ]   |                      |
    | Repositories | [ ]   |                      |
    | Verification | [ ]   |                      |
    | Errors       | [ ]   |                      |
    | Testing      | [ ]   |                      |
    | Export       | [ ]   |                      |
    | Screenshots  | [ ]   |                      |
    | Report       | [x]   | Markdown is the way. |
    | Plagiarism   | [ ]   |                      |

    Rubric

    TaskPointsDetails
    Models+20Prisma Schema. Prisma Migrate
    Endpoints+30Routes/Methods, Requests/Responses, Validation
    Repositories+30Prisma Client, CRUD subset, Verification, Errors
    Testing+20Prisma Studio, Postman Collection
    Screenshots+5
    Quality+5Clean, structured, well-organized, indented code
    Report-20Evaluation, Comments
    Plagiarism-∞
    Total110

    Footnotes

    1. Student Code of Conduct Policy (opens in a new tab) / Article (1) — سياسة النظام الطلابي (opens in a new tab) \ البند (١).