StaffCoder
Spring Boot
Spring BootSpring Boot 4.1Java 25
12 modules · 48 challengesSign up
Module 8 · File Handling and Storage

Lecture Slide Repository

medium—No data yet

Problem statement

Implement a lecture slide repository that stores uploaded PDF slide decks per course in a relational database and serves them back as downloads.

Requirements

SlideService

The service owns every read and write of the repository. Courses are plain numeric ids with no records of their own, so any courseId is a valid scope. Implement its three methods:

  • store validates an uploaded file for the given course, rejecting empty and non-PDF uploads with the exact errors listed under the upload endpoint, stores an accepted file with its content, original filename, and size, and returns the stored record
  • getSlideDecks returns the stored decks of the given course
  • getSlideDeck returns the stored deck with the given id, or raises the not-found failure listed under the download endpoint

SlideController

Each endpoint must be reachable at exactly the verb and path listed below.

POST /courses/{courseId}/slides

  • Accept a file uploaded as form data in a part named file
  • If the uploaded file contains no bytes, return 400 Bad Request with error message: "File is required"; the empty check comes first, so an empty upload is answered with this error whatever its declared type
  • If the file's declared content type is anything other than application/pdf, return 400 Bad Request with error message: "Only PDF decks are allowed"
  • A rejected upload must not store anything
  • On a valid upload, store the deck and return 201 Created with a body carrying exactly id, courseId, filename, and sizeBytes; filename is the original name of the uploaded file, preserved exactly, and sizeBytes is the number of bytes stored
  • Any non-empty file declared as application/pdf is accepted, including a file of a single byte
  • A stored deck appears in its own course's list and in no other

GET /courses/{courseId}/slides

  • Return 200 OK with the decks of that course only; decks of other courses never appear
  • Each entry carries exactly id, filename, and sizeBytes, never the file content and never courseId
  • Return 200 OK with an empty array when the course has no decks, including a course id that has never been used; this endpoint never returns an error status

GET /slides/{id}/download

  • Return 200 OK with the stored file content as the response body, byte-identical to what was uploaded
  • The response carries the Content-Disposition header announcing an attachment, with the original filename in double quotes: for a deck stored as lecture-notes.pdf the exact value is attachment; filename="lecture-notes.pdf"
  • The response Content-Type is application/pdf
  • If no deck with the given id exists, return 404 Not Found. The error message is "Slide deck not found with id: " followed by the requested id
  • Listing and downloading never modify the stored decks

Sample Requests & Responses

Expand to view details on sample requests and responses.

POST /courses/4/slides

Request:

curl -X POST http://localhost:8000/courses/4/slides -F "file=@lecture-notes.pdf;type=application/pdf"

Response (201 Created):

{
    "id": 1,
    "courseId": 4,
    "filename": "lecture-notes.pdf",
    "sizeBytes": 51
}

POST /courses/9/slides

Request:

curl -X POST http://localhost:8000/courses/9/slides -F "file=@Week 3 - Thermodynamics.pdf;type=application/pdf"

Response (201 Created):

{
    "id": 2,
    "courseId": 9,
    "filename": "Week 3 - Thermodynamics.pdf",
    "sizeBytes": 58
}

GET /courses/4/slides

Response (200 OK):

[
    {
        "id": 1,
        "filename": "lecture-notes.pdf",
        "sizeBytes": 51
    },
    {
        "id": 3,
        "filename": "week-2-recursion.pdf",
        "sizeBytes": 78
    }
]

GET /courses/15/slides

Response (200 OK):

[]

POST /courses/4/slides

Request:

curl -X POST http://localhost:8000/courses/4/slides -F "file=@notes.txt;type=text/plain"

Response (400 Bad Request):

{
    "error": "Only PDF decks are allowed",
    "timestamp": "2026-08-05T21:50:02.483702",
    "path": "/courses/4/slides"
}

POST /courses/4/slides

Request:

curl -X POST http://localhost:8000/courses/4/slides -F "file=@empty.pdf;type=application/pdf"

Response (400 Bad Request):

{
    "error": "File is required",
    "timestamp": "2026-08-05T21:50:02.645891",
    "path": "/courses/4/slides"
}

GET /slides/1/download

Response (200 OK):

HTTP/1.1 200 
Content-Disposition: attachment; filename="lecture-notes.pdf"
Content-Type: application/pdf
Content-Length: 51

%PDF-1.4
1 0 obj
<< /Type /Catalog >>
endobj
%%EOF

GET /slides/999/download

Response (404 Not Found):

{
    "error": "Slide deck not found with id: 999",
    "timestamp": "2026-08-05T21:50:02.827916",
    "path": "/slides/999/download"
}

More in File Handling and Storage

See the module
Passport Photo UploadThis problem covers accepting an uploaded image, checking it before storing it, and serving the original bytes back later.—No data yetmediumCSV Bulk Import HandlerThis problem covers reading an uploaded CSV row by row, storing the good rows and reporting the bad ones.—No data yetmediumScanned Receipt VaultThis problem covers storing uploaded receipts per user, cleaning up the filename and holding each user to a storage limit.—No data yetmedium

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.