Skip to content

jmamine/user-product-management-system

Repository files navigation

User Product Management System

A Spring Boot backend application for managing users and products with MongoDB integration.

Features

  • User Management: Create users with ID, name, age, email, and password
  • Product Management: Full CRUD operations for products
    • Display all products
    • Fetch product by ID
    • Add new product
    • Delete product by ID
    • Update product information
  • Exception Handling: Proper error handling for all operations
  • CLI Frontend: Simple command-line interface for interacting with the backend
  • Web Interface: Modern, responsive web UI for managing users and products
  • REST API: Full RESTful API endpoints for programmatic access
  • MongoDB Integration: Persistent storage using MongoDB Atlas

Prerequisites

  • Java 17 or higher
  • Maven 3.6 or higher
  • MongoDB Atlas account (or local MongoDB instance)

Setup

  1. Clone the repository (if applicable)

  2. Configure MongoDB connection

    Option 1: Using local properties file (Recommended)

    Create src/main/resources/application-local.properties with your MongoDB Atlas credentials:

    spring.data.mongodb.uri=mongodb+srv://admin:YOUR_PASSWORD@cluster0.swmdp.mongodb.net/user-product-management-db?retryWrites=true&w=majority

    Replace YOUR_PASSWORD with your actual MongoDB Atlas password.

    Note: The application-local.properties file is already in .gitignore to prevent committing sensitive credentials.

    Option 2: Using environment variable (Alternative)

    You can also set the MONGODB_URI environment variable:

    # Windows PowerShell
    $env:MONGODB_URI="mongodb+srv://admin:YOUR_PASSWORD@cluster0.swmdp.mongodb.net/user-product-management-db?retryWrites=true&w=majority"
    
    # Linux/Mac
    export MONGODB_URI="mongodb+srv://admin:YOUR_PASSWORD@cluster0.swmdp.mongodb.net/user-product-management-db?retryWrites=true&w=majority"
  3. Build the project

    mvn clean install
  4. Run the application

    For MongoDB Atlas (using local profile):

    mvn spring-boot:run -Dspring.profiles.active=local

    Note: You can also configure your IDE to use the local profile when running the application.

    For local MongoDB (default):

    mvn spring-boot:run

    The application will start on http://localhost:8080

Usage

Using the Web Interface

  1. Start the backend server (as described above)

  2. Open your web browser and navigate to:

    http://localhost:8080
    
  3. Navigate between sections:

    • Products Tab: Manage products (view, add, edit, delete)
    • Users Tab: Manage users (view, add, edit, delete)
  4. Features:

    • View all items: Automatically loads and displays all products or users
    • Add new items: Click "Add New Product/User" button to open the form
    • Edit items: Click the "Edit" button on any item card
    • Delete items: Click the "Delete" button on any item card (with confirmation)
    • Responsive design: Works on desktop, tablet, and mobile devices

The web interface communicates with the backend via REST API calls and provides a modern, user-friendly interface for all operations.

Using the CLI Frontend

  1. Start the backend server (as described above)

  2. Run the CLI frontend (in a separate terminal):

    First, ensure the project is compiled:

    mvn compile

    Then run the CLI using Maven:

    mvn exec:java -Dexec.mainClass="com.userproduct.cli.CLIFrontend" -Dexec.classpathScope=compile

    Note: Make sure the backend server is running before starting the CLI.

CLI Menu Options

Login Menu:

  1. Create User - Create a new user account
  2. Login - Login with an existing user ID
  3. Exit

Product Management Menu (after login):

  1. Display all products - Show all products in the repository
  2. Fetch product by ID - Get details of a specific product
  3. Add new product - Add a new product to the repository
  4. Delete product by ID - Remove a product from the repository
  5. Update product - Update an existing product's information
  6. Logout - Log out and return to login menu
  7. Exit

Using REST API Directly

The application also exposes REST endpoints:

User Endpoints

  • POST /api/users - Create a new user
  • GET /api/users - Get all users
  • GET /api/users/{id} - Get user by ID
  • PUT /api/users/{id} - Update a user
  • DELETE /api/users/{id} - Delete a user

Product Endpoints

  • GET /api/products - Get all products
  • GET /api/products/{id} - Get product by ID
  • POST /api/products - Add a new product
  • PUT /api/products/{id} - Update a product
  • DELETE /api/products/{id} - Delete a product

Example API Requests

Create User:

curl -X POST http://localhost:8080/api/users \
  -H "Content-Type: application/json" \
  -d '{
    "id": "user1",
    "name": "John Doe",
    "age": 30,
    "email": "john@example.com",
    "password": "password123"
  }'

Add Product:

curl -X POST http://localhost:8080/api/products \
  -H "Content-Type: application/json" \
  -d '{
    "id": "prod1",
    "name": "Laptop",
    "price": 999.99,
    "expirationDate": "2025-12-31"
  }'

Get All Products:

curl http://localhost:8080/api/products

Exception Handling

The application throws appropriate exceptions:

  • ProductNotFoundException - When a product with the given ID doesn't exist
  • ProductAlreadyExistsException - When trying to add a product with an existing ID

All exceptions are handled by the GlobalExceptionHandler and return appropriate HTTP status codes.

Project Structure

src/
├── main/
│   ├── java/com/userproduct/
│   │   ├── config/          # Configuration classes
│   │   ├── controller/      # REST controllers
│   │   ├── exception/       # Custom exceptions
│   │   ├── model/           # Entity models
│   │   ├── repository/      # MongoDB repositories
│   │   ├── service/         # Business logic
│   │   ├── cli/             # CLI frontend
│   │   ├── frontend/        # Frontend code (JavaFX)
│   │   └── config/          # Configuration (CORS, etc.)
│   └── resources/
│       ├── static/          # Web interface (HTML, CSS, JS)
│       │   ├── index.html
│       │   ├── styles.css
│       │   └── app.js
│       ├── application.properties
│       └── application-local.properties (create this file with your MongoDB credentials)
└── pom.xml

Technologies Used

  • Spring Boot 3.2.0
  • Spring Data MongoDB
  • Maven
  • MongoDB Atlas
  • Lombok
  • Jackson (for JSON processing)

License

This project is for educational purposes.

About

A CLI-based application for managing users and products. It supports user creation and full CRUD operations on products, including listing, fetching by ID, adding, updating, and deleting products with proper exception handling. Designed with a modular backend and optional MongoDB integration.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors