diff --git a/src/controllers/CartController.ts b/src/controllers/CartController.ts index 109262f..9f32590 100644 --- a/src/controllers/CartController.ts +++ b/src/controllers/CartController.ts @@ -1,5 +1,7 @@ import { Request, Response } from 'express'; -import { Cart, ICart, Order, Product } from '../mongoose/Schema'; +import { Cart, ICart } from '../models/cartModel'; +import { Product } from '../models/productModel'; +import { Order } from '../models/orderModel'; import { sendEmailasync } from '../services/sendGrid'; import { config } from 'dotenv'; diff --git a/src/controllers/ProductController.ts b/src/controllers/ProductController.ts index 2477bb8..047c624 100644 --- a/src/controllers/ProductController.ts +++ b/src/controllers/ProductController.ts @@ -1,5 +1,5 @@ import { Request, Response } from 'express'; -import { Product, IProduct } from '../mongoose/Schema'; +import { Product, IProduct } from '../models/productModel'; export async function createProduct(req: Request, res: Response) { try { diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index af58d56..b21ede8 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -1,7 +1,7 @@ import { Request, Response } from 'express'; import bcrypt from 'bcryptjs'; import jwt from 'jsonwebtoken'; -import { User, IUser } from '../mongoose/Schema'; +import { User, IUser } from '../models/userModel'; import { clearJwtCookie, setJwtCookie } from '../middlewares/checkAuth'; import validate from 'deep-email-validator'; diff --git a/src/models/cartModel.ts b/src/models/cartModel.ts new file mode 100644 index 0000000..8a115ba --- /dev/null +++ b/src/models/cartModel.ts @@ -0,0 +1,23 @@ +import mongoose, { Schema, Document } from 'mongoose'; + +interface ICart extends Document { + userId: string; + products: { [itemId: string]: number }; + createdAt: Date; + updatedAt: Date; +} + + +const CartSchema: Schema = new Schema({ + userId: { type: Schema.Types.ObjectId, ref: 'User', required: true, unique: true }, + products: { type: Schema.Types.Mixed, default: {} }, + createdAt: { type: Date, default: Date.now }, + updatedAt: { type: Date, default: Date.now }, +}); + +const Cart = mongoose.model('Cart', CartSchema); + +export { + Cart, + ICart +} diff --git a/src/models/orderModel.ts b/src/models/orderModel.ts new file mode 100644 index 0000000..bc0eaa0 --- /dev/null +++ b/src/models/orderModel.ts @@ -0,0 +1,27 @@ +import mongoose, { Schema, Document } from 'mongoose'; + + + +interface IOrder extends Document { + userId: string; + products: { [itemId: string]: number }; + emailSent: boolean; + createdAt: Date; + updatedAt: Date; +} + +const OrderSchema: Schema = new Schema({ + userId: { type: Schema.Types.ObjectId, ref: 'User', required: true }, + products: { type: Schema.Types.Mixed, default: {} }, + emailSent: { type: Boolean, default: false }, + createdAt: { type: Date, default: Date.now }, + updatedAt: { type: Date, default: Date.now }, +}); + + +const Order = mongoose.model('Order', OrderSchema); + +export { + Order, + IOrder +}; diff --git a/src/models/productModel.ts b/src/models/productModel.ts new file mode 100644 index 0000000..6385862 --- /dev/null +++ b/src/models/productModel.ts @@ -0,0 +1,28 @@ +import mongoose, { Schema, Document } from 'mongoose'; + +interface IProduct extends Document { + name: string; + description: string; + price: number; + userId: string; + createdAt: Date; + updatedAt: Date; +} + +const ProductSchema: Schema = new Schema({ + name: { type: String, required: true }, + description: { type: String, required: true }, + price: { type: Number, required: true }, + userId: { type: Schema.Types.ObjectId, ref: 'User', required: true }, + createdAt: { type: Date, default: Date.now }, + updatedAt: { type: Date, default: Date.now }, +}); + +const Product = mongoose.model('Product', ProductSchema); + +ProductSchema.index({ name: 1, userId: 1 }, { unique: true }); + +export { + Product, + IProduct +} diff --git a/src/models/userModel.ts b/src/models/userModel.ts new file mode 100644 index 0000000..f46db85 --- /dev/null +++ b/src/models/userModel.ts @@ -0,0 +1,26 @@ +// src/models/userModel.ts + +import mongoose, { Schema, Document } from 'mongoose'; + +interface IUser extends Document { + email: string; + password: string; + address: string; + createdAt: Date; + updatedAt: Date; +} + +const UserSchema: Schema = new Schema({ + email: { type: String, required: true, unique: true }, + password: { type: String, required: true }, + address: { type: String, required: true }, + createdAt: { type: Date, default: Date.now }, + updatedAt: { type: Date, default: Date.now }, +}); + +const User = mongoose.model('User', UserSchema); + +export { + User, + IUser +} diff --git a/src/mongoose/Schema.ts b/src/mongoose/Schema.ts deleted file mode 100644 index 0a1157b..0000000 --- a/src/mongoose/Schema.ts +++ /dev/null @@ -1,74 +0,0 @@ -import mongoose, { Schema, Document } from 'mongoose'; - -export interface IUser extends Document { - email: string; - password: string; - address: string; - createdAt: Date; - updatedAt: Date; -} - -export interface IProduct extends Document { - name: string; - description: string; - price: number; - userId: string; - createdAt: Date; - updatedAt: Date; -} - -export interface ICart extends Document { - userId: string; - products: { [itemId: string]: number }; - createdAt: Date; - updatedAt: Date; -} - -export interface IOrder extends Document { - userId: string; - products: { [itemId: string]: number }; - emailSent: boolean; - createdAt: Date; - updatedAt: Date; -} - -const UserSchema: Schema = new Schema({ - email: { type: String, required: true, unique: true }, - password: { type: String, required: true }, - address: { type: String, required: true }, - createdAt: { type: Date, default: Date.now }, - updatedAt: { type: Date, default: Date.now }, -}); - -const ProductSchema: Schema = new Schema({ - name: { type: String, required: true }, - description: { type: String, required: true }, - price: { type: Number, required: true }, - userId: { type: Schema.Types.ObjectId, ref: 'User', required: true }, - createdAt: { type: Date, default: Date.now }, - updatedAt: { type: Date, default: Date.now }, -}); - -const CartSchema: Schema = new Schema({ - userId: { type: Schema.Types.ObjectId, ref: 'User', required: true, unique: true }, - products: { type: Schema.Types.Mixed, default: {} }, - createdAt: { type: Date, default: Date.now }, - updatedAt: { type: Date, default: Date.now }, -}); - -const OrderSchema: Schema = new Schema({ - userId: { type: Schema.Types.ObjectId, ref: 'User', required: true }, - products: { type: Schema.Types.Mixed, default: {} }, - emailSent: { type: Boolean, default: false }, - createdAt: { type: Date, default: Date.now }, - updatedAt: { type: Date, default: Date.now }, -}); - -ProductSchema.index({ name: 1, userId: 1 }, { unique: true }); - -const User = mongoose.model('User', UserSchema); -const Product = mongoose.model('Product', ProductSchema); -const Cart = mongoose.model('Cart', CartSchema); -const Order = mongoose.model('Order', OrderSchema); - -export { User, Product, Cart, Order }; diff --git a/src/services/sendGrid.ts b/src/services/sendGrid.ts index d23428b..c805756 100644 --- a/src/services/sendGrid.ts +++ b/src/services/sendGrid.ts @@ -1,5 +1,6 @@ import { config } from "dotenv"; -import { User, Order } from "../mongoose/Schema"; +import { Order } from "../models/orderModel"; +import { User } from "../models/userModel"; import client from '@sendgrid/mail'; config();