const express = require('express');
const { db } = require('./db');
const app = express();
const PORT = 3000;

// Get all movies
app.get('/movies', (req, res) => {
  const rows = db.prepare('SELECT * FROM movies').all();
  res.json(rows);
});

// Get a movie by title
app.get('/movie/:title', (req, res) => {
  const row = db.prepare('SELECT * FROM movies WHERE title = ?').get(req.params.title);
  if (row) {
    res.json(row);
  } else {
    res.status(404).json({ error: 'Not found' });
  }
});

app.listen(PORT, () => {
  console.log(`API server running at http://localhost:${PORT}`);
});