Skip to content

[ENHANCEMENT] Backend API & MongoDB Database IntegrationΒ #128

@shrixtacy

Description

@shrixtacy

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions