Skip to content

smart inventory app for cellphone stores that keeps stock, sales, and orders perfectly in sync.

License

Notifications You must be signed in to change notification settings

0xYurii/CellSync

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“± CellSync - Phone Inventory Management API

Node.js Express PostgreSQL License

A full-stack RESTful API for managing phone inventory with complete CRUD operations

Features β€’ Installation β€’ Usage β€’ API Docs β€’ Testing


πŸ“‹ Table of Contents


🌟 Overview

CellSync is a modern, full-featured phone inventory management system built with Node.js, Express, and PostgreSQL. It provides a complete RESTful API with all CRUD operations (Create, Read, Update, Delete) and includes a beautiful web interface for easy testing and management.

Perfect for:

  • πŸ“± Mobile phone retailers
  • πŸͺ E-commerce platforms
  • πŸ“Š Inventory management systems
  • πŸŽ“ Learning REST API development
  • πŸš€ Prototyping and MVP development

✨ Features

πŸ”„ Complete CRUD Operations

  • βœ… Create - Add new phones to inventory
  • βœ… Read - View all phones, get specific phone, or search with filters
  • βœ… Update - Modify existing phone records
  • βœ… Delete - Remove phones from inventory

πŸ” Advanced Search

  • Filter by brand (Apple, Samsung, Google, etc.)
  • Filter by price range (min/max)
  • Filter by storage capacity
  • Combine multiple filters

πŸ›‘οΈ Best Practices

  • RESTful API design
  • SQL injection prevention (parameterized queries)
  • Error handling
  • ES6 modules
  • Environment variables
  • Clean code architecture

πŸ› οΈ Tech Stack

Backend:

  • Node.js - JavaScript runtime
  • Express.js - Web framework
  • PostgreSQL - Relational database
  • pg - PostgreSQL client
  • dotenv - Environment variable management

Frontend:

  • HTML5 - Markup
  • CSS3 - Styling (gradients, animations)
  • JavaScript (ES6+) - Fetch API, async/await

Development:

  • Nodemon - Auto-restart on file changes

Steps

  1. Clone the repository
git clone https://github.com/0xYurii/cellsync.git
cd cellsync
  1. Install dependencies
npm install
  1. Create environment file
cp .env.example .env
  1. Configure your .env file
DB_HOST=localhost
DB_USER=postgres
DB_DATABASE=cellsync
DB_PASSWORD=your_password
DB_PORT=5432

πŸ’Ύ Database Setup

1. Create the Database

# Using psql
psql -U postgres
CREATE DATABASE cellsync;
\q

Or use the command directly:

createdb cellsync -U postgres

2. Create the Tables

CREATE TABLE phones (
  id SERIAL PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  brand VARCHAR(100) NOT NULL,
  price DECIMAL(10, 2) NOT NULL,
  stock_quantity INTEGER NOT NULL,
  color VARCHAR(50) NOT NULL,
  storage VARCHAR(50) NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

3. Add Sample Data (Optional)

INSERT INTO phones (name, brand, price, stock_quantity, color, storage) VALUES
  ('iPhone 15 Pro', 'Apple', 999.99, 12, 'Titanium Blue', '256GB'),
  ('Galaxy S24 Ultra', 'Samsung', 1199.99, 8, 'Phantom Black', '512GB'),
  ('Pixel 8 Pro', 'Google', 899.99, 15, 'Obsidian', '128GB'),
  ('iPhone 14', 'Apple', 799.99, 20, 'Purple', '128GB'),
  ('Galaxy A54', 'Samsung', 449.99, 25, 'Awesome Violet', '128GB'),
  ('OnePlus 12', 'OnePlus', 799.99, 10, 'Flowy Emerald', '256GB'),
  ('Xiaomi 13 Pro', 'Xiaomi', 699.99, 7, 'Ceramic White', '256GB');

Or use the provided setup file:

psql -U postgres -d cellsync -f db/setup.sql

πŸš€ Usage

Start the Development Server

npm run dev

The server will start on http://localhost:3000


πŸ“š API Documentation

Endpoints Overview

Method Endpoint Description
POST /phones/insert Create a new phone
GET /phones Get all phones
GET /phones/:id Get a specific phone by ID
GET /phones/search Search phones with filters
PUT /phones/:id Update a phone
DELETE /phones/:id Delete a phone

Detailed Examples

1. Create a Phone

POST /phones/insert
Content-Type: application/json

{
  "name": "iPhone 15 Pro",
  "brand": "Apple",
  "price": 999.99,
  "stock_quantity": 15,
  "color": "Titanium Blue",
  "storage": "256GB"
}

Response: 201 Created

Phone inserted successfully

2. Get All Phones

GET /phones

Response: 200 OK - HTML list of all phones

3. Get Phone by ID

GET /phones/1

Response: 200 OK

{
  "id": 1,
  "name": "iPhone 15 Pro",
  "brand": "Apple",
  "price": "999.99",
  "stock_quantity": 12,
  "color": "Titanium Blue",
  "storage": "256GB"
}

4. Search Phones

GET /phones/search?brand=Apple
GET /phones/search?minPrice=500&maxPrice=800
GET /phones/search?storage=256GB
GET /phones/search?brand=Apple&minPrice=700&maxPrice=1000

Response: 200 OK - HTML list of matching phones

5. Update a Phone

PUT /phones/1
Content-Type: application/json

{
  "name": "iPhone 15 Pro Max",
  "brand": "Apple",
  "price": 1199.99,
  "stock_quantity": 20,
  "color": "Natural Titanium",
  "storage": "512GB"
}

Response: 200 OK

{
  "message": "Phone updated successfully",
  "phone": { ... }
}

6. Delete a Phone

DELETE /phones/1

Response: 200 OK

{
  "message": "Phone deleted successfully",
  "phone": { ... }
}

For complete API documentation, see API_DOCUMENTATION.md


πŸ§ͺ Testing

Method 1: Web Interface (Recommended)

  1. Start the server: npm run dev
  2. Open browser: http://localhost:3000/
  3. Use the tabs to test each operation:
    • βž• Create
    • πŸ“‹ Read All
    • πŸ” Search
    • ✏️ Update
    • πŸ—‘οΈ Delete

Method 2: Browser (GET requests only)

Method 3: Command Line (curl)

Create:

curl -X POST http://localhost:3000/phones/insert \
  -H "Content-Type: application/json" \
  -d '{"name":"Test Phone","brand":"TestBrand","price":599.99,"stock_quantity":10,"color":"Black","storage":"128GB"}'

Read:

curl http://localhost:3000/phones
curl http://localhost:3000/phones/1
curl "http://localhost:3000/phones/search?brand=Apple"

Update:

curl -X PUT http://localhost:3000/phones/1 \
  -H "Content-Type: application/json" \
  -d '{"name":"Updated Phone","brand":"Apple","price":899.99,"stock_quantity":15,"color":"Blue","storage":"256GB"}'

Delete:

curl -X DELETE http://localhost:3000/phones/1

πŸ“ Project Structure

CellSync/
β”œβ”€β”€ controllers/              # Business logic layer
β”‚   β”œβ”€β”€ getPhones.js         # READ operations (all, search)
β”‚   β”œβ”€β”€ insertPhone.js       # CREATE operation
β”‚   β”œβ”€β”€ updatePhone.js       # UPDATE & get by ID
β”‚   └── deletePhone.js       # DELETE operation
β”œβ”€β”€ db/                      # Database layer
β”‚   β”œβ”€β”€ pool.js             # PostgreSQL connection pool
β”‚   β”œβ”€β”€ queries.js          # All database queries
β”‚   └── setup.sql           # Database schema
β”œβ”€β”€ routes/                  # API routes
β”‚   └── getphones.js        # All CRUD endpoints
β”œβ”€β”€ public/                  # Static files
β”‚   └── test.html           # Interactive test interface
β”œβ”€β”€ node_modules/           # Dependencies
β”œβ”€β”€ .env                    # Environment variables (create this)
β”œβ”€β”€ .gitignore             # Git ignore file
β”œβ”€β”€ index.js               # Server entry point
β”œβ”€β”€ package.json           # Project metadata
β”œβ”€β”€ API_DOCUMENTATION.md   # Complete API reference
β”œβ”€β”€ CRUD_SUMMARY.md        # Quick reference guide
└── README.md              # This file

πŸ—οΈ Architecture

MVC Pattern (Modified)

Models (db/queries.js)

  • Database query functions
  • Data validation
  • SQL operations

Controllers (controllers/*)

  • Business logic
  • Request/response handling
  • Error handling

Routes (routes/*)

  • Endpoint definitions
  • HTTP method mapping
  • Route organization

Views (public/*)

  • User interface
  • Interactive testing

πŸ”’ Security Features

  • βœ… Parameterized queries - Prevents SQL injection
  • βœ… Environment variables - Sensitive data protection
  • βœ… Input validation - Data integrity
  • βœ… Error handling - Secure error messages
  • βœ… CORS ready - Cross-origin support (if needed)

⭐ Star this repo if you find it helpful! ⭐

Made with ❀️ by developers, for developers

Report Bug β€’ Request Feature


About

smart inventory app for cellphone stores that keeps stock, sales, and orders perfectly in sync.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published