createDrafts
This commit is contained in:
parent
593c43d00d
commit
715ddddb3d
@ -1,4 +0,0 @@
|
|||||||
|
|
||||||
> red-coder-bh@1.2.1 build /Users/teacher/code/stc-22-06/red-coder-bh
|
|
||||||
> tsc
|
|
||||||
|
|
@ -31,6 +31,7 @@
|
|||||||
"@types/cookie-session": "^2.0.44",
|
"@types/cookie-session": "^2.0.44",
|
||||||
"axios": "^0.26.1",
|
"axios": "^0.26.1",
|
||||||
"cookie-session": "^2.0.0",
|
"cookie-session": "^2.0.0",
|
||||||
|
"cors": "^2.8.5",
|
||||||
"cross-env": "^7.0.3",
|
"cross-env": "^7.0.3",
|
||||||
"dotenv": "^16.0.0",
|
"dotenv": "^16.0.0",
|
||||||
"express": "^4.17.3",
|
"express": "^4.17.3",
|
||||||
|
@ -1,2 +1,5 @@
|
|||||||
|
|
||||||
export const usersCollection = 'USERS_COLLECTIONS'
|
export const usersCollection = 'USERS_COLLECTIONS'
|
||||||
|
export const draftsCollection = 'DRAFTS_CLLECTION'
|
||||||
|
export const tasksCollection = 'TASKS_CLLECTION'
|
||||||
|
export const numberCollection = 'NUMBER_COLLECTION'
|
||||||
|
@ -4,6 +4,7 @@ import cookieSession from 'cookie-session'
|
|||||||
import './config'
|
import './config'
|
||||||
import { errorHandle } from './utils/error-handling'
|
import { errorHandle } from './utils/error-handling'
|
||||||
import { router } from './routes'
|
import { router } from './routes'
|
||||||
|
import cors from 'cors'
|
||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
|
|
||||||
@ -12,6 +13,7 @@ const port = process.env.RED_CODER_BH_PORT
|
|||||||
app.use(express.json())
|
app.use(express.json())
|
||||||
app.use(cookieSession({ secret: process.env.COOKIE_SESSION }))
|
app.use(cookieSession({ secret: process.env.COOKIE_SESSION }))
|
||||||
|
|
||||||
|
app.use(cors('*'))
|
||||||
app.use(router)
|
app.use(router)
|
||||||
|
|
||||||
app.use(errorHandle)
|
app.use(errorHandle)
|
||||||
|
@ -1,9 +1,19 @@
|
|||||||
import { Router } from 'express'
|
import { Router } from 'express'
|
||||||
|
import expressjwt from 'express-jwt'
|
||||||
|
|
||||||
import { authRouter } from './v1/auth'
|
import { authRouter } from './v1/auth'
|
||||||
import { bannerRouter } from './v1/banner'
|
import { bannerRouter } from './v1/banner'
|
||||||
|
import tasksRouter from './v1/tasks'
|
||||||
|
import draftsRouter from './v1/drafts'
|
||||||
|
|
||||||
export const router = Router()
|
export const router = Router()
|
||||||
|
|
||||||
|
const jwtMiddlevare = expressjwt({
|
||||||
|
secret: process.env.JWT_SECRET_STRING,
|
||||||
|
algorithms: ['HS256']
|
||||||
|
})
|
||||||
|
|
||||||
router.use(bannerRouter)
|
router.use(bannerRouter)
|
||||||
router.use('/v1/auth', authRouter)
|
router.use('/v1/auth', authRouter)
|
||||||
|
router.use(['/v1/tasks', '/v1/task'], jwtMiddlevare, tasksRouter)
|
||||||
|
router.use('/v1/drafts', jwtMiddlevare, draftsRouter)
|
||||||
|
15
src/routes/v1/drafts.ts
Normal file
15
src/routes/v1/drafts.ts
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import { Router } from 'express'
|
||||||
|
|
||||||
|
import { getAnswer } from '../../utils/common'
|
||||||
|
import { requiredFields } from '../../utils/required'
|
||||||
|
|
||||||
|
import { getDrafts } from './tasks.controller'
|
||||||
|
|
||||||
|
const router = Router()
|
||||||
|
|
||||||
|
router.get('/', async (req, res) => {
|
||||||
|
const drafts = await getDrafts((req as any)?.user?.id)
|
||||||
|
res.send(getAnswer(null, drafts))
|
||||||
|
})
|
||||||
|
|
||||||
|
export default router
|
62
src/routes/v1/tasks.controller.ts
Normal file
62
src/routes/v1/tasks.controller.ts
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
import { Db, ObjectId } from 'mongodb'
|
||||||
|
|
||||||
|
import { mainDb } from '../../utils/mongo'
|
||||||
|
import { cleanId } from '../../utils/common'
|
||||||
|
import { draftsCollection, tasksCollection, numberCollection } from '../../__data__/constants'
|
||||||
|
|
||||||
|
export const createTask = async ({ userId }) => {
|
||||||
|
const db: Db = await mainDb
|
||||||
|
|
||||||
|
const col = db.collection(draftsCollection)
|
||||||
|
const ncol = db.collection(numberCollection)
|
||||||
|
|
||||||
|
const numberObject = await ncol.findOne({})
|
||||||
|
if (!numberObject) {
|
||||||
|
await ncol.insertOne({ number: 1 })
|
||||||
|
}
|
||||||
|
|
||||||
|
ncol.updateOne({}, {
|
||||||
|
$set: {
|
||||||
|
number: (numberObject?.number || 1) + 1
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const draftTask = {
|
||||||
|
cratedBy: new ObjectId(userId),
|
||||||
|
createdDt: Date.now(),
|
||||||
|
lastModufy: Date.now(),
|
||||||
|
names: {
|
||||||
|
'ru': 'Полиндром'
|
||||||
|
},
|
||||||
|
'draft': true,
|
||||||
|
'number': (numberObject?.number || 1) + 1,
|
||||||
|
'markdown': '# Напишите функцию проверки текста на полиндром\\n## Напишите функцию проверки текста на полиндром\\n### Напишите функцию проверки текста на полиндром\\n#### Напишите функцию проверки текста на полиндром\\n##### Напишите функцию проверки текста на полиндром\\n###### Напишите функцию проверки текста на полиндром\\n\\nБлок текста с описанием задачи.\\n[Ссылка](https://reactjs.org/) на документацию реакта\\n\\nНе забудьте [зарегистрироваться](/auth) и [поискать](/main) задачки\\n\\n![react logo](https://tproger.ru/s3/uploads/2016/10/reactmini.png)\\n\\n![line](https://www.clipartmax.com/png/small/44-446497_lines-clipart-vertical-line-blue-vertical-line-png.png)\\n\\n> цитата\\n> цитата\\n> цитата\\n\\n```javascript\\nimport { useCallback, useRef, useEffect } from \'react\';\\nimport { editor as mEditor } from \'monaco-editor\';\\n\\nexport const useMonako = (inputData, remountFlag, language: string, onChange = (value: string) => null) => {\\n const editorRef = useRef<mEditor.IStandaloneCodeEditor>();\\n const nodeRef = useRef(null);\\n const remount = useCallback(() => {\\n editorRef.current?.dispose();\\n editorRef.current = mEditor.create(nodeRef.current, {\\n value: inputData,\\n theme: \'light\' === \'light\' ? \'vs\' : \'vs-dark\',\\n language,\\n });\\n\\n const model = editorRef.current.getModel();\\n model.onDidChangeContent(() => {\\n const value = model.getValue();\\n onChange(value)\\n });\\n } ,[inputData, nodeRef]);\\n\\n useEffect(() => {\\n try {\\n setTimeout(() => {\\n if (nodeRef.current) {\\n remount();\\n\\n () => {\\n editorRef.current.dispose();\\n editorRef.current = null;\\n };\\n }\\n }, 2);\\n } catch (error) {\\n console.error(error);\\n }\\n }, [nodeRef, remountFlag]);\\n\\n return {\\n nodeRef,\\n getValue: () => editorRef?.current?.getValue(),\\n };\\n}\\n\\n```'
|
||||||
|
}
|
||||||
|
|
||||||
|
await col.insertOne(draftTask)
|
||||||
|
|
||||||
|
return cleanId(draftTask)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getTaskData = async (taskId) => {
|
||||||
|
const db: Db = await mainDb
|
||||||
|
|
||||||
|
const col = db.collection(draftsCollection)
|
||||||
|
|
||||||
|
const task = await col.findOne({
|
||||||
|
_id: new ObjectId(taskId),
|
||||||
|
}).catch(console.log)
|
||||||
|
|
||||||
|
return cleanId(task)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getDrafts = async (userId) => {
|
||||||
|
const db: Db = await mainDb
|
||||||
|
|
||||||
|
const col = db.collection(draftsCollection)
|
||||||
|
const tasks = await col.find({
|
||||||
|
cratedBy: new ObjectId(userId),
|
||||||
|
}).toArray()
|
||||||
|
|
||||||
|
return cleanId(tasks)
|
||||||
|
}
|
21
src/routes/v1/tasks.ts
Normal file
21
src/routes/v1/tasks.ts
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import { Router } from 'express'
|
||||||
|
|
||||||
|
import { requiredFields } from '../../utils/required'
|
||||||
|
|
||||||
|
import { createTask, getTaskData } from './tasks.controller'
|
||||||
|
|
||||||
|
const router = Router()
|
||||||
|
|
||||||
|
router.post('/create', async (req, res) => {
|
||||||
|
const draft = await createTask({ userId: (req as any)?.user?.id })
|
||||||
|
res.send(draft)
|
||||||
|
})
|
||||||
|
|
||||||
|
router.post('/data', requiredFields(['id']), async (req, res) => {
|
||||||
|
const { id } = req.body
|
||||||
|
const task = await getTaskData(id)
|
||||||
|
|
||||||
|
res.send(task)
|
||||||
|
})
|
||||||
|
|
||||||
|
export default router
|
@ -2,7 +2,7 @@ const getAnswer = (errors, data, success = true) => {
|
|||||||
if (errors) {
|
if (errors) {
|
||||||
return { success: false, errors }
|
return { success: false, errors }
|
||||||
} else {
|
} else {
|
||||||
return { success, body: data }
|
return { success, data }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user