stawuah / CRUD-API-Example

Simple Example of a CRUD API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CRUD-API-Example

Table of Contents

License

This project is licensed under the MIT License - see the LICENSE file for details.

Developer: BJ Rutledge

Description

This project serves as a simple example of a CRUD (Create, Read, Update, Delete) API. It utilizes a MongoDB database hosted on Atlas for data storage.

Functionality

It features a Node.js and Express server with routes that enable the creation, reading, updating, and deletion of records for a fictional business.

Note: This project is currently under development, and the documentation will continue to be updated as development progresses.

Routes

Under development

Data Structures

Customer

Properties

  • Number ID
  • String firstName
  • String lastName
  • String email
  • String phone
  • Array of Order objects

Employee

Description

The Employee class represents an employee in a fictional business. It is used for managing employee information within the application.

Properties

  • employeeId (Number): The unique employee ID.
    • Type: Number
    • Required: Yes
  • firstName (String): The first name of the employee.
    • Type: String
    • Required: Yes
  • lastName (String): The last name of the employee.
    • Type: String
    • Required: Yes
  • middleInitial (String): The middle initial of the employee.
    • Type: String
  • hireDate (Date): The date when the employee was hired.
    • Type: Date
    • Required: Yes
  • terminationDate (Date): The date when the employee was terminated (optional).
    • Type: Date
  • terminated (Boolean): Indicates whether the employee is terminated.
    • Type: Boolean

Constructor

constructor(employeeId, firstName, lastName, middleInitial, hireDate)

Creates a new Employee instance.

  • employeeId (Number): The unique employee ID.
  • firstName (String): The first name of the employee.
  • lastName (String): The last name of the employee.
  • middleInitial (String): The middle initial of the employee.
  • hireDate (Date): The date when the employee was hired.

Methods

terminateEmployee(date)

Sets the termination date and marks the employee as terminated.

  • date (Date): The date of termination.

Example

const Employee = require("./Employee");

// Create a new employee
const employee = new Employee(1, "John", "Doe", "M", new Date("2023-09-24"));

// Terminate the employee
employee.terminateEmployee(new Date("2023-09-30"));

console.log(employee);
Properties
  • Number id
  • String firstName
  • String lastName
  • String email
  • String phone

Order

Properties

  • Date date
  • Number id
  • Array of Product objects named items
  • Number customerNumber
  • Number orderTotal
Instantiation Example
const date = new Date();
const orderId = 1234;
const products = [product1, product2, product3]; // Array of Product
const customerNumber = 24; // Each order is linked to a customer account

const order = new Order(date, orderId, products, customerNumber);
// Get the total for the order
console.log(order.orderTotal);

Product

Properties

  • Number number
  • Number cost
  • Number markup
  • Number listPrice
  • Boolean sellAtList
  • Number price
Instantiation Example
const productNum = 22;
const cost = 5.0;
const markup = 2.5;
const list = 6.22;
const sellAtList = false;
const productName = "My Widget";
const productDescription = "My Widget is so cool!";

const product = new Product(
  productNum,
  cost,
  markup,
  list,
  sellAtList,
  productName,
  productDescription
);
/** If sellAtList === true, the price is set to list;
 * otherwise, it will be markup * cost.
 * The default markup is 1.5. If the provided markup is below
 * the default, the default value is used.
 */
console.log(product.price);

Customer and Employee schemas and data models using Mongoose and MongoDB in Node.js:

Customer Schema

The customerSchema represents the schema for the Customer model. It defines the structure and constraints for storing customer information in the MongoDB database.

Fields

  • number (Number): The unique customer number.
    • Type: Number
    • Required: Yes
  • first (String): The first name of the customer.
    • Type: String
    • Minimum Length: 2 characters
    • Maximum Length: 20 characters
    • Required: Yes
  • middleInitial (String): The middle initial of the customer (optional).
    • Type: String
    • Maximum Length: 1 character
  • last (String): The last name of the customer.
    • Type: String
    • Minimum Length: 2 characters
    • Maximum Length: 20 characters
    • Required: Yes
  • taxable (Boolean): Indicates if the customer is taxable.
    • Type: Boolean
    • Required: Yes
  • orders (Array of Number): An array of order numbers associated with the customer. Orders will be stored separately.

Employee Schema

The employeeSchema represents the schema for the Employee model. It defines the structure and constraints for storing employee information in the MongoDB database.

Fields

  • number (Number): The unique employee number.
    • Type: Number
    • Required: Yes
  • first (String): The first name of the employee.
    • Type: String
    • Minimum Length: 2 characters
    • Maximum Length: 20 characters
    • Required: Yes
  • middleInitial (String): The middle initial of the employee (optional).
    • Type: String
    • Maximum Length: 1 character
  • last (String): The last name of the employee.
    • Type: String
    • Minimum Length: 2 characters
    • Maximum Length: 20 characters
    • Required: Yes
  • hireDate (Date): The date when the employee was hired.
    • Type: Date
    • Required: Yes
  • terminatd: (Boolean) True if customer is terminated .
  • terminationDate (Date): The date when the employee was terminated (optional).

Data Models

  • Customer: A Mongoose model based on the customerSchema. It represents customer data in the MongoDB database.

  • Employee: A Mongoose model based on the employeeSchema. It represents employee data in the MongoDB database.

These schemas and data models are designed to provide a structured way to store and manipulate customer and employee data within your Node.js application using MongoDB and Mongoose.

About

Simple Example of a CRUD API

License:MIT License


Languages

Language:JavaScript 98.8%Language:Pug 0.9%Language:CSS 0.4%