import { Router } from 'express';
import { query } from '../database';

const router = Router();

// 获取所有服装
router.get('/', async (req, res) => {
  try {
    const result = await query('SELECT * FROM cloth');
    res.json(result.rows);
  } catch (error) {
    const err = error as Error;
    res.status(500).json({ error: err.message });
  }
});

// 添加新服装
router.post('/', async (req, res) => {
  try {
    const { name, description, price, size, colors, material } = req.body;
    
    const result = await query(
      `INSERT INTO cloth (name, description, price, size, colors, material) 
       VALUES ($1, $2, $3, $4, $5, $6) 
       RETURNING *`,
      [name, description, price, size, colors, material]
    );
    
    res.status(201).json(result.rows[0]);
  } catch (error) {
    const err = error as Error;
    res.status(400).json({ error: err.message });
  }
});

export default router;