Compare commits
92 Commits
v1.0.1
...
feature/ch
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fbb5d26c8f | ||
|
|
57eb548d6c | ||
|
|
34d2889c1d | ||
|
|
afc782f50f | ||
|
|
ff6e688206 | ||
|
|
4b0d9b4dbc | ||
| 2cfcd7347b | |||
| c1fe275532 | |||
| de46e9bf50 | |||
|
|
76558a7eef | ||
|
|
4bf3d662ec | ||
|
|
637939e9af | ||
|
|
02891fe301 | ||
| 6015bce32f | |||
| 528b37246b | |||
| 0c693e1bac | |||
| c77aa76645 | |||
|
|
8a0ea25029 | ||
| 6fb3f3f921 | |||
|
|
89c9d7f901 | ||
|
|
b215e22f53 | ||
|
|
f13ae33cb4 | ||
|
|
18a1095cf0 | ||
|
|
3e27c0110c | ||
| c2ada9072f | |||
| 1bd1d2b279 | |||
| 7b1ce2ae83 | |||
| de22881c28 | |||
| 20d370f120 | |||
| 85986d1844 | |||
| dccf769477 | |||
| d088ef7af8 | |||
| fad8cdfe08 | |||
| a55e6d2ab2 | |||
| eb6da8aa1b | |||
| 19a55f57c0 | |||
| b649e86a43 | |||
|
|
c53f016eec | ||
|
|
0a7cbc3434 | ||
|
|
279650db25 | ||
| 2d45b4af72 | |||
| bd4de9d7ba | |||
| 10dbe5c147 | |||
| 435f17ee22 | |||
| 677dc3dc29 | |||
| 88e3fe4172 | |||
| 21dcc8bbec | |||
| 4778dba7ed | |||
| 07a02ade1f | |||
| 311846f92d | |||
| ebd561098f | |||
| b4372eb53a | |||
| bdb6b4ca3e | |||
| 426cb70eb6 | |||
| 61441e21b8 | |||
| a6f61ba033 | |||
| 8588a7fa38 | |||
| c35185ea32 | |||
| 78a332732d | |||
| 5f54355b97 | |||
| 4efbb56905 | |||
| 50c6286190 | |||
| de29da93ba | |||
| 7cdd7e790f | |||
| 569b1b0d9e | |||
| 272961c583 | |||
| 63284d6c08 | |||
| cb56e2dd2c | |||
| 325e2f125b | |||
| 786085d4d0 | |||
| c95a00fb19 | |||
| 95373069da | |||
| b8275a4c8f | |||
| fb868bfefc | |||
| 086c94c58a | |||
| 812a95dd85 | |||
| 85ee64e92b | |||
|
|
eb87f400ee | ||
| 92cabfa048 | |||
|
|
b2a853e183 | ||
|
|
0a6d50cb60 | ||
| 7809806b83 | |||
|
|
1aa0c36c84 | ||
|
|
7c3132c54b | ||
| 8fc85a7de8 | |||
|
|
cb8b2cc5aa | ||
| 6651215eeb | |||
| 80f8cef142 | |||
|
|
5a57291523 | ||
|
|
39ab269f07 | ||
| 7cb23080bf | |||
| d74e4a8624 |
45
.eslintrc.js
@@ -1,45 +0,0 @@
|
||||
module.exports = {
|
||||
env: {
|
||||
browser: true,
|
||||
commonjs: true,
|
||||
es2021: true,
|
||||
},
|
||||
extends: [
|
||||
'airbnb-base',
|
||||
],
|
||||
parserOptions: {
|
||||
ecmaVersion: 12,
|
||||
},
|
||||
rules: {
|
||||
indent: ['error', 4],
|
||||
semi: ['warn', 'never'],
|
||||
'object-curly-newline': ['warn', {
|
||||
ObjectExpression: 'always',
|
||||
ObjectPattern: {
|
||||
multiline: true,
|
||||
},
|
||||
ImportDeclaration: 'never',
|
||||
ExportDeclaration: {
|
||||
multiline: true, minProperties: 3,
|
||||
},
|
||||
}],
|
||||
'consistent-return': [0],
|
||||
'prefer-const': [0],
|
||||
'no-unused-vars': [0],
|
||||
'no-console': [0],
|
||||
'global-require': [0],
|
||||
'no-plusplus': [0],
|
||||
'no-underscore-dangle': [0],
|
||||
'import/no-dynamic-require': [0],
|
||||
'no-shadow': ['warn'],
|
||||
'no-restricted-syntax': ['warn'],
|
||||
'max-len': ['warn'],
|
||||
'linebreak-style': [0],
|
||||
'prefer-destructuring': [0],
|
||||
'imoprt-order': [0],
|
||||
'no-param-reassign': [1],
|
||||
'no-await-in-loop': [1],
|
||||
'no-return-assign': [1],
|
||||
'spaced-comment': [1],
|
||||
},
|
||||
}
|
||||
3
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
node_modules/
|
||||
.env
|
||||
.idea
|
||||
.idea
|
||||
coverage/
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
module.exports = {
|
||||
port: 8044,
|
||||
port: process.env.PORT ?? 8044,
|
||||
mongoAddr: process.env.MONGO_ADDR || 'localhost',
|
||||
mongoPort: 27017,
|
||||
}
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
FROM 'node:18'
|
||||
FROM node:20
|
||||
|
||||
RUN mkdir -p /usr/src/app/server/
|
||||
WORKDIR /usr/src/app/
|
||||
|
||||
COPY ./server /usr/src/app/server
|
||||
COPY ./package.json /usr/src/app/package.json
|
||||
COPY ./package-lock.json /usr/src/app/package-lock.json
|
||||
COPY ./.serverrc.js /usr/src/app/.serverrc.js
|
||||
# COPY ./.env /usr/src/app/.env
|
||||
|
||||
RUN npm i --only=prod
|
||||
# RUN npm i --omit=dev
|
||||
RUN npm ci
|
||||
EXPOSE 8044
|
||||
|
||||
CMD ["npm", "run", "up:prod"]
|
||||
|
||||
16
Jenkinsfile
vendored
@@ -1,7 +1,7 @@
|
||||
pipeline {
|
||||
agent {
|
||||
docker {
|
||||
image 'node:18'
|
||||
image 'node:20'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ pipeline {
|
||||
steps {
|
||||
sh 'node -v'
|
||||
sh 'npm -v'
|
||||
sh 'npm install'
|
||||
sh 'npm ci'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,16 +22,16 @@ pipeline {
|
||||
|
||||
stage('test') {
|
||||
steps {
|
||||
sh 'npm run test:start'
|
||||
sh 'npm run test'
|
||||
}
|
||||
}
|
||||
|
||||
stage('clean-all') {
|
||||
stage('archiving') {
|
||||
steps {
|
||||
sh 'rm -rf .[!.]*'
|
||||
sh 'rm -rf ./*'
|
||||
sh 'ls -a'
|
||||
script {
|
||||
archiveArtifacts artifacts: 'coverage/*/**'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
16
eslint.config.mjs
Normal file
@@ -0,0 +1,16 @@
|
||||
import globals from "globals";
|
||||
import pluginJs from "@eslint/js";
|
||||
|
||||
|
||||
export default [
|
||||
{ ignores: ['server/routers/old/*'] },
|
||||
{ files: ["**/*.js"], languageOptions: { sourceType: "commonjs" } },
|
||||
{ languageOptions: { globals: globals.node } },
|
||||
pluginJs.configs.recommended,
|
||||
{
|
||||
rules: {
|
||||
semi: ['warn', 'never'],
|
||||
'no-unused-vars': [0],
|
||||
}
|
||||
}
|
||||
];
|
||||
201
jest.config.js
Normal file
@@ -0,0 +1,201 @@
|
||||
/**
|
||||
* For a detailed explanation regarding each configuration property, visit:
|
||||
* https://jestjs.io/docs/configuration
|
||||
*/
|
||||
|
||||
/** @type {import('jest').Config} */
|
||||
const config = {
|
||||
// All imported modules in your tests should be mocked automatically
|
||||
// automock: false,
|
||||
|
||||
// Stop running tests after `n` failures
|
||||
// bail: 0,
|
||||
|
||||
// The directory where Jest should store its cached dependency information
|
||||
// cacheDirectory: "C:\\Users\\alex\\AppData\\Local\\Temp\\jest",
|
||||
|
||||
// Automatically clear mock calls, instances, contexts and results before every test
|
||||
clearMocks: true,
|
||||
|
||||
// Indicates whether the coverage information should be collected while executing the test
|
||||
collectCoverage: true,
|
||||
|
||||
// An array of glob patterns indicating a set of files for which coverage information should be collected
|
||||
collectCoverageFrom: [
|
||||
"<rootDir>/server/routers/**/*.js"
|
||||
],
|
||||
|
||||
// The directory where Jest should output its coverage files
|
||||
coverageDirectory: "coverage",
|
||||
|
||||
// An array of regexp pattern strings used to skip coverage collection
|
||||
coveragePathIgnorePatterns: [
|
||||
"\\\\node_modules\\\\",
|
||||
"<rootDir>/server/routers/old"
|
||||
],
|
||||
|
||||
// Indicates which provider should be used to instrument code for coverage
|
||||
coverageProvider: "v8",
|
||||
|
||||
// A list of reporter names that Jest uses when writing coverage reports
|
||||
// coverageReporters: [
|
||||
// "json",
|
||||
// "text",
|
||||
// "lcov",
|
||||
// "clover"
|
||||
// ],
|
||||
|
||||
// An object that configures minimum threshold enforcement for coverage results
|
||||
// coverageThreshold: undefined,
|
||||
|
||||
// A path to a custom dependency extractor
|
||||
// dependencyExtractor: undefined,
|
||||
|
||||
// Make calling deprecated APIs throw helpful error messages
|
||||
// errorOnDeprecated: false,
|
||||
|
||||
// The default configuration for fake timers
|
||||
// fakeTimers: {
|
||||
// "enableGlobally": false
|
||||
// },
|
||||
|
||||
// Force coverage collection from ignored files using an array of glob patterns
|
||||
// forceCoverageMatch: [],
|
||||
|
||||
// A path to a module which exports an async function that is triggered once before all test suites
|
||||
// globalSetup: undefined,
|
||||
|
||||
// A path to a module which exports an async function that is triggered once after all test suites
|
||||
// globalTeardown: undefined,
|
||||
|
||||
// A set of global variables that need to be available in all test environments
|
||||
// globals: {},
|
||||
|
||||
// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
|
||||
// maxWorkers: "50%",
|
||||
|
||||
// An array of directory names to be searched recursively up from the requiring module's location
|
||||
// moduleDirectories: [
|
||||
// "node_modules"
|
||||
// ],
|
||||
|
||||
// An array of file extensions your modules use
|
||||
// moduleFileExtensions: [
|
||||
// "js",
|
||||
// "mjs",
|
||||
// "cjs",
|
||||
// "jsx",
|
||||
// "ts",
|
||||
// "tsx",
|
||||
// "json",
|
||||
// "node"
|
||||
// ],
|
||||
|
||||
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
|
||||
// moduleNameMapper: {},
|
||||
|
||||
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
|
||||
// modulePathIgnorePatterns: [],
|
||||
|
||||
// Activates notifications for test results
|
||||
// notify: false,
|
||||
|
||||
// An enum that specifies notification mode. Requires { notify: true }
|
||||
// notifyMode: "failure-change",
|
||||
|
||||
// A preset that is used as a base for Jest's configuration
|
||||
// preset: undefined,
|
||||
|
||||
// Run tests from one or more projects
|
||||
// projects: undefined,
|
||||
|
||||
// Use this configuration option to add custom reporters to Jest
|
||||
// reporters: undefined,
|
||||
|
||||
// Automatically reset mock state before every test
|
||||
// resetMocks: false,
|
||||
|
||||
// Reset the module registry before running each individual test
|
||||
// resetModules: false,
|
||||
|
||||
// A path to a custom resolver
|
||||
// resolver: undefined,
|
||||
|
||||
// Automatically restore mock state and implementation before every test
|
||||
// restoreMocks: false,
|
||||
|
||||
// The root directory that Jest should scan for tests and modules within
|
||||
// rootDir: undefined,
|
||||
|
||||
// A list of paths to directories that Jest should use to search for files in
|
||||
// roots: [
|
||||
// "<rootDir>"
|
||||
// ],
|
||||
|
||||
// Allows you to use a custom runner instead of Jest's default test runner
|
||||
// runner: "jest-runner",
|
||||
|
||||
// The paths to modules that run some code to configure or set up the testing environment before each test
|
||||
// setupFiles: [],
|
||||
|
||||
// A list of paths to modules that run some code to configure or set up the testing framework before each test
|
||||
// setupFilesAfterEnv: [],
|
||||
|
||||
// The number of seconds after which a test is considered as slow and reported as such in the results.
|
||||
// slowTestThreshold: 5,
|
||||
|
||||
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
|
||||
// snapshotSerializers: [],
|
||||
|
||||
// The test environment that will be used for testing
|
||||
// testEnvironment: "jest-environment-node",
|
||||
|
||||
// Options that will be passed to the testEnvironment
|
||||
// testEnvironmentOptions: {},
|
||||
|
||||
// Adds a location field to test results
|
||||
// testLocationInResults: false,
|
||||
|
||||
// The glob patterns Jest uses to detect test files
|
||||
// testMatch: [
|
||||
// "**/__tests__/**/*.[jt]s?(x)",
|
||||
// "**/?(*.)+(spec|test).[tj]s?(x)"
|
||||
// ],
|
||||
|
||||
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
|
||||
// testPathIgnorePatterns: [
|
||||
// "\\\\node_modules\\\\"
|
||||
// ],
|
||||
|
||||
// The regexp pattern or array of patterns that Jest uses to detect test files
|
||||
// testRegex: [],
|
||||
|
||||
// This option allows the use of a custom results processor
|
||||
// testResultsProcessor: undefined,
|
||||
|
||||
// This option allows use of a custom test runner
|
||||
// testRunner: "jest-circus/runner",
|
||||
|
||||
// A map from regular expressions to paths to transformers
|
||||
// transform: undefined,
|
||||
|
||||
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
|
||||
// transformIgnorePatterns: [
|
||||
// "\\\\node_modules\\\\",
|
||||
// "\\.pnp\\.[^\\\\]+$"
|
||||
// ],
|
||||
|
||||
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
|
||||
// unmockedModulePathPatterns: undefined,
|
||||
|
||||
// Indicates whether each individual test should be reported during the run
|
||||
verbose: true,
|
||||
|
||||
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
|
||||
// watchPathIgnorePatterns: [],
|
||||
|
||||
// Whether to use watchman for file crawling
|
||||
// watchman: true,
|
||||
};
|
||||
|
||||
module.exports = config;
|
||||
5441
package-lock.json
generated
28
package.json
@@ -4,16 +4,15 @@
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "npx nodemon ./server",
|
||||
"start": "cross-env PORT=8033 npx nodemon ./server",
|
||||
"up:prod": "cross-env NODE_ENV=\"production\" node ./server",
|
||||
"deploy:d:stop": "docker-compose down",
|
||||
"deploy:d:build": "docker-compose build",
|
||||
"deploy:d:up": "docker-compose up -d",
|
||||
"deploy:d:stop": "docker compose down",
|
||||
"deploy:d:build": "docker compose build",
|
||||
"deploy:d:up": "docker compose up -d",
|
||||
"redeploy": "npm run deploy:d:stop && npm run deploy:d:build && npm run deploy:d:up",
|
||||
"eslint": "npx eslint ./server",
|
||||
"eslint:fix": "npx eslint ./server --fix",
|
||||
"test": "echo \"test complete\"",
|
||||
"test:start": "start-server-and-test up:prod 8044 test"
|
||||
"test": "jest"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -24,6 +23,7 @@
|
||||
"license": "MIT",
|
||||
"homepage": "https://bitbucket.org/online-mentor/multi-stub#readme",
|
||||
"dependencies": {
|
||||
"axios": "^1.7.7",
|
||||
"bcrypt": "^5.1.0",
|
||||
"body-parser": "^1.19.0",
|
||||
"cookie-parser": "^1.4.5",
|
||||
@@ -37,17 +37,19 @@
|
||||
"jsdom": "^22.1.0",
|
||||
"jsonwebtoken": "^8.5.1",
|
||||
"mongodb": "^3.6.8",
|
||||
"mysql": "^2.18.1",
|
||||
"mongoose": "^8.7.1",
|
||||
"pbkdf2-password": "^1.2.1",
|
||||
"socket.io": "^4.7.1",
|
||||
"start-server-and-test": "^1.13.1",
|
||||
"uuid": "^9.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^9.12.0",
|
||||
"@types/node": "18.17.1",
|
||||
"eslint": "8.46.0",
|
||||
"eslint-config-airbnb-base": "15.0.0",
|
||||
"eslint-plugin-import": "2.28.0",
|
||||
"nodemon": "3.0.1"
|
||||
"eslint": "^9.12.0",
|
||||
"globals": "^15.11.0",
|
||||
"jest": "^29.7.0",
|
||||
"mockingoose": "^2.16.2",
|
||||
"nodemon": "3.0.1",
|
||||
"supertest": "^7.0.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
16
server/__tests__/__snapshots__/todo.test.js.snap
Normal file
@@ -0,0 +1,16 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`todo list app get list 1`] = `
|
||||
{
|
||||
"body": [
|
||||
{
|
||||
"_id": "670f69b5796ce7a9069da2f7",
|
||||
"created": "2024-10-16T07:22:29.042Z",
|
||||
"id": "670f69b5796ce7a9069da2f7",
|
||||
"items": [],
|
||||
"title": "qqq",
|
||||
},
|
||||
],
|
||||
"success": true,
|
||||
}
|
||||
`;
|
||||
34
server/__tests__/todo.test.js
Normal file
@@ -0,0 +1,34 @@
|
||||
const { describe, it, expect } = require('@jest/globals')
|
||||
const request = require('supertest')
|
||||
const express = require('express')
|
||||
const mockingoose = require('mockingoose')
|
||||
const { ListModel } = require('../data/model/todo/list')
|
||||
|
||||
const todo = require('../routers/todo/routes')
|
||||
|
||||
const app = express()
|
||||
app.use(todo)
|
||||
|
||||
const listExample = {
|
||||
"title": "qqq",
|
||||
"items": [],
|
||||
"_id": "670f69b5796ce7a9069da2f7",
|
||||
"created": "2024-10-16T07:22:29.042Z",
|
||||
"id": "670f69b5796ce7a9069da2f7"
|
||||
}
|
||||
|
||||
describe('todo list app', () => {
|
||||
it('get list', (done) => {
|
||||
mockingoose(ListModel)
|
||||
.toReturn([listExample], 'find')
|
||||
.toReturn(listExample, 'create')
|
||||
|
||||
request(app)
|
||||
.get('/list')
|
||||
.expect(200)
|
||||
.then((response) => {
|
||||
expect(response.body).toMatchSnapshot()
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
2
server/data/const.js
Normal file
@@ -0,0 +1,2 @@
|
||||
exports.TODO_LIST_MODEL_NAME = 'TODO_LIST'
|
||||
exports.TODO_ITEM_MODEL_NAME = 'TODO_ITEM'
|
||||
23
server/data/model/todo/item.js
Normal file
@@ -0,0 +1,23 @@
|
||||
const { Schema, model } = require('mongoose')
|
||||
|
||||
const { TODO_ITEM_MODEL_NAME } = require('../../const')
|
||||
|
||||
const schema = new Schema({
|
||||
title: String,
|
||||
done: { type: Boolean, default: false },
|
||||
closed: Date,
|
||||
created: {
|
||||
type: Date, default: () => new Date().toISOString(),
|
||||
},
|
||||
})
|
||||
|
||||
schema.set('toJSON', {
|
||||
virtuals: true,
|
||||
versionKey: false,
|
||||
})
|
||||
|
||||
schema.virtual('id').get(function () {
|
||||
return this._id.toHexString()
|
||||
})
|
||||
|
||||
exports.ItemModel = model(TODO_ITEM_MODEL_NAME, schema)
|
||||
27
server/data/model/todo/list.js
Normal file
@@ -0,0 +1,27 @@
|
||||
const { Schema, model } = require('mongoose')
|
||||
|
||||
const { TODO_LIST_MODEL_NAME, TODO_ITEM_MODEL_NAME } = require('../../const')
|
||||
|
||||
const schema = new Schema({
|
||||
title: String,
|
||||
created: {
|
||||
type: Date, default: () => new Date().toISOString(),
|
||||
},
|
||||
items: [{ type: Schema.Types.ObjectId, ref: TODO_ITEM_MODEL_NAME }],
|
||||
})
|
||||
|
||||
schema.set('toJSON', {
|
||||
virtuals: true,
|
||||
versionKey: false,
|
||||
})
|
||||
|
||||
schema.virtual('id').get(function () {
|
||||
return this._id.toHexString()
|
||||
})
|
||||
|
||||
schema.method('addItem', async function (itemObjectId) {
|
||||
this.items.push(itemObjectId)
|
||||
await this.save()
|
||||
})
|
||||
|
||||
exports.ListModel = model(TODO_LIST_MODEL_NAME, schema)
|
||||
@@ -7,6 +7,8 @@ const app = express()
|
||||
const cors = require('cors')
|
||||
require('dotenv').config()
|
||||
|
||||
exports.app = app
|
||||
|
||||
const config = require('../.serverrc')
|
||||
const { setIo } = require('./io')
|
||||
|
||||
@@ -40,26 +42,8 @@ app.use(require('./root'))
|
||||
/**
|
||||
* Добавляйте сюда свои routers.
|
||||
*/
|
||||
app.use('/lobsters', require('./routers/lobsters'))
|
||||
app.use('/example', require('./routers/example'))
|
||||
// app.use('/coder', require('./routers/coder'))
|
||||
//app.use('/stc-21-03', require('./routers/stc-21-03'))
|
||||
//app.use('/stc-21', require('./routers/stc'))
|
||||
//app.use('/stc-22-24', require('./routers/stc-22-24'))
|
||||
// app.use('/bushou-api', require('./routers/bushou'))
|
||||
|
||||
// app.use('/uryndyklar-api', require('./routers/uryndyklar'))
|
||||
// app.use('/neptunium', require('./routers/neptunium'))
|
||||
// app.use('/music-learn', require('./routers/music-learn'))
|
||||
// app.use('/publicium', require('./routers/publicium'))
|
||||
// app.use('/task-boss', require('./routers/task-boss'))
|
||||
// app.use('/car-wash', require('./routers/car-wash'))
|
||||
app.use('/zoom-bar', require('./routers/zoom-bar'))
|
||||
//app.use('/basket', require('./routers/basket'))
|
||||
//app.use('/easy-project', require('./routers/easy-project'))
|
||||
//app.use('/sugarbun', require('./routers/sugarbun'))
|
||||
app.use('/epja-2023-2', require('./routers/epja-2023-2'))
|
||||
require('./routers/hub-video')
|
||||
app.use('/epja-2024-1', require('./routers/epja-2024-1'))
|
||||
app.use('/todo', require('./routers/todo/routes'))
|
||||
|
||||
app.use(require('./error'))
|
||||
|
||||
|
||||
@@ -1,17 +1,32 @@
|
||||
const router = require('express').Router()
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const router = require('express').Router()
|
||||
const mongoose = require('mongoose')
|
||||
|
||||
const pkg = require('../package.json')
|
||||
|
||||
require('./utils/mongoose')
|
||||
const folderPath = path.resolve(__dirname, './routers')
|
||||
const folders = fs.readdirSync(folderPath)
|
||||
|
||||
router.get('/', (req, res) => {
|
||||
res.send(`
|
||||
router.get('/', async (req, res) => {
|
||||
res.send(`
|
||||
<h1>multy stub is working v${pkg.version}</h1>
|
||||
<ul>
|
||||
${folders.map((f) => `<li>${f}</li>`).join('')}
|
||||
</ul>
|
||||
|
||||
<h2>models</h2>
|
||||
<ul>${
|
||||
(await Promise.all(
|
||||
(
|
||||
await mongoose.modelNames()).map(async (name) => {
|
||||
const count = await mongoose.model(name).countDocuments()
|
||||
return `<li>${name} - ${count}</li>`
|
||||
}
|
||||
)
|
||||
)).map(t => t).join(' ')
|
||||
}</ul>
|
||||
`)
|
||||
})
|
||||
|
||||
|
||||
147
server/routers/epja-2024-1/cats/admin/index.js
Normal file
@@ -0,0 +1,147 @@
|
||||
const adminRouter = require('express').Router()
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const { TOKEN } = require('../const')
|
||||
require('dotenv').config()
|
||||
|
||||
const dataFilePath = path.join(__dirname, '../data.json')
|
||||
let data = require('../data.json')
|
||||
|
||||
const verifyToken = (req, res, next) => {
|
||||
const token = req.headers['authorization']
|
||||
if (token === TOKEN) {
|
||||
next()
|
||||
} else {
|
||||
res.status(403).send({ 'status': 'Failed', 'data': 'Invalid token' })
|
||||
}
|
||||
}
|
||||
|
||||
const saveData = (data) => {
|
||||
fs.writeFileSync(dataFilePath, JSON.stringify(data, null, 2), 'utf-8')
|
||||
}
|
||||
|
||||
adminRouter.post('/edit/nickname', verifyToken, (req, res) => {
|
||||
const { name, colored } = req.body
|
||||
if (!name || !colored) {
|
||||
return res.status(400).send({ 'status': 'Failed', 'data': 'Nickname is required' })
|
||||
}
|
||||
|
||||
data.nickname = { name, colored }
|
||||
saveData(data)
|
||||
|
||||
res.status(200).send({ 'status': 'OK', 'data': 'Nickname updated successfully' })
|
||||
})
|
||||
|
||||
adminRouter.post('/edit/tech-stack', verifyToken, (req, res) => {
|
||||
const { techStack } = req.body
|
||||
if (!techStack || !Array.isArray(techStack)) {
|
||||
return res.status(400).send({ 'status': 'Failed', 'data': 'Valid tech stack is required' })
|
||||
}
|
||||
|
||||
data.techStack = techStack
|
||||
saveData(data)
|
||||
|
||||
res.status(200).send({ 'status': 'OK', 'data': 'Tech stack updated successfully' })
|
||||
})
|
||||
|
||||
adminRouter.post('/edit/city', verifyToken, (req, res) => {
|
||||
const { city } = req.body
|
||||
if (!city) {
|
||||
return res.status(400).send({ 'status': 'Failed', 'data': 'City is required' })
|
||||
}
|
||||
|
||||
const isValid = typeof city === 'object' && 'name' in city && 'href' in city
|
||||
|
||||
if (!isValid) {
|
||||
return res.status(400).send({ 'status': 'Failed', 'data': 'City must contain href and name' })
|
||||
}
|
||||
|
||||
data.city = city
|
||||
saveData(data)
|
||||
|
||||
res.status(200).send({ 'status': 'OK', 'data': 'City updated successfully' })
|
||||
})
|
||||
|
||||
adminRouter.post('/edit/github-repo', verifyToken, (req, res) => {
|
||||
const { github } = req.body
|
||||
if (!github) {
|
||||
return res.status(400).send({ 'status': 'Failed', 'data': 'Github is required' })
|
||||
}
|
||||
|
||||
const isValid = typeof github === 'object' && 'author' in github && 'repo' in github
|
||||
|
||||
if (!isValid) {
|
||||
return res.status(400).send({ 'status': 'Failed', 'data': 'Github must contain author and repo' })
|
||||
}
|
||||
|
||||
data.githubRepo = github
|
||||
saveData(data)
|
||||
|
||||
res.status(200).send({ 'status': 'OK', 'data': 'Github updated successfully' })
|
||||
})
|
||||
|
||||
adminRouter.post('/edit/nav-links', verifyToken, (req, res) => {
|
||||
const { navLinks } = req.body
|
||||
if (!navLinks || !Array.isArray(navLinks)) {
|
||||
return res.status(400).send({ 'status': 'Failed', 'data': 'Valid navLinks are required' })
|
||||
}
|
||||
|
||||
const isValid = navLinks.every(link =>
|
||||
link && typeof link === 'object' && 'href' in link && 'title' in link
|
||||
)
|
||||
|
||||
if (!isValid) {
|
||||
return res.status(400).send({ 'status': 'Failed', 'data': 'Each navLink must contain href and title' })
|
||||
}
|
||||
|
||||
data.navLinks = navLinks
|
||||
saveData(data)
|
||||
|
||||
res.status(200).send({ 'status': 'OK', 'data': 'Navigation links updated successfully' })
|
||||
})
|
||||
|
||||
adminRouter.post('/edit/links', verifyToken, (req, res) => {
|
||||
const { links } = req.body
|
||||
if (!links || !Array.isArray(links)) {
|
||||
return res.status(400).send({ 'status': 'Failed', 'data': 'Valid links are required' })
|
||||
}
|
||||
|
||||
const isValid = links.every(link =>
|
||||
link && typeof link === 'object' && 'href' in link && 'title' in link
|
||||
)
|
||||
|
||||
if (!isValid) {
|
||||
return res.status(400).send({ 'status': 'Failed', 'data': 'Each link must contain href and title' })
|
||||
}
|
||||
|
||||
data.links = links
|
||||
saveData(data)
|
||||
|
||||
res.status(200).send({ 'status': 'OK', 'data': 'Links updated successfully' })
|
||||
})
|
||||
|
||||
adminRouter.post('/edit/projects', verifyToken, (req, res) => {
|
||||
const { projects } = req.body
|
||||
if (!projects) {
|
||||
return res.status(400).send({ 'status': 'Failed', 'data': 'Projects are required' })
|
||||
}
|
||||
|
||||
const projectFields = ['id', 'title', 'description', 'link', 'techStack', 'image']
|
||||
|
||||
const isValidProject = (project) => {
|
||||
return projectFields.every(field => field && field in project)
|
||||
}
|
||||
|
||||
const allProjectsValid = projects.every(project => isValidProject(project))
|
||||
|
||||
if (!allProjectsValid) {
|
||||
return res.status(400).send({ 'status': 'Failed', 'data': 'All projects must contain ' + projectFields.join(", ") })
|
||||
}
|
||||
|
||||
data.projects = projects
|
||||
saveData(data)
|
||||
|
||||
res.status(200).send({ 'status': 'OK', 'data': 'Projects updated successfully' })
|
||||
})
|
||||
|
||||
module.exports = adminRouter
|
||||
16
server/routers/epja-2024-1/cats/auth/index.js
Normal file
@@ -0,0 +1,16 @@
|
||||
const authRouter = require('express').Router()
|
||||
const { TOKEN } = require('../const')
|
||||
|
||||
|
||||
module.exports = authRouter
|
||||
|
||||
authRouter.post('/login', (req, res) => {
|
||||
const { email, password } = req.body
|
||||
console.log(`Login with email=${email} and password=${password}`)
|
||||
|
||||
if (email === 'admin@admin.admin' && password === 'admin') {
|
||||
res.status(200).send({ 'status': 'OK', 'data': `${TOKEN}` })
|
||||
} else {
|
||||
res.status(401).send({ 'status': 'Failed!', 'data': 'Invalid email or password' })
|
||||
}
|
||||
})
|
||||
3
server/routers/epja-2024-1/cats/const.js
Normal file
@@ -0,0 +1,3 @@
|
||||
const TOKEN = "ASDFGHJKLLKJHGFDSDFGHJKJHGF"
|
||||
|
||||
module.exports = { TOKEN }
|
||||
118
server/routers/epja-2024-1/cats/data.json
Normal file
@@ -0,0 +1,118 @@
|
||||
{
|
||||
"nickname": {
|
||||
"name": "supercool",
|
||||
"colored": "nickname"
|
||||
},
|
||||
"techStack": [
|
||||
"React",
|
||||
"Next.js",
|
||||
"Svelte",
|
||||
"SvelteKit",
|
||||
"HTML",
|
||||
"CSS",
|
||||
"JavaScript",
|
||||
"TypeScript",
|
||||
"TailwindCSS",
|
||||
"Styled-Components",
|
||||
"Framer-Motion",
|
||||
"shadcn-ui",
|
||||
"Ant-Design",
|
||||
"ESLint",
|
||||
"Prettier",
|
||||
"husky",
|
||||
"lint-staged",
|
||||
"Redux",
|
||||
"RTK Query",
|
||||
"Tanstack Query",
|
||||
"Python",
|
||||
"FastApi",
|
||||
"Flask",
|
||||
"SQLite",
|
||||
"PostgreSQL",
|
||||
"MongoDB",
|
||||
"SQLAlchemy",
|
||||
"Alembic",
|
||||
"PyPy",
|
||||
"poetry",
|
||||
"pylint",
|
||||
"GitHub Actions",
|
||||
"GitLab CI/CD",
|
||||
"Docker",
|
||||
"Git"
|
||||
],
|
||||
"githubRepo": {
|
||||
"author": "MishaBlin",
|
||||
"repo": "EPJA_portfolio_app"
|
||||
},
|
||||
"city": {
|
||||
"name": "Innopolis",
|
||||
"href": "https://ru.wikipedia.org/wiki/Иннополис"
|
||||
},
|
||||
"navLinks": [
|
||||
{
|
||||
"href": "#about",
|
||||
"title": "About"
|
||||
},
|
||||
{
|
||||
"href": "#projects",
|
||||
"title": "Projects"
|
||||
}
|
||||
],
|
||||
"links": [
|
||||
{
|
||||
"href": "mailto:svyatoslavsvyatkin@yandex.ru",
|
||||
"title": "Mail"
|
||||
},
|
||||
{
|
||||
"href": "https://t.me/dmhd6219",
|
||||
"title": "Telegram"
|
||||
},
|
||||
{
|
||||
"href": "https://github.com/dmhd6219",
|
||||
"title": "GitHub"
|
||||
},
|
||||
{
|
||||
"href": "https://last.fm/user/dmhd",
|
||||
"title": "LastFm"
|
||||
},
|
||||
{
|
||||
"href": "https://pay.cloudtips.ru/p/02da9349",
|
||||
"title": "Buy me a coffee"
|
||||
}
|
||||
],
|
||||
"projects": [
|
||||
{
|
||||
"title": "Elasticsearch",
|
||||
"description": "Elasticsearch is a distributed search and analytics engine, scalable data store and vector database optimized for speed and relevance on production-scale workloads.",
|
||||
"link": "https://github.com/elastic/elasticsearch",
|
||||
"techStack": [
|
||||
"Java",
|
||||
"Groovy"
|
||||
],
|
||||
"image": "https://datascientest.com/en/files/2023/04/Elasticsearch.jpg",
|
||||
"id": "elasticsearch"
|
||||
},
|
||||
{
|
||||
"title": "React-native",
|
||||
"description": "React Native brings React's declarative UI framework to iOS and Android. With React Native, you use native UI controls and have full access to the native platform.",
|
||||
"link": "https://github.com/facebook/react-native",
|
||||
"techStack": [
|
||||
"C++",
|
||||
"Javascript",
|
||||
"Kotlin"
|
||||
],
|
||||
"image": "https://www.simplilearn.com/ice9/free_resources_article_thumb/React_Native_Tutorial.jpg",
|
||||
"id": "react-native"
|
||||
},
|
||||
{
|
||||
"title": "Zarr",
|
||||
"description": "Zarr is a Python package providing an implementation of compressed, chunked, N-dimensional arrays, designed for use in parallel computing. See the documentation for more information.",
|
||||
"link": "https://github.com/zarr-developers/zarr-python",
|
||||
"techStack": [
|
||||
"Python"
|
||||
],
|
||||
"image": "https://raw.githubusercontent.com/zarr-developers/community/main/logos/logo2.png",
|
||||
"id": "zarr"
|
||||
}
|
||||
]
|
||||
}
|
||||
11
server/routers/epja-2024-1/cats/index.js
Normal file
@@ -0,0 +1,11 @@
|
||||
const authRouter = require('./auth')
|
||||
const adminRouter = require('./admin')
|
||||
const rootRouter = require('./root')
|
||||
|
||||
const router = require('express').Router()
|
||||
|
||||
module.exports = router
|
||||
|
||||
router.use(`/auth`, authRouter)
|
||||
router.use(`/admin`, adminRouter)
|
||||
router.use(`/`, rootRouter)
|
||||
44
server/routers/epja-2024-1/cats/root/index.js
Normal file
@@ -0,0 +1,44 @@
|
||||
const rootRouter = require('express').Router()
|
||||
const data = require('../data.json')
|
||||
|
||||
rootRouter.get('/get/nickname', (req, res) => {
|
||||
res.status(200).send({ 'status': 'OK', 'data': data.nickname })
|
||||
})
|
||||
|
||||
rootRouter.get('/get/tech-stack', (req, res) => {
|
||||
res.status(200).send({ 'status': 'OK', 'data': data.techStack })
|
||||
})
|
||||
|
||||
rootRouter.get('/get/github-repo', (req, res) => {
|
||||
res.status(200).send({ 'status': 'OK', 'data': data.githubRepo })
|
||||
})
|
||||
|
||||
rootRouter.get('/get/city', (req, res) => {
|
||||
res.status(200).send({ 'status': 'OK', 'data': data.city })
|
||||
})
|
||||
|
||||
rootRouter.get('/get/nav-links', (req, res) => {
|
||||
res.status(200).send({ 'status': 'OK', 'data': data.navLinks })
|
||||
})
|
||||
|
||||
rootRouter.get('/get/links', (req, res) => {
|
||||
res.status(200).send({ 'status': 'OK', 'data': data.links })
|
||||
})
|
||||
|
||||
rootRouter.get('/get/projects', (req, res) => {
|
||||
res.status(200).send({ 'status': 'OK', 'data': data.projects })
|
||||
})
|
||||
|
||||
rootRouter.get('/get/projects/:id', (req, res) => {
|
||||
const { id } = req.params
|
||||
const project = data.projects.find(p => p.id === id)
|
||||
|
||||
if (project) {
|
||||
res.status(200).send({ status: 'OK', data: project })
|
||||
} else {
|
||||
res.status(404).send({ status: 'NOT_FOUND', message: 'Project not found' })
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
module.exports = rootRouter
|
||||
9
server/routers/epja-2024-1/ecliptica/index.js
Normal file
@@ -0,0 +1,9 @@
|
||||
const plantsRouter = require('./plants/getPlants')
|
||||
const calendarRouter = require('./plants_calendar/index')
|
||||
|
||||
const router = require('express').Router()
|
||||
|
||||
module.exports = router
|
||||
|
||||
router.use('/plants',plantsRouter)
|
||||
router.use('/plants_calendar',calendarRouter)
|
||||
6
server/routers/epja-2024-1/ecliptica/plants/config.js
Normal file
@@ -0,0 +1,6 @@
|
||||
const CONFIG = {
|
||||
CLIENT_ID: 'kD2JXj1faSCYAWdT4B069wQAx89CZAkXmzTinRvH',
|
||||
CLIENT_SECRET: 'bJq7Uiwua52tHiLP80N60hALNtQX2wcE4Mj6yNA9OzG2iZbgHuqyeAs6WSWX6MNJdfv0Nqzb7OHta8qPZr4zxWBLTauleaMfraln3xFEvbXLDpi1Lcrwe7DxfgsQQ1E4',
|
||||
}
|
||||
|
||||
module.exports = CONFIG
|
||||
143
server/routers/epja-2024-1/ecliptica/plants/getPlants.js
Normal file
@@ -0,0 +1,143 @@
|
||||
const express = require ('express')
|
||||
const axios = require ('axios')
|
||||
const plantsRouter = express.Router ()
|
||||
const CONFIG = require ('./config')
|
||||
|
||||
|
||||
async function getAccessToken () {
|
||||
const formData = new FormData ()
|
||||
formData.append ('grant_type', 'client_credentials')
|
||||
formData.append ('client_id', CONFIG.CLIENT_ID)
|
||||
formData.append ('client_secret', CONFIG.CLIENT_SECRET)
|
||||
|
||||
try {
|
||||
const response = await axios.post (
|
||||
'https://open.plantbook.io/api/v1/token/',
|
||||
formData,
|
||||
{
|
||||
headers: {'Content-Type': 'multipart/form-data'},
|
||||
}
|
||||
)
|
||||
if (response.data && response.data.access_token) {
|
||||
console.log ('Access token retrieved:', response.data.access_token)
|
||||
return response.data.access_token
|
||||
} else {
|
||||
console.error ('Error: access_token not found in response')
|
||||
return null
|
||||
}
|
||||
} catch (error) {
|
||||
console.error (
|
||||
'Error fetching access token:',
|
||||
error.response ? error.response.data : error.message
|
||||
)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchPlantData (plantId) {
|
||||
const accessToken = await getAccessToken ()
|
||||
if (!accessToken) {
|
||||
return null
|
||||
}
|
||||
try {
|
||||
const response = await axios.get (
|
||||
`https://open.plantbook.io/api/v1/plant/detail/${encodeURIComponent (plantId)}/`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
},
|
||||
}
|
||||
)
|
||||
return response.data
|
||||
} catch (error) {
|
||||
console.error (
|
||||
'Error fetching plant data:',
|
||||
error.response ? error.response.data : error.message
|
||||
)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
plantsRouter.get ('/list', async (req, res) => {
|
||||
const accessToken = await getAccessToken ()
|
||||
if (!accessToken) {
|
||||
res.status (500).send ({message: 'Error obtaining access token'})
|
||||
return
|
||||
}
|
||||
try {
|
||||
const alias = req.query.alias || 'monstera'
|
||||
const response = await axios.get (
|
||||
'https://open.plantbook.io/api/v1/plant/search',
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
},
|
||||
params: {
|
||||
alias: alias,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
const plantsList = response.data.results
|
||||
|
||||
const plants = await Promise.all (
|
||||
plantsList.map (async plant => {
|
||||
const plantDetails = await fetchPlantData (plant.pid)
|
||||
return {
|
||||
id: plant.pid,
|
||||
alias: plant.alias,
|
||||
display_name: plant.display_name,
|
||||
image_url: plantDetails ? plantDetails.image_url : null,
|
||||
max_light: plantDetails ? plantDetails.max_light : null,
|
||||
min_light: plantDetails ? plantDetails.min_light : null,
|
||||
max_temp: plantDetails ? plantDetails.max_temp : null,
|
||||
min_temp: plantDetails ? plantDetails.min_temp : null,
|
||||
max_env_humid: plantDetails ? plantDetails.max_env_humid : null,
|
||||
min_env_humid: plantDetails ? plantDetails.min_env_humid : null,
|
||||
max_soil_moist: plantDetails ? plantDetails.max_soil_moist : null,
|
||||
min_soil_moist: plantDetails ? plantDetails.min_soil_moist : null,
|
||||
max_soil_ec: plantDetails ? plantDetails.max_soil_ec : null,
|
||||
min_soil_ec: plantDetails ? plantDetails.min_soil_ec : null,
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
res.send ({results: plants})
|
||||
} catch (error) {
|
||||
console.error (
|
||||
'Error fetching plant list:',
|
||||
error.response ? error.response.data : error.message
|
||||
)
|
||||
res.status (500).send ({message: 'Error fetching plant list'})
|
||||
}
|
||||
})
|
||||
|
||||
plantsRouter.get ('/:id', async (req, res) => {
|
||||
const plantId = req.params.id
|
||||
const plantData = await fetchPlantData (plantId)
|
||||
|
||||
if (plantData) {
|
||||
const detailedPlantData = {
|
||||
id: plantData.pid,
|
||||
display_name: plantData.display_name,
|
||||
alias: plantData.alias,
|
||||
max_light: plantData.max_light,
|
||||
min_light: plantData.min_light,
|
||||
max_temperature: plantData.max_temp,
|
||||
min_temperature: plantData.min_temp,
|
||||
max_humidity: plantData.max_env_humid,
|
||||
min_humidity: plantData.min_env_humid,
|
||||
max_soil_moisture: plantData.max_soil_moist,
|
||||
min_soil_moisture: plantData.min_soil_moist,
|
||||
max_soil_ec: plantData.max_soil_ec,
|
||||
min_soil_ec: plantData.min_soil_ec,
|
||||
image_url: plantData.image_url,
|
||||
}
|
||||
|
||||
res.send (detailedPlantData)
|
||||
} else {
|
||||
res.status (404).send ({message: 'Plant not found'})
|
||||
}
|
||||
})
|
||||
|
||||
module.exports = plantsRouter
|
||||
@@ -0,0 +1,42 @@
|
||||
const express = require('express')
|
||||
|
||||
const plantsRouter = express.Router()
|
||||
|
||||
const plants = [
|
||||
{
|
||||
name: "Rose",
|
||||
image: "https://ervanarium.com.br/wp-content/uploads/2019/03/cactus-3142589_1920.jpg",
|
||||
frequency: 3,
|
||||
startDate: "2024-10-09",
|
||||
},
|
||||
{
|
||||
name: "Sunflower",
|
||||
image: "https://avatars.mds.yandex.net/i?id=31da587c9aabc83ad3615023f91d7284781be06c-10701700-images-thumbs&n=13",
|
||||
frequency: 3,
|
||||
startDate: "2024-10-05",
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
const calculateWateringDates = (startDate, frequency) => {
|
||||
const dates = []
|
||||
const start = new Date(startDate)
|
||||
|
||||
for (let i = 0; i < 30; i += frequency) {
|
||||
const nextWateringDate = new Date(start)
|
||||
nextWateringDate.setDate(start.getDate() + i)
|
||||
dates.push(nextWateringDate.toISOString().split('T')[0])
|
||||
}
|
||||
return dates
|
||||
}
|
||||
|
||||
const plantsWithDates = plants.map(plant => ({
|
||||
...plant,
|
||||
wateringDates: calculateWateringDates(plant.startDate, plant.frequency),
|
||||
}))
|
||||
|
||||
plantsRouter.get('/api/plants', (req, res) => {
|
||||
res.json(plantsWithDates)
|
||||
})
|
||||
|
||||
module.exports = plantsRouter
|
||||
73
server/routers/epja-2024-1/enterfront/auth/index.js
Normal file
@@ -0,0 +1,73 @@
|
||||
const authRouter = require('express').Router()
|
||||
|
||||
// For creating tokens
|
||||
const jwt = require('jsonwebtoken')
|
||||
|
||||
const { TOKEN_KEY } = require('../key')
|
||||
|
||||
|
||||
module.exports = authRouter
|
||||
|
||||
const { addUserToDB, getUserFromDB } = require('../db')
|
||||
|
||||
|
||||
// Get a user by its id
|
||||
authRouter.get('/:id', (req, res) => {
|
||||
const user = getUserFromDB(req.params.id)
|
||||
|
||||
if (user) {
|
||||
res.status(200).send({user})
|
||||
} else {
|
||||
res.status(404).send({message: 'User was not found'})
|
||||
}
|
||||
})
|
||||
|
||||
// For login (authorization)
|
||||
authRouter.post('/login', (req, res) => {
|
||||
const { name, password } = req.body
|
||||
|
||||
const user = getUserFromDB(name)
|
||||
|
||||
// Invalid identification
|
||||
if (!user) {
|
||||
res.status(401).send({message: 'Invalid credentials (id)'})
|
||||
return
|
||||
}
|
||||
|
||||
// Invalid authentication
|
||||
if (!password || password !== user.password) {
|
||||
res.status(401).send({message: 'Invalid credentials (password)'})
|
||||
return
|
||||
}
|
||||
|
||||
// Now, authorization
|
||||
const token = jwt.sign({id: name}, TOKEN_KEY, {
|
||||
expiresIn: '1h'
|
||||
})
|
||||
|
||||
res.status(200).send({token})
|
||||
})
|
||||
|
||||
|
||||
authRouter.post('/reg', (req, res) => {
|
||||
const { name, password, nickname } = req.body
|
||||
|
||||
const user = getUserFromDB(name)
|
||||
|
||||
// Invalid identification
|
||||
if (user) {
|
||||
res.status(409).send({message: 'Such id already exists'})
|
||||
return
|
||||
}
|
||||
|
||||
if (!name || !password || !nickname) {
|
||||
res.status(401).send({message: 'Empty or invalid fields'})
|
||||
return
|
||||
}
|
||||
|
||||
// Add to 'DB'
|
||||
const newUser = {id: name, password: password, nickname: nickname}
|
||||
addUserToDB(newUser)
|
||||
|
||||
res.status(200).send({user: newUser})
|
||||
})
|
||||
52
server/routers/epja-2024-1/enterfront/auth/users.json
Normal file
@@ -0,0 +1,52 @@
|
||||
[
|
||||
{
|
||||
"nickname": "Alice Johnson",
|
||||
"password": "1234",
|
||||
"id": "alice"
|
||||
},
|
||||
{
|
||||
"nickname": "Bob Smith",
|
||||
"password": "1234",
|
||||
"id": "bobsm"
|
||||
},
|
||||
{
|
||||
"nickname": "Charlie Brown",
|
||||
"password": "1234",
|
||||
"id": "charl"
|
||||
},
|
||||
{
|
||||
"nickname": "David Clark",
|
||||
"password": "1234",
|
||||
"id": "david"
|
||||
},
|
||||
{
|
||||
"nickname": "Eve Adams",
|
||||
"password": "1234",
|
||||
"id": "evead"
|
||||
},
|
||||
{
|
||||
"nickname": "Frank Wright",
|
||||
"password": "1234",
|
||||
"id": "frank"
|
||||
},
|
||||
{
|
||||
"nickname": "Grace Lee",
|
||||
"password": "1234",
|
||||
"id": "grace"
|
||||
},
|
||||
{
|
||||
"nickname": "Hannah Scott",
|
||||
"password": "1234",
|
||||
"id": "hanna"
|
||||
},
|
||||
{
|
||||
"nickname": "Ian Davis",
|
||||
"password": "1234",
|
||||
"id": "ianda"
|
||||
},
|
||||
{
|
||||
"nickname": "Jill Thompson",
|
||||
"password": "1234",
|
||||
"id": "jillt"
|
||||
}
|
||||
]
|
||||
64
server/routers/epja-2024-1/enterfront/change/index.js
Normal file
@@ -0,0 +1,64 @@
|
||||
const changeRouter = require('express').Router()
|
||||
|
||||
module.exports = changeRouter
|
||||
|
||||
const { getUserFromDB, deleteUserFromDB, addUserToDB } = require('../db')
|
||||
|
||||
|
||||
changeRouter.post('/nickname', (req, res) => {
|
||||
const { id, newNickname } = req.body
|
||||
|
||||
const user = getUserFromDB(id)
|
||||
|
||||
// Invalid identification
|
||||
if (!user) {
|
||||
res.status(401).send({message: 'Invalid credentials (id)'})
|
||||
return
|
||||
}
|
||||
|
||||
const updatedUser = {
|
||||
"nickname": newNickname,
|
||||
"password": user.password,
|
||||
"id": user.id
|
||||
}
|
||||
|
||||
// Delete the old one
|
||||
deleteUserFromDB(id)
|
||||
|
||||
// Insert updated
|
||||
addUserToDB(updatedUser)
|
||||
|
||||
res.status(200).send({})
|
||||
})
|
||||
|
||||
changeRouter.post('/password', (req, res) => {
|
||||
const { id, newPassword } = req.body
|
||||
|
||||
const user = getUserFromDB(id)
|
||||
|
||||
// Invalid identification
|
||||
if (!user) {
|
||||
res.status(401).send({message: 'Invalid credentials (id)'})
|
||||
return
|
||||
}
|
||||
|
||||
// Delete the old one
|
||||
deleteUserFromDB(id)
|
||||
|
||||
// Insert updated
|
||||
const updatedUser = {
|
||||
"nickname": user.nickname,
|
||||
"password": newPassword,
|
||||
"id": user.id
|
||||
}
|
||||
addUserToDB(updatedUser)
|
||||
|
||||
res.status(200).send({})
|
||||
})
|
||||
|
||||
changeRouter.delete('/:id', (req, res) => {
|
||||
const { id } = req.params
|
||||
|
||||
deleteUserFromDB(id)
|
||||
})
|
||||
|
||||
662
server/routers/epja-2024-1/enterfront/chat/chats.json
Normal file
@@ -0,0 +1,662 @@
|
||||
[
|
||||
{
|
||||
"id1": "alice",
|
||||
"id2": "bobsm",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Hello Bob!",
|
||||
"senderId": "alice",
|
||||
"recipientId": "bobsm",
|
||||
"timestamp": "09.10.2024 07:00:00"
|
||||
},
|
||||
{
|
||||
"data": "Hey Alice, how are you?",
|
||||
"senderId": "bobsm",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 07:05:00"
|
||||
},
|
||||
{
|
||||
"data": "I'm good, thanks for asking.",
|
||||
"senderId": "alice",
|
||||
"recipientId": "bobsm",
|
||||
"timestamp": "09.10.2024 07:10:00"
|
||||
},
|
||||
{
|
||||
"data": "Glad to hear!",
|
||||
"senderId": "bobsm",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 07:15:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "alice",
|
||||
"id2": "charl",
|
||||
"messages": [
|
||||
{
|
||||
"data": "How's the project going?",
|
||||
"senderId": "alice",
|
||||
"recipientId": "charl",
|
||||
"timestamp": "09.10.2024 07:20:00"
|
||||
},
|
||||
{
|
||||
"data": "It's coming along, almost done!",
|
||||
"senderId": "charl",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 07:25:00"
|
||||
},
|
||||
{
|
||||
"data": "That's great to hear!",
|
||||
"senderId": "alice",
|
||||
"recipientId": "charl",
|
||||
"timestamp": "09.10.2024 07:30:00"
|
||||
},
|
||||
{
|
||||
"data": "Thanks for checking in.",
|
||||
"senderId": "charl",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 07:35:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "alice",
|
||||
"id2": "david",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Did you get the files?",
|
||||
"senderId": "david",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 07:40:00"
|
||||
},
|
||||
{
|
||||
"data": "Yes, I did. Thank you!",
|
||||
"senderId": "alice",
|
||||
"recipientId": "david",
|
||||
"timestamp": "09.10.2024 07:45:00"
|
||||
},
|
||||
{
|
||||
"data": "You're welcome.",
|
||||
"senderId": "david",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 07:50:00"
|
||||
},
|
||||
{
|
||||
"data": "Let me know if you need anything else.",
|
||||
"senderId": "alice",
|
||||
"recipientId": "david",
|
||||
"timestamp": "09.10.2024 07:55:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "alice",
|
||||
"id2": "frank",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Can you review this document for me?",
|
||||
"senderId": "alice",
|
||||
"recipientId": "frank",
|
||||
"timestamp": "09.10.2024 08:20:00"
|
||||
},
|
||||
{
|
||||
"data": "Sure, I'll take a look.",
|
||||
"senderId": "frank",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 08:25:00"
|
||||
},
|
||||
{
|
||||
"data": "Thanks, much appreciated!",
|
||||
"senderId": "alice",
|
||||
"recipientId": "frank",
|
||||
"timestamp": "09.10.2024 08:30:00"
|
||||
},
|
||||
{
|
||||
"data": "No problem.",
|
||||
"senderId": "frank",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 08:35:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "alice",
|
||||
"id2": "grace",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Hey Grace, let's meet up for coffee!",
|
||||
"senderId": "alice",
|
||||
"recipientId": "grace",
|
||||
"timestamp": "09.10.2024 08:40:00"
|
||||
},
|
||||
{
|
||||
"data": "Sounds good, when are you free?",
|
||||
"senderId": "grace",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 08:45:00"
|
||||
},
|
||||
{
|
||||
"data": "How about tomorrow afternoon?",
|
||||
"senderId": "alice",
|
||||
"recipientId": "grace",
|
||||
"timestamp": "09.10.2024 08:50:00"
|
||||
},
|
||||
{
|
||||
"data": "Works for me!",
|
||||
"senderId": "grace",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 08:55:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "alice",
|
||||
"id2": "hanna",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Hannah, do you have a moment?",
|
||||
"senderId": "alice",
|
||||
"recipientId": "hanna",
|
||||
"timestamp": "09.10.2024 09:00:00"
|
||||
},
|
||||
{
|
||||
"data": "Sure, what's up?",
|
||||
"senderId": "hanna",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 09:05:00"
|
||||
},
|
||||
{
|
||||
"data": "Just wanted to check on the report.",
|
||||
"senderId": "alice",
|
||||
"recipientId": "hanna",
|
||||
"timestamp": "09.10.2024 09:10:00"
|
||||
},
|
||||
{
|
||||
"data": "I'll send it soon.",
|
||||
"senderId": "hanna",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 09:15:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "alice",
|
||||
"id2": "ianda",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Ian, have you completed the review?",
|
||||
"senderId": "alice",
|
||||
"recipientId": "ianda",
|
||||
"timestamp": "09.10.2024 09:20:00"
|
||||
},
|
||||
{
|
||||
"data": "Yes, I sent my feedback.",
|
||||
"senderId": "ianda",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 09:25:00"
|
||||
},
|
||||
{
|
||||
"data": "Thanks for that.",
|
||||
"senderId": "alice",
|
||||
"recipientId": "ianda",
|
||||
"timestamp": "09.10.2024 09:30:00"
|
||||
},
|
||||
{
|
||||
"data": "Anytime!",
|
||||
"senderId": "ianda",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 09:35:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "alice",
|
||||
"id2": "jillt",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Jill, let's schedule a catch-up meeting.",
|
||||
"senderId": "alice",
|
||||
"recipientId": "jillt",
|
||||
"timestamp": "09.10.2024 09:40:00"
|
||||
},
|
||||
{
|
||||
"data": "Sounds good, when works for you?",
|
||||
"senderId": "jillt",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 09:45:00"
|
||||
},
|
||||
{
|
||||
"data": "Tomorrow afternoon?",
|
||||
"senderId": "alice",
|
||||
"recipientId": "jillt",
|
||||
"timestamp": "09.10.2024 09:50:00"
|
||||
},
|
||||
{
|
||||
"data": "That works for me!",
|
||||
"senderId": "jillt",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 09:55:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "alice",
|
||||
"id2": "evead",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Eve, did you send the schedule?",
|
||||
"senderId": "alice",
|
||||
"recipientId": "evead",
|
||||
"timestamp": "09.10.2024 10:00:00"
|
||||
},
|
||||
{
|
||||
"data": "Yes, just sent it.",
|
||||
"senderId": "evead",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 10:05:00"
|
||||
},
|
||||
{
|
||||
"data": "Thanks, much appreciated!",
|
||||
"senderId": "alice",
|
||||
"recipientId": "evead",
|
||||
"timestamp": "09.10.2024 10:10:00"
|
||||
},
|
||||
{
|
||||
"data": "No problem!",
|
||||
"senderId": "evead",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 10:15:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "bobsm",
|
||||
"id2": "charl",
|
||||
"messages": [
|
||||
{
|
||||
"data": "How's everything going?",
|
||||
"senderId": "bobsm",
|
||||
"recipientId": "charl",
|
||||
"timestamp": "09.10.2024 10:20:00"
|
||||
},
|
||||
{
|
||||
"data": "Pretty good, how about you?",
|
||||
"senderId": "charl",
|
||||
"recipientId": "bobsm",
|
||||
"timestamp": "09.10.2024 10:25:00"
|
||||
},
|
||||
{
|
||||
"data": "Can't complain!",
|
||||
"senderId": "bobsm",
|
||||
"recipientId": "charl",
|
||||
"timestamp": "09.10.2024 10:30:00"
|
||||
},
|
||||
{
|
||||
"data": "Glad to hear that.",
|
||||
"senderId": "charl",
|
||||
"recipientId": "bobsm",
|
||||
"timestamp": "09.10.2024 10:35:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "bobsm",
|
||||
"id2": "david",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Can you send the report?",
|
||||
"senderId": "bobsm",
|
||||
"recipientId": "david",
|
||||
"timestamp": "09.10.2024 10:40:00"
|
||||
},
|
||||
{
|
||||
"data": "I'll send it in an hour.",
|
||||
"senderId": "david",
|
||||
"recipientId": "bobsm",
|
||||
"timestamp": "09.10.2024 10:45:00"
|
||||
},
|
||||
{
|
||||
"data": "Perfect, thanks.",
|
||||
"senderId": "bobsm",
|
||||
"recipientId": "david",
|
||||
"timestamp": "09.10.2024 10:50:00"
|
||||
},
|
||||
{
|
||||
"data": "No problem.",
|
||||
"senderId": "david",
|
||||
"recipientId": "bobsm",
|
||||
"timestamp": "09.10.2024 10:55:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "charl",
|
||||
"id2": "evead",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Hey Eve, how's it going?",
|
||||
"senderId": "charl",
|
||||
"recipientId": "evead",
|
||||
"timestamp": "09.10.2024 11:00:00"
|
||||
},
|
||||
{
|
||||
"data": "Good, how about you?",
|
||||
"senderId": "evead",
|
||||
"recipientId": "charl",
|
||||
"timestamp": "09.10.2024 11:05:00"
|
||||
},
|
||||
{
|
||||
"data": "Can't complain!",
|
||||
"senderId": "charl",
|
||||
"recipientId": "evead",
|
||||
"timestamp": "09.10.2024 11:10:00"
|
||||
},
|
||||
{
|
||||
"data": "Glad to hear.",
|
||||
"senderId": "evead",
|
||||
"recipientId": "charl",
|
||||
"timestamp": "09.10.2024 11:15:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "charl",
|
||||
"id2": "frank",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Do you have time to talk today?",
|
||||
"senderId": "charl",
|
||||
"recipientId": "frank",
|
||||
"timestamp": "09.10.2024 11:20:00"
|
||||
},
|
||||
{
|
||||
"data": "I have a meeting, but I can chat afterward.",
|
||||
"senderId": "frank",
|
||||
"recipientId": "charl",
|
||||
"timestamp": "09.10.2024 11:25:00"
|
||||
},
|
||||
{
|
||||
"data": "Sounds good.",
|
||||
"senderId": "charl",
|
||||
"recipientId": "frank",
|
||||
"timestamp": "09.10.2024 11:30:00"
|
||||
},
|
||||
{
|
||||
"data": "I'll message you after.",
|
||||
"senderId": "frank",
|
||||
"recipientId": "charl",
|
||||
"timestamp": "09.10.2024 11:35:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "david",
|
||||
"id2": "frank",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Did you review the document?",
|
||||
"senderId": "david",
|
||||
"recipientId": "frank",
|
||||
"timestamp": "09.10.2024 11:40:00"
|
||||
},
|
||||
{
|
||||
"data": "Yes, it's all good.",
|
||||
"senderId": "frank",
|
||||
"recipientId": "david",
|
||||
"timestamp": "09.10.2024 11:45:00"
|
||||
},
|
||||
{
|
||||
"data": "Great, thanks for the quick turnaround!",
|
||||
"senderId": "david",
|
||||
"recipientId": "frank",
|
||||
"timestamp": "09.10.2024 11:50:00"
|
||||
},
|
||||
{
|
||||
"data": "No worries!",
|
||||
"senderId": "frank",
|
||||
"recipientId": "david",
|
||||
"timestamp": "09.10.2024 11:55:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "david",
|
||||
"id2": "grace",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Grace, can you send the updated schedule?",
|
||||
"senderId": "david",
|
||||
"recipientId": "grace",
|
||||
"timestamp": "09.10.2024 12:00:00"
|
||||
},
|
||||
{
|
||||
"data": "Yes, I'll send it in a few minutes.",
|
||||
"senderId": "grace",
|
||||
"recipientId": "david",
|
||||
"timestamp": "09.10.2024 12:05:00"
|
||||
},
|
||||
{
|
||||
"data": "Thanks, much appreciated!",
|
||||
"senderId": "david",
|
||||
"recipientId": "grace",
|
||||
"timestamp": "09.10.2024 12:10:00"
|
||||
},
|
||||
{
|
||||
"data": "You're welcome!",
|
||||
"senderId": "grace",
|
||||
"recipientId": "david",
|
||||
"timestamp": "09.10.2024 12:15:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "frank",
|
||||
"id2": "grace",
|
||||
"messages": [
|
||||
{
|
||||
"data": "How are you today?",
|
||||
"senderId": "frank",
|
||||
"recipientId": "grace",
|
||||
"timestamp": "09.10.2024 12:20:00"
|
||||
},
|
||||
{
|
||||
"data": "I'm doing well, thanks for asking.",
|
||||
"senderId": "grace",
|
||||
"recipientId": "frank",
|
||||
"timestamp": "09.10.2024 12:25:00"
|
||||
},
|
||||
{
|
||||
"data": "Glad to hear that.",
|
||||
"senderId": "frank",
|
||||
"recipientId": "grace",
|
||||
"timestamp": "09.10.2024 12:30:00"
|
||||
},
|
||||
{
|
||||
"data": "How about you?",
|
||||
"senderId": "grace",
|
||||
"recipientId": "frank",
|
||||
"timestamp": "09.10.2024 12:35:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "frank",
|
||||
"id2": "hanna",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Did you attend the meeting?",
|
||||
"senderId": "frank",
|
||||
"recipientId": "hanna",
|
||||
"timestamp": "09.10.2024 12:40:00"
|
||||
},
|
||||
{
|
||||
"data": "Yes, it was productive.",
|
||||
"senderId": "hanna",
|
||||
"recipientId": "frank",
|
||||
"timestamp": "09.10.2024 12:45:00"
|
||||
},
|
||||
{
|
||||
"data": "Good to hear!",
|
||||
"senderId": "frank",
|
||||
"recipientId": "hanna",
|
||||
"timestamp": "09.10.2024 12:50:00"
|
||||
},
|
||||
{
|
||||
"data": "Indeed, lots to follow up on.",
|
||||
"senderId": "hanna",
|
||||
"recipientId": "frank",
|
||||
"timestamp": "09.10.2024 12:55:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "grace",
|
||||
"id2": "hanna",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Can we meet later today?",
|
||||
"senderId": "grace",
|
||||
"recipientId": "hanna",
|
||||
"timestamp": "09.10.2024 01:00:00"
|
||||
},
|
||||
{
|
||||
"data": "Sure, what's a good time?",
|
||||
"senderId": "hanna",
|
||||
"recipientId": "grace",
|
||||
"timestamp": "09.10.2024 01:05:00"
|
||||
},
|
||||
{
|
||||
"data": "How about 3?",
|
||||
"senderId": "grace",
|
||||
"recipientId": "hanna",
|
||||
"timestamp": "09.10.2024 01:10:00"
|
||||
},
|
||||
{
|
||||
"data": "Works for me.",
|
||||
"senderId": "hanna",
|
||||
"recipientId": "grace",
|
||||
"timestamp": "09.10.2024 01:15:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "grace",
|
||||
"id2": "ianda",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Ian, did you get the message I sent?",
|
||||
"senderId": "grace",
|
||||
"recipientId": "ianda",
|
||||
"timestamp": "09.10.2024 01:20:00"
|
||||
},
|
||||
{
|
||||
"data": "Yes, I'll respond soon.",
|
||||
"senderId": "ianda",
|
||||
"recipientId": "grace",
|
||||
"timestamp": "09.10.2024 01:25:00"
|
||||
},
|
||||
{
|
||||
"data": "Thanks, appreciate it!",
|
||||
"senderId": "grace",
|
||||
"recipientId": "ianda",
|
||||
"timestamp": "09.10.2024 01:30:00"
|
||||
},
|
||||
{
|
||||
"data": "You're welcome!",
|
||||
"senderId": "ianda",
|
||||
"recipientId": "grace",
|
||||
"timestamp": "09.10.2024 01:35:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "hanna",
|
||||
"id2": "ianda",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Ian, do you have a minute?",
|
||||
"senderId": "hanna",
|
||||
"recipientId": "ianda",
|
||||
"timestamp": "09.10.2024 01:40:00"
|
||||
},
|
||||
{
|
||||
"data": "Yes, what do you need?",
|
||||
"senderId": "ianda",
|
||||
"recipientId": "hanna",
|
||||
"timestamp": "09.10.2024 01:45:00"
|
||||
},
|
||||
{
|
||||
"data": "Just a quick update on the project.",
|
||||
"senderId": "hanna",
|
||||
"recipientId": "ianda",
|
||||
"timestamp": "09.10.2024 01:50:00"
|
||||
},
|
||||
{
|
||||
"data": "I'll email you the details.",
|
||||
"senderId": "ianda",
|
||||
"recipientId": "hanna",
|
||||
"timestamp": "09.10.2024 01:55:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "hanna",
|
||||
"id2": "jillt",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Jill, can we talk tomorrow?",
|
||||
"senderId": "hanna",
|
||||
"recipientId": "jillt",
|
||||
"timestamp": "09.10.2024 02:00:00"
|
||||
},
|
||||
{
|
||||
"data": "Yes, I'm free after 2.",
|
||||
"senderId": "jillt",
|
||||
"recipientId": "hanna",
|
||||
"timestamp": "09.10.2024 02:05:00"
|
||||
},
|
||||
{
|
||||
"data": "Perfect, see you then.",
|
||||
"senderId": "hanna",
|
||||
"recipientId": "jillt",
|
||||
"timestamp": "09.10.2024 02:10:00"
|
||||
},
|
||||
{
|
||||
"data": "Looking forward to it.",
|
||||
"senderId": "jillt",
|
||||
"recipientId": "hanna",
|
||||
"timestamp": "09.10.2024 02:15:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "ianda",
|
||||
"id2": "jillt",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Jill, I have the files you requested.",
|
||||
"senderId": "ianda",
|
||||
"recipientId": "jillt",
|
||||
"timestamp": "09.10.2024 02:20:00"
|
||||
},
|
||||
{
|
||||
"data": "Thanks, please send them over.",
|
||||
"senderId": "jillt",
|
||||
"recipientId": "ianda",
|
||||
"timestamp": "09.10.2024 02:25:00"
|
||||
},
|
||||
{
|
||||
"data": "I'll send them right now.",
|
||||
"senderId": "ianda",
|
||||
"recipientId": "jillt",
|
||||
"timestamp": "09.10.2024 02:30:00"
|
||||
},
|
||||
{
|
||||
"data": "Great, thanks again!",
|
||||
"senderId": "jillt",
|
||||
"recipientId": "ianda",
|
||||
"timestamp": "09.10.2024 02:35:00"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
86
server/routers/epja-2024-1/enterfront/chat/index.js
Normal file
@@ -0,0 +1,86 @@
|
||||
const chatRouter = require('express').Router()
|
||||
|
||||
module.exports = chatRouter
|
||||
|
||||
const { getChatFromDB, getUsersChats, addChatToDB, getUserFromDB,
|
||||
addMessageToChat} = require('../db')
|
||||
|
||||
chatRouter.get('/item/:id1/:id2', (req, res) => {
|
||||
const { id1, id2 } = req.params
|
||||
|
||||
if (id1 === id2) {
|
||||
res.status(400).send({message: 'Ids should be different'})
|
||||
return
|
||||
}
|
||||
|
||||
const chat = getChatFromDB(id1, id2)
|
||||
|
||||
if (chat) {
|
||||
res.status(200).send({chat})
|
||||
} else {
|
||||
res.status(404).send({message: 'Chat was not found'})
|
||||
}
|
||||
})
|
||||
|
||||
chatRouter.post('/item/:id1/:id2', (req, res) => {
|
||||
const { id1, id2 } = req.params
|
||||
|
||||
if (id1 === id2) {
|
||||
res.status(400).send({message: 'Ids should be different'})
|
||||
return
|
||||
}
|
||||
|
||||
const chat = getChatFromDB(id1, id2)
|
||||
|
||||
if (chat) {
|
||||
// Chat already exists
|
||||
res.status(200).send({chat})
|
||||
} else {
|
||||
if (!getUserFromDB(id1) || !getUserFromDB(id2)) {
|
||||
res.status(404).send({message: 'Such interlocutor does not exist'})
|
||||
} else {
|
||||
// Creating new chat
|
||||
const newChat = {
|
||||
id1: id1,
|
||||
id2: id2,
|
||||
messages: []
|
||||
}
|
||||
|
||||
addChatToDB(newChat)
|
||||
|
||||
res.status(200).send({newChat})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
chatRouter.get('/list/:id', (req, res) => {
|
||||
const { id } = req.params
|
||||
|
||||
const userChats = getUsersChats(id)
|
||||
|
||||
if (!userChats) {
|
||||
res.status(404).send({message: 'Error with retrieving chats'})
|
||||
} else {
|
||||
res.status(200).send({chats: userChats})
|
||||
}
|
||||
})
|
||||
|
||||
chatRouter.post('/message/:sender/:receiver', (req, res) => {
|
||||
const { sender, receiver } = req.params
|
||||
const { message } = req.body
|
||||
|
||||
const chat = getChatFromDB(sender, receiver)
|
||||
|
||||
if (!chat) {
|
||||
// Chat already exists
|
||||
res.status(400).send({message: "Such chat does not exist"})
|
||||
} else {
|
||||
if (!getUserFromDB(sender) || !getUserFromDB(receiver)) {
|
||||
res.status(404).send({message: 'Such people do not exist'})
|
||||
} else {
|
||||
// Add new message
|
||||
addMessageToChat(chat, message)
|
||||
res.status(200).send({})
|
||||
}
|
||||
}
|
||||
})
|
||||
74
server/routers/epja-2024-1/enterfront/db.js
Normal file
@@ -0,0 +1,74 @@
|
||||
// Read already defined users (pseudo-DB)
|
||||
const users = require('./auth/users.json')
|
||||
const chats = require('./chat/chats.json')
|
||||
|
||||
const getUserFromDB = (userID) => {
|
||||
if (!userID) {return false}
|
||||
|
||||
// Accessing 'DB'
|
||||
const user = users.find((user) => user.id === userID)
|
||||
|
||||
if (user) {
|
||||
return user
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
const deleteUserFromDB = (userID) => {
|
||||
const index = users.findIndex(item => item.id === userID)
|
||||
if (index !== -1) {
|
||||
users.splice(index, 1)
|
||||
}
|
||||
}
|
||||
|
||||
const addUserToDB = (user) => {
|
||||
users.push(user)
|
||||
}
|
||||
|
||||
const getChatFromDB = (firstID, secondID) => {
|
||||
if (!firstID || !secondID) {return false}
|
||||
|
||||
// Accessing 'DB'
|
||||
const chat = chats.find((item) =>
|
||||
(item.id1 === firstID && item.id2 === secondID) || (item.id1 === secondID && item.id2 === firstID))
|
||||
|
||||
if (chat) {
|
||||
return chat
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
const getUsersChats = (userID) => {
|
||||
if (!userID) {return false}
|
||||
|
||||
const userChats = chats.filter((chat) => (chat.id1 === userID || chat.id2 === userID))
|
||||
|
||||
if (userChats) {
|
||||
return userChats
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
const addMessageToChat = (chat, msg) => {
|
||||
chat.messages.push(msg)
|
||||
}
|
||||
|
||||
const deleteChatFromDB = (firstID, secondID) => {
|
||||
const index = chats.findIndex(item =>
|
||||
(item.id1 === firstID && item.id2 === secondID) || (item.id1 === secondID && item.id2 === firstID))
|
||||
|
||||
if (index !== -1) {
|
||||
chats.splice(index, 1)
|
||||
}
|
||||
}
|
||||
|
||||
const addChatToDB = (chat) => {
|
||||
chats.push(chat)
|
||||
}
|
||||
|
||||
|
||||
module.exports = {users, chats, getUserFromDB, getChatFromDB, addUserToDB,
|
||||
deleteUserFromDB, addChatToDB, deleteChatFromDB, getUsersChats, addMessageToChat}
|
||||
17
server/routers/epja-2024-1/enterfront/index.js
Normal file
@@ -0,0 +1,17 @@
|
||||
const changeRouter = require("./change")
|
||||
const authRouter = require("./auth")
|
||||
const chatRouter = require("./chat")
|
||||
|
||||
const router = require('express').Router()
|
||||
|
||||
const delay = require('./middlewares/delay')
|
||||
const verify = require('./middlewares/verify')
|
||||
|
||||
module.exports = router
|
||||
|
||||
// router.use(delay(300));
|
||||
// router.use('/books', delay, booksRouter);
|
||||
|
||||
router.use('/auth', authRouter)
|
||||
router.use('/change', verify, changeRouter)
|
||||
router.use('/chat', verify, chatRouter)
|
||||
3
server/routers/epja-2024-1/enterfront/key.js
Normal file
@@ -0,0 +1,3 @@
|
||||
const TOKEN_KEY = '5frv12e4few3r'
|
||||
|
||||
module.exports = { TOKEN_KEY }
|
||||
@@ -0,0 +1,5 @@
|
||||
const delay = (ms = 1000) => (req, res, next) => {
|
||||
setTimeout(next, ms)
|
||||
}
|
||||
|
||||
module.exports = delay
|
||||
22
server/routers/epja-2024-1/enterfront/middlewares/verify.js
Normal file
@@ -0,0 +1,22 @@
|
||||
const jwt = require('jsonwebtoken')
|
||||
|
||||
const { TOKEN_KEY } = require('../key')
|
||||
|
||||
function verifyToken(req, res, next) {
|
||||
const token = req.headers['authorization']?.split(' ')[1]
|
||||
|
||||
if (!token) {
|
||||
return res.status(401).send({ message: 'No token provided' })
|
||||
}
|
||||
|
||||
// Verify token
|
||||
jwt.verify(token, TOKEN_KEY, (err, decoded) => {
|
||||
if (err) {
|
||||
return res.status(401).send({ message: 'Unauthorized' })
|
||||
}
|
||||
|
||||
next() // Proceed to the next middleware or route
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = verifyToken
|
||||
10
server/routers/epja-2024-1/index.js
Normal file
@@ -0,0 +1,10 @@
|
||||
const { Router } = require('express')
|
||||
const router = Router()
|
||||
|
||||
router.use('/enterfront', require('./enterfront/index'))
|
||||
|
||||
router.use('/cats', require('./cats/index'))
|
||||
|
||||
router.use('/ecliptica', require('./ecliptica/index'))
|
||||
|
||||
module.exports = router
|
||||
@@ -1,19 +0,0 @@
|
||||
const router = require('express').Router()
|
||||
|
||||
const first = router.get('/first', (req, res) => {
|
||||
res.send({
|
||||
success: true,
|
||||
warnings: [{
|
||||
title: 'Внимание',
|
||||
text: 'Данный api создан для примера!',
|
||||
}],
|
||||
})
|
||||
|
||||
/**
|
||||
* Этот эндпоинт будет доступен по адресу http://89.223.91.151:8080/multystub/example/first
|
||||
*/
|
||||
})
|
||||
|
||||
router.use('/example-api', first)
|
||||
|
||||
module.exports = router
|
||||
@@ -1,7 +1,7 @@
|
||||
const ObjectId = require('mongodb').ObjectID
|
||||
const getHash = require('pbkdf2-password')()
|
||||
|
||||
const { getDB } = require('../../utils/mongo')
|
||||
const { getDB } = require('../../../utils/mongo')
|
||||
|
||||
const USERS_COLLECTION = 'users'
|
||||
const LISTS_COLLECTION = 'lists'
|
||||
|
Before Width: | Height: | Size: 377 B After Width: | Height: | Size: 377 B |
|
Before Width: | Height: | Size: 470 B After Width: | Height: | Size: 470 B |
|
Before Width: | Height: | Size: 578 B After Width: | Height: | Size: 578 B |
|
Before Width: | Height: | Size: 481 B After Width: | Height: | Size: 481 B |
|
Before Width: | Height: | Size: 368 B After Width: | Height: | Size: 368 B |
|
Before Width: | Height: | Size: 593 B After Width: | Height: | Size: 593 B |
|
Before Width: | Height: | Size: 539 B After Width: | Height: | Size: 539 B |
|
Before Width: | Height: | Size: 414 B After Width: | Height: | Size: 414 B |
|
Before Width: | Height: | Size: 414 B After Width: | Height: | Size: 414 B |
|
Before Width: | Height: | Size: 739 B After Width: | Height: | Size: 739 B |
|
Before Width: | Height: | Size: 686 B After Width: | Height: | Size: 686 B |
|
Before Width: | Height: | Size: 750 B After Width: | Height: | Size: 750 B |
|
Before Width: | Height: | Size: 687 B After Width: | Height: | Size: 687 B |
|
Before Width: | Height: | Size: 706 B After Width: | Height: | Size: 706 B |
|
Before Width: | Height: | Size: 776 B After Width: | Height: | Size: 776 B |
|
Before Width: | Height: | Size: 628 B After Width: | Height: | Size: 628 B |
|
Before Width: | Height: | Size: 742 B After Width: | Height: | Size: 742 B |
|
Before Width: | Height: | Size: 853 B After Width: | Height: | Size: 853 B |
|
Before Width: | Height: | Size: 911 B After Width: | Height: | Size: 911 B |
|
Before Width: | Height: | Size: 601 B After Width: | Height: | Size: 601 B |
|
Before Width: | Height: | Size: 648 B After Width: | Height: | Size: 648 B |
|
Before Width: | Height: | Size: 709 B After Width: | Height: | Size: 709 B |
|
Before Width: | Height: | Size: 442 B After Width: | Height: | Size: 442 B |
|
Before Width: | Height: | Size: 858 B After Width: | Height: | Size: 858 B |
|
Before Width: | Height: | Size: 718 B After Width: | Height: | Size: 718 B |
|
Before Width: | Height: | Size: 1015 B After Width: | Height: | Size: 1015 B |
|
Before Width: | Height: | Size: 1.0 KiB After Width: | Height: | Size: 1.0 KiB |
|
Before Width: | Height: | Size: 811 B After Width: | Height: | Size: 811 B |
|
Before Width: | Height: | Size: 872 B After Width: | Height: | Size: 872 B |
|
Before Width: | Height: | Size: 659 B After Width: | Height: | Size: 659 B |
|
Before Width: | Height: | Size: 767 B After Width: | Height: | Size: 767 B |
|
Before Width: | Height: | Size: 719 B After Width: | Height: | Size: 719 B |
|
Before Width: | Height: | Size: 1.0 KiB After Width: | Height: | Size: 1.0 KiB |
|
Before Width: | Height: | Size: 812 B After Width: | Height: | Size: 812 B |
|
Before Width: | Height: | Size: 760 B After Width: | Height: | Size: 760 B |
|
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.3 KiB |
|
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |
|
Before Width: | Height: | Size: 955 B After Width: | Height: | Size: 955 B |