Web Systems & Technologies · Pre-final · Module 07
Overview
01 / 20
module-07-real-data
Real Data
Pre-final · Module 7 — the listing that survives a refresh
2nd Year · BSIT Student Marketplace Neon · Prisma · API routes
Callback
Remember the listing that vanished
In Module 6 the cards look right. The data is still a typed array. Refresh — back to the same three fakes.
MODULE 6
Fixed reuse + structure
Navbar once. ListingCard once. Routes from files.
STILL BROKEN
No persistence
Nobody can add a real listing. Classmates never see your posts.
TODAY
A permanent home
PostgreSQL on Neon. API talks to it. Form writes a row.
The idea
The page asks. The API talks to the database.
🖥
Page
Home and Post form — what the student sees
→
⚙
API route
/api/listings — GET reads, POST writes
→
🗄
Database
Neon PostgreSQL — rows that survive refresh
Two directions
Same route. GET fetches. POST saves.
GET /api/listings
Home page loads → GET /api/listings → Prisma reads the table → JSON back to the page → cards render
POST /api/listings
Student clicks Post → POST /api/listings → Prisma creates a row → JSON of the new listing → redirect home — it is there
Scope
What we are not doing today
SKIP
Cloudinary
Photos stay as a placeholder URL. Real uploads are Module 8.
SKIP
Vercel
The app stays on localhost. A public URL is Module 8.
SKIP
Login
Anyone who can open the form can post. Auth is optional later.
Database
Neon — PostgreSQL without installing Postgres
  • 1Sign up at neon.tech · new project named marketplace
  • 2Copy the connection string — it starts with postgresql://
  • !Treat it like a password. It lives in .env only.
Prisma
Install Prisma 6 and paste the URL
npm install prisma@6 @prisma/client@6 npx prisma init
  • Use version 6 — latest Prisma is a different setup
  • Creates prisma/schema.prisma and a .env file
  • Put your Neon string in DATABASE_URL="..." · never commit .env
Schema
A Listing is a row
model Listing { id Int @id @default(autoincrement()) title String description String price Float imageUrl String @default("https://placehold.co/300x200") createdAt DateTime @default(now()) }
This is not the home-page array. This is the contract with the database.
Migrate
Create the table. Look at it.
npx prisma migrate dev --name init npx prisma studio
  • migrate builds the Listing table on Neon
  • studio opens a spreadsheet of that table — empty is correct
  • If migrate asks to reset, stop and check the connection string first
Folders
New files this module
marketplace/
├── app/
│ ├── page.tsx ← home reads the database
│ ├── post/page.tsx ← form that POSTs
│ ├── listings/[id]/page.tsx ← find one row
│ └── api/listings/route.ts ← GET + POST
├── lib/db.ts ← one Prisma client
├── prisma/schema.prisma
├── prisma/seed.ts ← sample listings (download)
└── .env ← Neon URL, never commit
lib/db.ts
One door into the database
import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); export default prisma;
Import this file everywhere you need listings. Do not copy new PrismaClient() into each page.
API route
app/api/listings/route.ts
import prisma from '@/lib/db'; export async function GET() { const listings = await prisma.listing.findMany({ orderBy: { createdAt: 'desc' } }); return Response.json(listings); } export async function POST(request: Request) { const body = await request.json(); const listing = await prisma.listing.create({ data: { title: body.title, description: body.description, price: parseFloat(body.price) } }); return Response.json(listing); }
Prove it
Post one row from the console
fetch('/api/listings', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ title: 'Test Item', description: 'A test.', price: 100 }) })
  • Open http://localhost:3000/api/listings — you should see JSON, not cards
  • Refresh that URL — the test item is still there
Home page
Throw away the fake array
// app/page.tsx import prisma from '@/lib/db'; export default async function Home() { const listings = await prisma.listing.findMany({ orderBy: { createdAt: 'desc' }, }); // map listings to ListingCard + Link like Module 6 }
The home page reads the table. The form writes through the API. Same database.
Post form
app/post/page.tsx — the form that writes a row
  • 'use client' at the top — this page runs in the browser so the inputs can remember text
  • useState holds title, price, description while they type
  • Submit → POST /api/listings → router.push('/')
  • Add a Post link in the navbar or on the home page to /post
That is the only hook lesson today. No useEffect. No extra state libraries.
Detail page
Posted ids will not match the old array
// app/listings/[id]/page.tsx const { id } = await params; const listing = await prisma.listing.findUnique({ where: { id: parseInt(id) }, });
  • The database issues its own ids. The fake 1, 2, 3 array is gone
  • If it is missing, show “Listing not found” — that is fine
Stuck?
Three errors you will see
NO ROUTE.JS
Create app/api/listings/route.ts. Same idea as page.tsx — the file name Next.js looks for.
PRISMA ADAPTER / CONFIG.TS
That is Prisma 7. Uninstall and run npm install prisma@6 @prisma/client@6 again.
DETAIL SHOWS NOT FOUND
You still have the Module 6 fake array. Use findUnique and await params.
Student Activity
Connect Module 6 to a real database
MUST HAVE
Neon URL in .env · Listing model · lib/db.ts · GET + POST in route.ts · home + detail read the table · /post form · optional prisma/seed.ts
PROOF
Post through the form. Refresh home — still there. Prisma Studio shows the same row.
SUBMIT
GitHub repo URL (no .env) + screenshot of home after refresh + screenshot of Prisma Studio.
Sample data
Seed the marketplace so you can test right away
  • 1Download seed.ts → save it as prisma/seed.ts
  • 2Run npx tsx prisma/seed.ts
  • 3Refresh home — six campus listings appear
  • !deleteMany clears existing rows first — that is intentional
↓ Download seed.ts Also on the activity page under Step 5.
Empty table works. Seeded table lets you click cards and detail pages without typing six forms.
What's Next
You crossed the line
  • ✓A row in PostgreSQL — not browser memory
  • ✓GET reads · POST writes · same /api/listings
  • ✓Refresh keeps the listing
  • →This is the Pre-final checkpoint
  • →Next: Module 8 — Cloudinary + Vercel
That is what separates a page from an application. Questions?
note //