Significant API progress

This commit is contained in:
oitsjustjose 2022-04-14 20:27:45 -05:00
commit 01227bb28f
27 changed files with 2378 additions and 0 deletions

View file

@ -0,0 +1,30 @@
import { Document, model, Schema } from "mongoose";
export type ProductModel = Document & {
name: string;
quantity: number;
price: number;
hidden: boolean;
};
const ProductSchema = new Schema<ProductModel>({
name: {
type: String,
required: true,
},
quantity: {
type: Number,
required: true,
},
price: {
type: Number,
required: true,
},
hidden: {
type: Boolean,
required: true,
default: false,
},
});
export default model<ProductModel>("products", ProductSchema, "products");

View file

@ -0,0 +1,27 @@
import { Document, model, Schema, ObjectId, Types } from "mongoose";
export type ReviewModel = Document & {
productId: ObjectId;
userId: ObjectId;
rating: number; // Out of Decimal from 0.0 to 5.0 (stars)
reviewTitle: string; // General title of the review left
reviewBody: string; // The actual review left behind.
};
const PRSchema = new Schema<ReviewModel>({
productId: {
type: Types.ObjectId,
ref: "products",
required: true,
},
userId: {
type: Types.ObjectId,
ref: "users",
required: true,
},
rating: Number,
reviewTitle: String,
reviewBody: String,
});
export default model<ReviewModel>("productReviews", PRSchema, "productReviews");

View file

@ -0,0 +1,38 @@
import { Document, model, Schema } from "mongoose";
import { scryptSync, randomBytes, timingSafeEqual } from "crypto";
export type UserModel = Document & {
first: string;
last: string;
email: string;
password: string;
admin: boolean;
hashPassword: (password: string) => string;
validPassword: (password: string) => boolean;
};
const UserSchema = new Schema<UserModel>({
first: String,
last: String,
email: String,
password: String,
admin: {
type: Boolean,
default: false,
},
});
UserSchema.methods.hashPassword = function (password: string) {
const salt = randomBytes(16).toString("hex");
const hash = scryptSync(password, salt, 64).toString("hex");
return `${salt}:${hash}`;
};
UserSchema.methods.validPassword = function (inp: string) {
const [salt, key] = this.password.split(":");
const hashedBuf = scryptSync(inp, salt, 64);
const keyBuf = Buffer.from(key, "hex");
return timingSafeEqual(hashedBuf, keyBuf);
};
export default model<UserModel>("users", UserSchema, "users");