WS 101 study hub

WS 101 · Pre-final · Module 8 · ~75–90 min

Student Marketplace → Ship

Photos on Cloudinary. App on Vercel. A URL you can open on your phone.

Your progress

0 / 10

Before you start

Check these off so you’re ready to ship.

Not today

Do not use GitHub Pages — this is a Next.js app, so the host is Vercel. Never commit .env. Search and login wait until the live URL works.

STEP 1

Cloudinary + upload route

  1. 1

    Sign up at cloudinary.com. Copy Cloud Name, API Key, and API Secret into .env.

  2. 2

    Run npm install cloudinary.

  3. 3

    Create app/api/upload/route.ts (not route.js) — receive a file, upload it, return { url }.

.envnever commit this file
CLOUDINARY_CLOUD_NAME=your-cloud-name
CLOUDINARY_API_KEY=your-api-key
CLOUDINARY_API_SECRET=your-api-secret
app/api/upload/route.tsreturns { url }
import { v2 as cloudinary } from 'cloudinary';

cloudinary.config({
  cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
  api_key: process.env.CLOUDINARY_API_KEY,
  api_secret: process.env.CLOUDINARY_API_SECRET,
});

export async function POST(request: Request) {
  const formData = await request.formData();
  const file = formData.get('file');

  if (!(file instanceof File)) {
    return Response.json({ error: 'No file' }, { status: 400 });
  }

  const buffer = Buffer.from(await file.arrayBuffer());

  const result = await new Promise<{ secure_url: string }>((resolve, reject) => {
    cloudinary.uploader.upload_stream(
      { folder: 'marketplace' },
      (error, uploaded) => {
        if (error || !uploaded) reject(error);
        else resolve(uploaded);
      }
    ).end(buffer);
  });

  return Response.json({ url: result.secure_url });
}

Vercel has no folder for uploads. The app stores a URL, not a file.

STEP 2

Save and show the photo

  1. 1

    In app/api/listings/route.ts, read imageUrl from the body and pass it into prisma.listing.create. Module 7 already made the column — you just have to write it.

  2. 2

    Update ListingCard so it takes an imageUrl prop and uses <img src={imageUrl} /> instead of a hardcoded placeholder.

  3. 3

    On home (and detail if you show a photo there), pass imageUrl={item.imageUrl} into ListingCard.

  4. 4

    On app/post/page.tsx, add imageFile state and a file input — that is where imageFile comes from.

  5. 5

    Inside handleSubmit: if a file was chosen, POST /api/upload first, then POST /api/listings with that URL. If they skip the file, keep the placeholder.

app/api/listings/route.tsadd imageUrl to create
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),
      imageUrl: body.imageUrl,
    },
  });

  return Response.json(listing, { status: 201 });
}
components/ListingCard.tsxprop instead of placeholder
type CardProps = {
  title: string;
  price: number;
  description: string;
  imageUrl: string;
};

export default function ListingCard({ title, price, description, imageUrl }: CardProps) {
  return (
    <div>
      <img src={imageUrl} alt={title} />
      <h3>{title}</h3>
      <p>₱{price}</p>
      <p>{description}</p>
    </div>
  );
}
app/page.tsxpass the URL from the row
<ListingCard
  title={item.title}
  price={item.price}
  description={item.description}
  imageUrl={item.imageUrl}
/>
app/post/page.tsxfile input + state
const [title, setTitle] = useState('');
const [price, setPrice] = useState('');
const [description, setDescription] = useState('');
const [imageFile, setImageFile] = useState<File | null>(null);

// inside the form, with title / price / description:
<label>
  Photo (optional)
  <input
    type="file"
    accept="image/*"
    onChange={(e) => setImageFile(e.target.files?.[0] ?? null)}
  />
</label>
app/post/page.tsxupload then save
async function handleSubmit(e: React.FormEvent) {
  e.preventDefault();

  let imageUrl = 'https://placehold.co/300x200';

  if (imageFile) {
    const uploadData = new FormData();
    uploadData.append('file', imageFile);
    const uploadRes = await fetch('/api/upload', { method: 'POST', body: uploadData });
    const uploadResult = await uploadRes.json();
    imageUrl = uploadResult.url;
  }

  await fetch('/api/listings', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ title, price, description, imageUrl }),
  });

  router.push('/');
}

Order matters: upload → get url → save the listing. If the photo uploads but the card stays a placeholder, you forgot imageUrl on create or on ListingCard.

STEP 3

Check localhost + Prisma generate

  1. 1

    Home and detail already call Prisma (Module 7). The form already uses relative /api/listings. Both work on Vercel.

  2. 2

    Search the project for localhost:3000. Delete any fetch('http://localhost:3000/...').

  3. 3

    Add "postinstall": "prisma generate" in package.json so Vercel can build.

package.jsonVercel build
"scripts": {
  "dev": "next dev",
  "build": "next build",
  "postinstall": "prisma generate"
}

If the live home page is empty, you still hardcoded localhost — or you forgot DATABASE_URL on Vercel.

STEP 4

GitHub + Vercel

  1. 1

    Run git status — .env must not be listed. Commit and push.

  2. 2

    Go to vercel.com, sign in with GitHub, Import the repo.

  3. 3

    Paste DATABASE_URL and the three Cloudinary variables before you click Deploy.

  4. 4

    Open the live URL on a laptop and on a phone. Post a listing with a photo. Refresh.

Vercel envpaste all four
DATABASE_URL
CLOUDINARY_CLOUD_NAME
CLOUDINARY_API_KEY
CLOUDINARY_API_SECRET

No npm run dev on the phone. If classmates see your listing, you shipped.

Done checklist

Tick everything before you submit.

Submit

Turn in the live URL. GitHub Pages is the wrong host.

DeliverableWhat to turn in
Live siteYour Vercel URL
RepoGitHub URL — without .env

Final challenge: send the URL to one classmate and ask them to post. If you see their listing, the app works end to end.

Still posting only on localhost? Finish Module 7 first then come back.