Backend API & MongoDB Database Integration
Level: π΄ Hard
Contributor: @shrixtacy β Apertre 3.0
π Summary
CITY-STYLE has no database and no real backend API. The only server-side code is a minimal Express newsletter handler that stores subscribers in a flat JSON file. This issue proposes setting up a MongoDB database with a full REST API to power authentication, products, cart, orders, and reviews.
Current State
- Express server exists (server/index.js) but only serves
/api/newsletter and /api/health
- No database β zero MongoDB, zero Mongoose
- Products are hardcoded JS objects in ProductDetail.jsx and Home.jsx
- Cart, orders, reviews, user profiles β all non-existent on the backend
- No API service layer on the frontend
π Proposed Changes
1. MongoDB Setup & Schema Design
| Schema |
Key Fields |
User |
name, email, passwordHash, avatar, role (user/admin), addresses[], wishlist[] |
| Product |
name, slug, description, price, originalPrice, images[], category, sizes[], colors[], stock, ratings, reviewCount |
Cart |
userId, items[{ productId, quantity, size, color }] |
Order |
userId, items[], shippingAddress, paymentStatus, orderStatus, total, timestamps |
| Review |
userId, productId, rating, title, content, verified, createdAt |
2. Backend Structure
server/
βββ config/
β βββ db.js # MongoDB connection
βββ models/
β βββ User.js
β βββ Product.js
β βββ Cart.js
β βββ Order.js
β βββ Review.js
βββ controllers/
β βββ authController.js
β βββ productController.js
β βββ cartController.js
β βββ orderController.js
β βββ reviewController.js
βββ routes/
β βββ auth.js
β βββ products.js
β βββ cart.js
β βββ orders.js
β βββ reviews.js
β βββ newsletter.js # existing
βββ middleware/
β βββ authMiddleware.js # JWT verification
β βββ adminMiddleware.js # Admin role check
βββ scripts/
β βββ seed.js # Seed initial product data
βββ index.js # existing, updated with new routes
3. API Endpoints
Auth (/api/auth)
| Method |
Endpoint |
Description |
| POST |
/register |
Register with email/password |
| POST |
/login |
Login, returns JWT |
| POST |
/google |
Google OAuth β create/find user |
| GET |
/me |
Get current user (protected) |
Products (/api/products)
| Method |
Endpoint |
Description |
| GET |
/ |
List all (filter, sort, paginate) |
| GET |
/:slug |
Single product |
| POST |
/ |
Create (admin) |
| PUT |
/:id |
Update (admin) |
| DELETE |
/:id |
Delete (admin) |
Cart (/api/cart) β all protected
| Method |
Endpoint |
Description |
| GET |
/ |
Get user's cart |
| POST |
/ |
Add item |
| PUT |
/:itemId |
Update quantity |
| DELETE |
/:itemId |
Remove item |
Orders (/api/orders) β all protected
| Method |
Endpoint |
Description |
| POST |
/ |
Place order |
| GET |
/ |
User's order history |
| GET |
/:id |
Order details |
Reviews (/api/reviews)
| Method |
Endpoint |
Description |
| GET |
/product/:productId |
Get reviews for a product |
| POST |
/ |
Submit review (protected) |
4. Frontend API
Backend API & MongoDB Database Integration
Level: π΄ Hard
Contributor: @shrixtacy β Apertre 3.0
π Summary
CITY-STYLE has no database and no real backend API. The only server-side code is a minimal Express newsletter handler that stores subscribers in a flat JSON file. This issue proposes setting up a MongoDB database with a full REST API to power authentication, products, cart, orders, and reviews.
Current State
/api/newsletterand/api/healthπ Proposed Changes
1. MongoDB Setup & Schema Design
UserCartOrder2. Backend Structure
3. API Endpoints
Auth (
/api/auth)/register/login/google/meProducts (
/api/products)//:slug//:id/:idCart (
/api/cart) β all protected///:itemId/:itemIdOrders (
/api/orders) β all protected///:idReviews (
/api/reviews)/product/:productId/4. Frontend API