interim commit
This commit is contained in:
parent
d062fa0e6f
commit
8d721a88c7
20 changed files with 17329 additions and 152 deletions
|
|
@ -1,10 +1,11 @@
|
|||
import * as bodyParser from 'body-parser'
|
||||
import cors from 'cors'
|
||||
import express from 'express'
|
||||
import { configGetHandler, configGetSpecificHandler } from './app/controllers/configGetHandler'
|
||||
import { configPatchHandler } from './app/controllers/configPatchHandler'
|
||||
import { configPutHandler } from './app/controllers/configPutHandler'
|
||||
import { loginPostHandler } from './app/controllers/loginPostHandler'
|
||||
import { registerPostHandler } from './app/controllers/registerPostHandler'
|
||||
import { configGetHandler, configGetSpecificHandler } from './app/controllers/configGetHandler'
|
||||
import { configPutHandler } from './app/controllers/configPutHandler'
|
||||
import { UserModel } from './app/db/user.schema'
|
||||
import { auth, optAuth } from './app/middleware'
|
||||
|
||||
|
|
@ -28,6 +29,7 @@ app.use(bodyParser.urlencoded({
|
|||
|
||||
app.put('/api/configs', optAuth, configPutHandler)
|
||||
app.get('/api/configs', auth, configGetHandler)
|
||||
app.patch('/api/configs/:id', auth, configPatchHandler)
|
||||
app.get('/api/configs/:id', configGetSpecificHandler)
|
||||
app.post('/api/users/login', loginPostHandler)
|
||||
app.post('/api/users/register', registerPostHandler)
|
||||
|
|
|
|||
|
|
@ -3,7 +3,14 @@ import ConfigSchema from '../db/config.schema'
|
|||
|
||||
export const configGetHandler = async (req: Request, res: Response) => {
|
||||
try {
|
||||
return res.json(req.user?.configs)
|
||||
const configs = []
|
||||
|
||||
for (const _id of req.user?.configs) {
|
||||
const { name } = await ConfigSchema.findById(_id)
|
||||
configs.push({ _id, name })
|
||||
}
|
||||
|
||||
return res.json(configs)
|
||||
} catch (ex) {
|
||||
console.error(ex)
|
||||
return res.status(500).send({ error: ex })
|
||||
|
|
|
|||
22
backend/src/app/controllers/configPatchHandler.ts
Normal file
22
backend/src/app/controllers/configPatchHandler.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import { Request, Response } from 'express'
|
||||
import ConfigSchema from '../db/config.schema'
|
||||
|
||||
export const configPatchHandler = async (req: Request, res: Response) => {
|
||||
try {
|
||||
const config = await ConfigSchema.findById(req.params.id)
|
||||
|
||||
if (!req.user?.configs.includes(config._id)) {
|
||||
return res.status(403).send()
|
||||
}
|
||||
|
||||
config.json = req.body.json
|
||||
config.name = req.body.name
|
||||
|
||||
await config.save()
|
||||
|
||||
return res.status(200).send(config._id)
|
||||
} catch (ex) {
|
||||
console.error(ex)
|
||||
return res.status(500).send({ error: ex })
|
||||
}
|
||||
}
|
||||
|
|
@ -5,9 +5,10 @@ export const configPutHandler = async (req: Request, res: Response) => {
|
|||
try {
|
||||
const config = new ConfigSchema()
|
||||
config.json = req.body.json
|
||||
config.name = req.body.name
|
||||
await config.save()
|
||||
|
||||
if(req.user) {
|
||||
if (req.user) {
|
||||
req.user.configs.push(config._id)
|
||||
await req.user.save()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,11 +6,16 @@ const ConfigSchema = new Schema({
|
|||
type: String,
|
||||
default: shortid.generate
|
||||
},
|
||||
json: Object
|
||||
name: {
|
||||
type: String,
|
||||
default: "Unnamed"
|
||||
},
|
||||
json: Object,
|
||||
})
|
||||
|
||||
export type ConfigModel = Document & {
|
||||
_id: string,
|
||||
name: string,
|
||||
json: object,
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue