CRUD-API-Example
Table of Contents
- 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
IDString
firstNameString
lastNameString
emailString
phoneArray
ofOrder
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
idString
firstNameString
lastNameString
emailString
phone
Order
Properties
Date
dateNumber
idArray
ofProduct
objects nameditems
Number
customerNumberNumber
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
numberNumber
costNumber
markupNumber
listPriceBoolean
sellAtListNumber
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 thecustomerSchema
. It represents customer data in the MongoDB database. -
Employee
: A Mongoose model based on theemployeeSchema
. 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.