Compare commits

..

8 Commits

Author SHA1 Message Date
Primakov Alexandr Alexandrovich
38bc0c55c8 1.1.0 2025-01-18 16:52:14 +03:00
Primakov Alexandr Alexandrovich
9fb4219418 todo list get list 2025-01-18 16:52:10 +03:00
Primakov Alexandr Alexandrovich
fb644b6f7b create todo list 2025-01-18 16:50:58 +03:00
Primakov Alexandr Alexandrovich
d88e680413 todo auth 2025-01-18 15:44:14 +03:00
Primakov Alexandr Alexandrovich
8b7f43d15a mode todo in todo folder 2025-01-17 19:51:33 +03:00
b9edd0091c Merge pull request 'delete masters _id' (#60) from feature/arm-master-id into master
Reviewed-on: #60
2025-01-13 17:26:46 +03:00
a927727c1b Merge pull request 'fix: change get for orders' (#59) from fix/orders-get into master
Reviewed-on: #59
2025-01-12 10:43:08 +03:00
a9d9aa02da fix: change get for orders
Some checks failed
platform/multy-stub/pipeline/head There was a failure building this commit
2025-01-12 10:40:50 +03:00
16 changed files with 247 additions and 41 deletions

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{ {
"name": "multi-stub", "name": "multi-stub",
"version": "1.0.1", "version": "1.1.0",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "multi-stub", "name": "multi-stub",
"version": "1.0.1", "version": "1.1.0",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"axios": "^1.7.9", "axios": "^1.7.9",

View File

@@ -1,6 +1,6 @@
{ {
"name": "multi-stub", "name": "multi-stub",
"version": "1.0.1", "version": "1.1.0",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

View File

@@ -1,2 +0,0 @@
exports.TODO_LIST_MODEL_NAME = 'TODO_LIST'
exports.TODO_ITEM_MODEL_NAME = 'TODO_ITEM'

View File

@@ -80,7 +80,7 @@ app.use(require("./root"))
*/ */
app.use("/kfu-m-24-1", require("./routers/kfu-m-24-1")) app.use("/kfu-m-24-1", require("./routers/kfu-m-24-1"))
app.use("/epja-2024-1", require("./routers/epja-2024-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("/dogsitters-finder", require("./routers/dogsitters-finder"))
app.use("/kazan-explore", require("./routers/kazan-explore")) app.use("/kazan-explore", require("./routers/kazan-explore"))
app.use("/edateam", require("./routers/edateam-legacy")) app.use("/edateam", require("./routers/edateam-legacy"))

View File

@@ -1,6 +1,6 @@
const router = require('express').Router() const router = require('express').Router()
router.get('/orders', (req, res) => { router.post('/orders', (req, res) => {
res res
.status(200) .status(200)
.send(require(`./json/arm-orders/success.json`)) .send(require(`./json/arm-orders/success.json`))

View File

@@ -27,7 +27,7 @@ router.put('/new', (req, res) => {
return res.status(500).send('No data to update'); // Internal server error 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 data.push(indexedUpdatedData); // Add the new dictionary to the array

View 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

View 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"

View 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

View 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);

View File

@@ -1,18 +1,22 @@
const { Schema, model } = require('mongoose') 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({ const schema = new Schema({
title: String, title: String,
created: { created: {
type: Date, default: () => new Date().toISOString(), 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 }], items: [{ type: Schema.Types.ObjectId, ref: TODO_ITEM_MODEL_NAME }],
}) })
schema.set('toJSON', { schema.set('toJSON', {
virtuals: true, virtuals: true,
versionKey: false, versionKey: false,
transform: function (doc, ret) {
delete ret._id
}
}) })
schema.virtual('id').get(function () { schema.virtual('id').get(function () {

View 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);

View 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

View File

@@ -1,12 +1,26 @@
const { Router } = require('express') 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 { 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() 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) => { router.get('/list', async (req, res) => {
const items = await ListModel const items = await ListModel
.find({}) .find({})
@@ -16,34 +30,5 @@ router.get('/list', async (req, res) => {
res.send(getAnswer(null, items)) 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 module.exports = router

View 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;