Compare commits
8 Commits
fix/orders
...
v1.1.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
38bc0c55c8 | ||
|
|
9fb4219418 | ||
|
|
fb644b6f7b | ||
|
|
d88e680413 | ||
|
|
8b7f43d15a | ||
| b9edd0091c | |||
|
|
558245f876 | ||
| a927727c1b |
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "multi-stub",
|
||||
"version": "1.0.1",
|
||||
"version": "1.1.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "multi-stub",
|
||||
"version": "1.0.1",
|
||||
"version": "1.1.0",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"axios": "^1.7.9",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "multi-stub",
|
||||
"version": "1.0.1",
|
||||
"version": "1.1.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
exports.TODO_LIST_MODEL_NAME = 'TODO_LIST'
|
||||
exports.TODO_ITEM_MODEL_NAME = 'TODO_ITEM'
|
||||
@@ -80,7 +80,7 @@ app.use(require("./root"))
|
||||
*/
|
||||
app.use("/kfu-m-24-1", require("./routers/kfu-m-24-1"))
|
||||
app.use("/epja-2024-1", require("./routers/epja-2024-1"))
|
||||
app.use("/todo", require("./routers/todo/routes"))
|
||||
app.use("/v1/todo", require("./routers/todo"))
|
||||
app.use("/dogsitters-finder", require("./routers/dogsitters-finder"))
|
||||
app.use("/kazan-explore", require("./routers/kazan-explore"))
|
||||
app.use("/edateam", require("./routers/edateam-legacy"))
|
||||
|
||||
@@ -11,6 +11,9 @@ const schema = new Schema({
|
||||
schema.set('toJSON', {
|
||||
virtuals: true,
|
||||
versionKey: false,
|
||||
transform(_doc, ret) {
|
||||
delete ret._id;
|
||||
}
|
||||
})
|
||||
|
||||
schema.virtual('id').get(function () {
|
||||
|
||||
@@ -27,7 +27,7 @@ router.put('/new', (req, res) => {
|
||||
return res.status(500).send('No data to update'); // Internal server error
|
||||
}
|
||||
|
||||
indexedUpdatedData = { id: data.length, ...updatedData }; // Add the new dictionary to the array
|
||||
const indexedUpdatedData = { id: data.length, ...updatedData }; // Add the new dictionary to the array
|
||||
|
||||
data.push(indexedUpdatedData); // Add the new dictionary to the array
|
||||
|
||||
|
||||
74
server/routers/todo/auth.js
Normal file
74
server/routers/todo/auth.js
Normal file
@@ -0,0 +1,74 @@
|
||||
const { Router } = require("express");
|
||||
const hash = require("pbkdf2-password")();
|
||||
const { promisify } = require("node:util");
|
||||
const jwt = require('jsonwebtoken')
|
||||
|
||||
const { getAnswer } = require("../../utils/common");
|
||||
|
||||
const { AuthModel } = require("./model/todo/auth");
|
||||
const { TOKEN_KEY } = require('./const')
|
||||
const { UserModel } = require("./model/todo/user");
|
||||
|
||||
const { requiredValidate } = require('./utils')
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.post(
|
||||
"/signup",
|
||||
requiredValidate("login", "password", "email"),
|
||||
async (req, res, next) => {
|
||||
const { login, password, email } = req.body
|
||||
|
||||
const user = await AuthModel.findOne({ login });
|
||||
|
||||
if (user) {
|
||||
throw new Error("Пользователь с таким логином уже существует");
|
||||
}
|
||||
|
||||
hash({ password }, async function (err, pass, salt, hash) {
|
||||
if (err) return next(err);
|
||||
|
||||
const user = await UserModel.create({ login, email });
|
||||
await AuthModel.create({ login, hash, salt, userId: user.id });
|
||||
|
||||
res.json(getAnswer(null, { ok: true }))
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
function authenticate(login, pass, cb) {
|
||||
AuthModel.findOne({ login }).populate('userId').exec().then((user) => {
|
||||
if (!user) return cb(null, null)
|
||||
|
||||
hash({ password: pass, salt: user.salt }, function (err, pass, salt, hash) {
|
||||
if (err) return cb(err)
|
||||
if (hash === user.hash) return cb(null, user)
|
||||
cb(null, null)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const auth = promisify(authenticate)
|
||||
|
||||
router.post('/signin', requiredValidate('login', 'password'), async (req, res) => {
|
||||
const { login, password } = req.body
|
||||
|
||||
const user = await auth(login, password)
|
||||
|
||||
if (!user) {
|
||||
throw new Error("Неверный логин или пароль")
|
||||
}
|
||||
|
||||
const accessToken = jwt.sign({
|
||||
...JSON.parse(JSON.stringify(user.userId)),
|
||||
}, TOKEN_KEY, {
|
||||
expiresIn: '12h'
|
||||
})
|
||||
|
||||
res.json(getAnswer(null, {
|
||||
user: user.userId,
|
||||
token: accessToken,
|
||||
}))
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
7
server/routers/todo/const.js
Normal file
7
server/routers/todo/const.js
Normal file
@@ -0,0 +1,7 @@
|
||||
exports.TODO_LIST_MODEL_NAME = 'TODO_LIST'
|
||||
exports.TODO_ITEM_MODEL_NAME = 'TODO_ITEM'
|
||||
|
||||
exports.TODO_AUTH_PASSWD_MODEL_NAME = 'TODO_AUTH_PASSWD'
|
||||
exports.TODO_AUTH_USER_MODEL_NAME = 'TODO_AUTH_USER'
|
||||
exports.TODO_AUTH_CHAT_MODEL_NAME = 'TODO_AUTH_CHAT'
|
||||
exports.TOKEN_KEY = process.env.TOKEN_KEY || "asdfhoa-podh829438132-iahda98gauj-dj2i3-111"
|
||||
12
server/routers/todo/index.js
Normal file
12
server/routers/todo/index.js
Normal file
@@ -0,0 +1,12 @@
|
||||
const { Router } = require('express')
|
||||
|
||||
const router = Router()
|
||||
|
||||
const todoRouter = require('./routes')
|
||||
const authRouter = require('./auth')
|
||||
|
||||
router.use('/auth', authRouter)
|
||||
|
||||
router.use(todoRouter)
|
||||
|
||||
module.exports = router
|
||||
31
server/routers/todo/model/todo/auth.js
Normal file
31
server/routers/todo/model/todo/auth.js
Normal file
@@ -0,0 +1,31 @@
|
||||
const { Schema, model } = require("mongoose");
|
||||
|
||||
const {
|
||||
TODO_AUTH_PASSWD_MODEL_NAME,
|
||||
TODO_AUTH_USER_MODEL_NAME,
|
||||
} = require("../../const");
|
||||
|
||||
const schema = new Schema({
|
||||
login: { type: String, required: true, unique: true },
|
||||
hash: { type: String, required: true },
|
||||
salt: { type: String, required: true },
|
||||
userId: { type: Schema.Types.ObjectId, ref: TODO_AUTH_USER_MODEL_NAME },
|
||||
created: {
|
||||
type: Date,
|
||||
default: () => new Date().toISOString(),
|
||||
},
|
||||
});
|
||||
|
||||
schema.set("toJSON", {
|
||||
virtuals: true,
|
||||
versionKey: false,
|
||||
transform: function (doc, ret) {
|
||||
delete ret._id;
|
||||
},
|
||||
});
|
||||
|
||||
schema.virtual("id").get(function () {
|
||||
return this._id.toHexString();
|
||||
});
|
||||
|
||||
exports.AuthModel = model(TODO_AUTH_PASSWD_MODEL_NAME, schema);
|
||||
@@ -1,18 +1,22 @@
|
||||
const { Schema, model } = require('mongoose')
|
||||
|
||||
const { TODO_LIST_MODEL_NAME, TODO_ITEM_MODEL_NAME } = require('../../const')
|
||||
const { TODO_LIST_MODEL_NAME, TODO_ITEM_MODEL_NAME, TODO_AUTH_USER_MODEL_NAME } = require('../../const')
|
||||
|
||||
const schema = new Schema({
|
||||
title: String,
|
||||
created: {
|
||||
type: Date, default: () => new Date().toISOString(),
|
||||
},
|
||||
createdBy: { type: Schema.Types.ObjectId, ref: TODO_AUTH_USER_MODEL_NAME },
|
||||
items: [{ type: Schema.Types.ObjectId, ref: TODO_ITEM_MODEL_NAME }],
|
||||
})
|
||||
|
||||
schema.set('toJSON', {
|
||||
virtuals: true,
|
||||
versionKey: false,
|
||||
transform: function (doc, ret) {
|
||||
delete ret._id
|
||||
}
|
||||
})
|
||||
|
||||
schema.virtual('id').get(function () {
|
||||
27
server/routers/todo/model/todo/user.js
Normal file
27
server/routers/todo/model/todo/user.js
Normal file
@@ -0,0 +1,27 @@
|
||||
const { Schema, model } = require("mongoose");
|
||||
|
||||
const { TODO_AUTH_USER_MODEL_NAME } = require("../../const");
|
||||
|
||||
const schema = new Schema({
|
||||
login: { type: String, required: true, unique: true },
|
||||
email: { type: String, required: true, unique: true },
|
||||
role: { type: String, default: "user" },
|
||||
created: {
|
||||
type: Date,
|
||||
default: () => new Date().toISOString(),
|
||||
},
|
||||
})
|
||||
|
||||
schema.set("toJSON", {
|
||||
virtuals: true,
|
||||
versionKey: false,
|
||||
transform: function (doc, ret) {
|
||||
delete ret._id
|
||||
},
|
||||
})
|
||||
|
||||
schema.virtual("id").get(function () {
|
||||
return this._id.toHexString()
|
||||
})
|
||||
|
||||
exports.UserModel = model(TODO_AUTH_USER_MODEL_NAME, schema);
|
||||
49
server/routers/todo/routes copy.js
Normal file
49
server/routers/todo/routes copy.js
Normal file
@@ -0,0 +1,49 @@
|
||||
const { Router } = require('express')
|
||||
|
||||
const { ListModel } = require('./model/todo/list')
|
||||
const { ItemModel } = require('./model/todo/item')
|
||||
const { getAnswer } = require('../../utils/common')
|
||||
|
||||
const router = Router()
|
||||
|
||||
// test - http://localhost:8033/todo/list
|
||||
router.get('/list', async (req, res) => {
|
||||
const items = await ListModel
|
||||
.find({})
|
||||
.populate('items')
|
||||
.exec()
|
||||
|
||||
res.send(getAnswer(null, items))
|
||||
})
|
||||
|
||||
// test - http://localhost:8033/todo/list/create/new%20List%20Name
|
||||
router.get('/list/create/:title', async (req, res) => {
|
||||
const { title } = req.params
|
||||
|
||||
const list = await ListModel.create({ title })
|
||||
|
||||
res.send(getAnswer(null, list))
|
||||
})
|
||||
|
||||
// test - http://localhost:8033/todo/item/create/:listId/new%20one
|
||||
router.get('/item/create/:listId/:name', async (req, res, next) => {
|
||||
const { name, listId } = req.params
|
||||
|
||||
try {
|
||||
const list = await ListModel.findById(listId)
|
||||
|
||||
if (!list) {
|
||||
throw new Error('no such list')
|
||||
}
|
||||
|
||||
const item = await ItemModel.create({ name })
|
||||
|
||||
list.addItem(item.id)
|
||||
|
||||
res.send(getAnswer(null, await ListModel.findById(listId)))
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@@ -1,12 +1,26 @@
|
||||
const { Router } = require('express')
|
||||
const { expressjwt } = require('express-jwt')
|
||||
|
||||
const { ListModel } = require('../../data/model/todo/list')
|
||||
const { ItemModel } = require('../../data/model/todo/item')
|
||||
const { getAnswer } = require('../../utils/common')
|
||||
|
||||
const { ListModel } = require('./model/todo/list')
|
||||
const { ItemModel } = require('./model/todo/item')
|
||||
const { TOKEN_KEY } = require('./const')
|
||||
const { requiredValidate } = require('./utils')
|
||||
|
||||
const router = Router()
|
||||
|
||||
// test - http://localhost:8033/todo/list
|
||||
router.use(expressjwt({ secret: TOKEN_KEY, algorithms: ['HS256'] }))
|
||||
|
||||
router.post('/', requiredValidate('title'), async (req, res) => {
|
||||
const { title } = req.body
|
||||
const userId = req.auth.id
|
||||
|
||||
const list = await ListModel.create({ title, createdBy: userId })
|
||||
|
||||
res.send(getAnswer(null, list))
|
||||
})
|
||||
|
||||
router.get('/list', async (req, res) => {
|
||||
const items = await ListModel
|
||||
.find({})
|
||||
@@ -16,34 +30,5 @@ router.get('/list', async (req, res) => {
|
||||
res.send(getAnswer(null, items))
|
||||
})
|
||||
|
||||
// test - http://localhost:8033/todo/list/create/new%20List%20Name
|
||||
router.get('/list/create/:title', async (req, res) => {
|
||||
const { title } = req.params
|
||||
|
||||
const list = await ListModel.create({ title })
|
||||
|
||||
res.send(getAnswer(null, list))
|
||||
})
|
||||
|
||||
// test - http://localhost:8033/todo/item/create/:listId/new%20one
|
||||
router.get('/item/create/:listId/:name', async (req, res, next) => {
|
||||
const { name, listId } = req.params
|
||||
|
||||
try {
|
||||
const list = await ListModel.findById(listId)
|
||||
|
||||
if (!list) {
|
||||
throw new Error('no such list')
|
||||
}
|
||||
|
||||
const item = await ItemModel.create({ name })
|
||||
|
||||
list.addItem(item.id)
|
||||
|
||||
res.send(getAnswer(null, await ListModel.findById(listId)))
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
|
||||
19
server/routers/todo/utils.js
Normal file
19
server/routers/todo/utils.js
Normal file
@@ -0,0 +1,19 @@
|
||||
const requiredValidate =
|
||||
(...fields) =>
|
||||
(req, res, next) => {
|
||||
const errors = []
|
||||
|
||||
fields.forEach((field) => {
|
||||
if (!req.body[field]) {
|
||||
errors.push(field);
|
||||
}
|
||||
})
|
||||
|
||||
if (errors.length) {
|
||||
throw new Error(`Не все поля заполнены: ${errors.join(", ")}`);
|
||||
} else {
|
||||
next()
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.requiredValidate = requiredValidate;
|
||||
Reference in New Issue
Block a user