done changing
This commit is contained in:
parent
4ae6bb45d3
commit
37b0c1c9e1
7 changed files with 193 additions and 71 deletions
112
package-lock.json
generated
112
package-lock.json
generated
|
@ -15,6 +15,7 @@
|
|||
"express": "^4.18.2",
|
||||
"jsonwebtoken": "^9.0.0",
|
||||
"mongodb": "^5.6.0",
|
||||
"mongoose": "^7.2.2",
|
||||
"prisma": "^4.15.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
@ -548,6 +549,14 @@
|
|||
"safe-buffer": "^5.0.1"
|
||||
}
|
||||
},
|
||||
"node_modules/kareem": {
|
||||
"version": "2.5.1",
|
||||
"resolved": "https://registry.npmjs.org/kareem/-/kareem-2.5.1.tgz",
|
||||
"integrity": "sha512-7jFxRVm+jD+rkq3kY0iZDJfsO2/t4BBPeEb2qKn2lR/9KhuksYk5hxzfRYWMPV8P/x2d0kHD306YyWLzjjH+uA==",
|
||||
"engines": {
|
||||
"node": ">=12.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/lodash": {
|
||||
"version": "4.17.21",
|
||||
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
|
||||
|
@ -662,6 +671,104 @@
|
|||
"whatwg-url": "^11.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/mongoose": {
|
||||
"version": "7.2.2",
|
||||
"resolved": "https://registry.npmjs.org/mongoose/-/mongoose-7.2.2.tgz",
|
||||
"integrity": "sha512-JPBMTF+oYfLGVFWbHSZ/H+f1GajNanGLYH6c/P0nE3bNJfwGhX573vieGR0kNlNhj3cZk8WCPrnVsTNeUmFUag==",
|
||||
"dependencies": {
|
||||
"bson": "^5.3.0",
|
||||
"kareem": "2.5.1",
|
||||
"mongodb": "5.5.0",
|
||||
"mpath": "0.9.0",
|
||||
"mquery": "5.0.0",
|
||||
"ms": "2.1.3",
|
||||
"sift": "16.0.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=14.20.1"
|
||||
},
|
||||
"funding": {
|
||||
"type": "opencollective",
|
||||
"url": "https://opencollective.com/mongoose"
|
||||
}
|
||||
},
|
||||
"node_modules/mongoose/node_modules/mongodb": {
|
||||
"version": "5.5.0",
|
||||
"resolved": "https://registry.npmjs.org/mongodb/-/mongodb-5.5.0.tgz",
|
||||
"integrity": "sha512-XgrkUgAAdfnZKQfk5AsYL8j7O99WHd4YXPxYxnh8dZxD+ekYWFRA3JktUsBnfg+455Smf75/+asoU/YLwNGoQQ==",
|
||||
"dependencies": {
|
||||
"bson": "^5.3.0",
|
||||
"mongodb-connection-string-url": "^2.6.0",
|
||||
"socks": "^2.7.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=14.20.1"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"saslprep": "^1.0.3"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@aws-sdk/credential-providers": "^3.201.0",
|
||||
"mongodb-client-encryption": ">=2.3.0 <3",
|
||||
"snappy": "^7.2.2"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@aws-sdk/credential-providers": {
|
||||
"optional": true
|
||||
},
|
||||
"mongodb-client-encryption": {
|
||||
"optional": true
|
||||
},
|
||||
"snappy": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/mongoose/node_modules/ms": {
|
||||
"version": "2.1.3",
|
||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
|
||||
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA=="
|
||||
},
|
||||
"node_modules/mpath": {
|
||||
"version": "0.9.0",
|
||||
"resolved": "https://registry.npmjs.org/mpath/-/mpath-0.9.0.tgz",
|
||||
"integrity": "sha512-ikJRQTk8hw5DEoFVxHG1Gn9T/xcjtdnOKIU1JTmGjZZlg9LST2mBLmcX3/ICIbgJydT2GOc15RnNy5mHmzfSew==",
|
||||
"engines": {
|
||||
"node": ">=4.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/mquery": {
|
||||
"version": "5.0.0",
|
||||
"resolved": "https://registry.npmjs.org/mquery/-/mquery-5.0.0.tgz",
|
||||
"integrity": "sha512-iQMncpmEK8R8ncT8HJGsGc9Dsp8xcgYMVSbs5jgnm1lFHTZqMJTUWTDx1LBO8+mK3tPNZWFLBghQEIOULSTHZg==",
|
||||
"dependencies": {
|
||||
"debug": "4.x"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=14.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/mquery/node_modules/debug": {
|
||||
"version": "4.3.4",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
|
||||
"integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==",
|
||||
"dependencies": {
|
||||
"ms": "2.1.2"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=6.0"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"supports-color": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/mquery/node_modules/ms": {
|
||||
"version": "2.1.2",
|
||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
|
||||
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
|
||||
},
|
||||
"node_modules/ms": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
|
||||
|
@ -889,6 +996,11 @@
|
|||
"url": "https://github.com/sponsors/ljharb"
|
||||
}
|
||||
},
|
||||
"node_modules/sift": {
|
||||
"version": "16.0.1",
|
||||
"resolved": "https://registry.npmjs.org/sift/-/sift-16.0.1.tgz",
|
||||
"integrity": "sha512-Wv6BjQ5zbhW7VFefWusVP33T/EM0vYikCaQ2qR8yULbsilAT8/wQaXvuQ3ptGLpoKx+lihJE3y2UTgKDyyNHZQ=="
|
||||
},
|
||||
"node_modules/smart-buffer": {
|
||||
"version": "4.2.0",
|
||||
"resolved": "https://registry.npmjs.org/smart-buffer/-/smart-buffer-4.2.0.tgz",
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
"express": "^4.18.2",
|
||||
"jsonwebtoken": "^9.0.0",
|
||||
"mongodb": "^5.6.0",
|
||||
"mongoose": "^7.2.2",
|
||||
"prisma": "^4.15.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
|
@ -1,37 +1,37 @@
|
|||
datasource db {
|
||||
provider = "mongodb"
|
||||
url = env("DATABASE_URL")
|
||||
}
|
||||
// datasource db {
|
||||
// provider = "mongodb"
|
||||
// url = env("DATABASE_URL")
|
||||
// }
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
// generator client {
|
||||
// provider = "prisma-client-js"
|
||||
// }
|
||||
|
||||
model User {
|
||||
id String @id @default(cuid()) @map("_id")
|
||||
firstName String
|
||||
lastName String
|
||||
email String @unique
|
||||
password String
|
||||
address String
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
}
|
||||
// model User {
|
||||
// id String @id @default(cuid()) @map("_id")
|
||||
// firstName String
|
||||
// lastName String
|
||||
// email String @unique
|
||||
// password String
|
||||
// address String
|
||||
// createdAt DateTime @default(now())
|
||||
// updatedAt DateTime @updatedAt
|
||||
// }
|
||||
|
||||
model Item {
|
||||
id String @id @default(cuid()) @map("_id")
|
||||
name String
|
||||
price Float
|
||||
quantity Int @default(0)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
}
|
||||
// model Item {
|
||||
// id String @id @default(cuid()) @map("_id")
|
||||
// name String
|
||||
// price Float
|
||||
// quantity Int @default(0)
|
||||
// createdAt DateTime @default(now())
|
||||
// updatedAt DateTime @updatedAt
|
||||
// }
|
||||
|
||||
model Cart {
|
||||
id String @id @default(cuid()) @map("_id")
|
||||
userId String
|
||||
itemId String
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
@@unique([userId, itemId])
|
||||
}
|
||||
// model Cart {
|
||||
// id String @id @default(cuid()) @map("_id")
|
||||
// userId String
|
||||
// itemId String
|
||||
// createdAt DateTime @default(now())
|
||||
// updatedAt DateTime @updatedAt
|
||||
// @@unique([userId, itemId])
|
||||
// }
|
|
@ -1,34 +1,26 @@
|
|||
import { Request, Response } from 'express';
|
||||
import bcrypt from 'bcryptjs';
|
||||
import { User } from '@prisma/client';
|
||||
import { createUser } from '../models/User';
|
||||
import User, { IUser } from '../models/User';
|
||||
|
||||
async function createUserController(req: Request, res: Response) {
|
||||
export async function createUser(req: Request, res: Response) {
|
||||
try {
|
||||
const { firstName, lastName, email, password, address } = req.body;
|
||||
|
||||
// Hash the password
|
||||
const hashedPassword = await bcrypt.hash(password, 10);
|
||||
|
||||
const user: User = await createUser({
|
||||
// Create the user in the database
|
||||
const user: IUser = await User.create({
|
||||
firstName,
|
||||
lastName,
|
||||
email,
|
||||
password: hashedPassword,
|
||||
address,
|
||||
id: '',
|
||||
createdAt: undefined,
|
||||
updatedAt: undefined
|
||||
});
|
||||
|
||||
res.json(hashedPassword);
|
||||
res.json(user);
|
||||
} catch (error) {
|
||||
console.error('Error creating user:', error);
|
||||
res.status(500).json({ error: 'An error occurred while creating the user.' });
|
||||
}
|
||||
}
|
||||
|
||||
const UserController = {
|
||||
createUser: createUserController,
|
||||
};
|
||||
|
||||
export default UserController;
|
||||
|
|
21
src/index.ts
21
src/index.ts
|
@ -1,17 +1,26 @@
|
|||
import express from 'express';
|
||||
import mongoose from 'mongoose';
|
||||
import userRoutes from './routes/user';
|
||||
|
||||
const env = require('dotenv').config().parsed;
|
||||
|
||||
const app = express();
|
||||
const PORT = 3000;
|
||||
|
||||
app.use(express.json());
|
||||
|
||||
// Connect to MongoDB using Mongoose
|
||||
mongoose.connect(env.DATABASE_URL);
|
||||
|
||||
const db = mongoose.connection;
|
||||
|
||||
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
|
||||
db.once('open', () => {
|
||||
console.log('Connected to MongoDB');
|
||||
});
|
||||
|
||||
app.use('/users', userRoutes);
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log('Server started on port 3000!');
|
||||
})
|
||||
|
||||
|
||||
|
||||
|
||||
console.log(`Server started on port ${PORT}`);
|
||||
});
|
||||
|
|
|
@ -1,17 +1,25 @@
|
|||
import { PrismaClient, User } from '@prisma/client';
|
||||
import mongoose, { Schema, Document } from 'mongoose';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
export interface IUser extends Document {
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
email: string;
|
||||
password: string;
|
||||
address: string;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
export async function createUser(user: User): Promise<User> {
|
||||
const createdUser = await prisma.user.create({
|
||||
data: {
|
||||
firstName: user.firstName,
|
||||
lastName: user.lastName,
|
||||
email: user.email,
|
||||
password: user.password,
|
||||
address: user.address,
|
||||
},
|
||||
const UserSchema: Schema = new Schema({
|
||||
firstName: { type: String, required: true },
|
||||
lastName: { type: String, required: true },
|
||||
email: { type: String, required: true },
|
||||
password: { type: String, required: true },
|
||||
address: { type: String, required: true },
|
||||
createdAt: { type: Date, default: Date.now },
|
||||
updatedAt: { type: Date, default: Date.now },
|
||||
});
|
||||
|
||||
return createdUser;
|
||||
}
|
||||
const User = mongoose.model<IUser>('User', UserSchema);
|
||||
|
||||
export default User;
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import express from 'express';
|
||||
import UserController from '../controllers/UserController';
|
||||
import { createUser } from '../controllers/UserController';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.post('/', UserController.createUser);
|
||||
router.post('/', createUser);
|
||||
|
||||
export default router;
|
Loading…
Reference in a new issue