initial commit

This commit is contained in:
Matthieu Bessat 2020-06-27 23:23:09 +02:00
commit 32b5df8c22
10 changed files with 1749 additions and 0 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
dist
node_modules

22
package.json Normal file
View file

@ -0,0 +1,22 @@
{
"dependencies": {
"@types/express": "^4.17.6",
"@types/mongoose": "^5.7.28",
"body-parser": "^1.19.0",
"express": "^4.17.1",
"mongoose": "^5.9.20",
"typescript": "^3.9.5"
},
"scripts": {
"test": "jest --verbose --detectOpenHandles",
"test-watch": "jest --verbose --watchAll",
"dev": "./node_modules/.bin/tsc --watch",
"serve": "./node_modules/.bin/nodemon dist/app.js",
"build": "./node_modules/.bin/tsc"
},
"main": "dist/index.js",
"devDependencies": {
"nodemon": "^2.0.4",
"supertest": "^4.0.2"
}
}

25
src/app.ts Normal file
View file

@ -0,0 +1,25 @@
import express from 'express'
import DefaultController from './controllers/DefaultController'
import AdminTagController from './controllers/AdminTagController'
import mongoose from 'mongoose'
import bodyParser from 'body-parser'
const app: express.Application = express()
const host: string = "0.0.0.0"
const port: number = 8001
mongoose.connect('mongodb://localhost:27017/forumvirt', {useNewUrlParser: true, useUnifiedTopology: true});
app.use(bodyParser.json())
app.get('/', DefaultController.home)
app.get('/api/tags', AdminTagController.getTags)
app.put('/api/tags/:id', AdminTagController.updateTag)
app.post('/api/tags', AdminTagController.storeTag)
app.delete('/api/tags/:id', AdminTagController.destroyTag)
app.listen(port, host, () => {
console.log(`API listening on ${host}:${port}`)
})

View file

@ -0,0 +1,9 @@
import Organization from '../models/Organization'
export default class AdminOrganizationController {
static getOrganizations(req, res) {
Organization.find().then(data => {
res.json(data)
})
}
}

View file

@ -0,0 +1,58 @@
import Tag from '../models/Tag'
import express from 'express'
export default class AdminTagController {
static getTags(_: any, res: express.Response) {
Tag.find().then(data => {
res.json(data)
})
}
static storeTag(req: express.Request, res: express.Response) {
Tag.create(req.body).then(data => {
res.json({
success: true,
data
})
}).catch(err => {
res.status(400).json({
success: false,
errors: err.errors
})
})
}
static updateTag(req: express.Request, res: express.Response) {
Tag.updateOne({ _id: req.params.id }, req.body).then(data => {
res.json({
success: true,
data
})
}).catch(err => {
let errors
if (err.errors !== undefined) {
errors = err.errors
} else {
errors = err
}
res.status(400).json({
success: false,
errors
})
})
}
static destroyTag(req: express.Request, res: express.Response) {
Tag.deleteOne({ _id: req.params.id }).then(data => {
res.json({
success: true,
data
})
}).catch(err => {
res.status(400).json({
success: false,
errors: err.errors
})
})
}
}

View file

@ -0,0 +1,26 @@
import Organization from '../models/Organization'
import * as express from 'express'
export default class DefaultController {
static home(req: express.Request, res: express.Response) {
// Organization.create({
// admin_name: "hello",
// email: 'spamfree@matthieubessat.fr',
// token: 'dqsdsqdsq',
// validationState: "ndsqqdsd"
// }).then(data => {
// console.log(data)
// res.json({
// success: true
// })
// }).catch(err => {
// res.json({
// success: false,
// errors: err.errors
// })
// })
res.json({
success: true
})
}
}

View file

@ -0,0 +1,95 @@
import mongoose, { Schema } from 'mongoose'
import { Tag } from './Tag'
interface AllowedStringOptions {
name: string;
allowedValues: string[]
}
class AllowedString extends mongoose.SchemaType {
name: String
allowedValues: string[] = []
constructor(key: string, options: AllowedStringOptions) {
super(key, options)
this.allowedValues = options.allowedValues
this.name = options.name
}
cast(value: any) {
console.log(value + ' is being validated')
if (this.allowedValues.indexOf(value) === -1) {
let validationStr: string = 'AllowedString: ' + value + ' must be one of which: [' + this.allowedValues.join(', ') + ']'
console.log(validationStr)
throw new Error(validationStr)
}
return value
}
}
// @ts-ignore
mongoose.Schema.Types['AllowedString'] = AllowedString
const ScheduleIntervalBoundary = new Schema({
hour: { type: Number },
minute: { type: Number }
})
const Media = new Schema({
location: { type: String, required: true },
bucket: { type: String },
key: { type: String },
type: {
type: AllowedString,
name: 'MediaType',
allowedValues: ['cover', 'thumbnail', 'video', 'image']
}
})
const OrganizationVersion = new Schema({
name: { type: String, required: true },
descriptionShort: { type: String, required: true },
descriptionLong: { type: String, required: true },
thumbnail: { type: Media, required: true },
gallery: [{ type: Media }],
cover: { type: Media },
contacts: [{
name: { type: String, required: true },
type: { type: String, required: true }, // (facebook,twitter,instagram,website,address,person,email,phone)
content: { type: String, required: true },
}],
schedule: [{
name: { type: String, required: true },
description: { type: String },
day: { type: String }, // (Mon,Tue,Wed,Thu,Fri,Sat,Sun)
from: { type: ScheduleIntervalBoundary, required: true },
to: { type: ScheduleIntervalBoundary, required: true }
}],
pricing: [{
name: { type: String, required: true },
priceLabel: { type: String, required: true },
description: { type: String }
}],
tag: { type: Tag }
})
const Organization = new Schema({
admin_name: { type: String, required: true },
email: { type: String, required: true },
token: { type: String, required: true },
validationState: {
type: AllowedString,
required: true,
default: 'none',
name: 'ValidationState',
allowedValues: ['none', 'pending', 'rejected', 'published']
},
rejectionDescription: { type: String },
proposedVersion: { type: OrganizationVersion },
publishedVersion: { type: OrganizationVersion },
createdAt: { type: Date },
updatedAt: { type: Date },
publishedAt: { type: Date },
})
export default mongoose.model('Organization', Organization)

11
src/models/Tag.ts Normal file
View file

@ -0,0 +1,11 @@
import mongoose, { Schema } from 'mongoose'
const _Tag = new Schema({
name: { type: String, required: true },
icon: { type: String, required: true },
description: { type: String }
})
export const Tag = _Tag
export default mongoose.model('Tag', _Tag)

12
tsconfig.json Normal file
View file

@ -0,0 +1,12 @@
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"outDir": "dist",
"target": "es6",
"strict": true
},
"include": [
"src/app.ts"
]
}

1488
yarn.lock Normal file

File diff suppressed because it is too large Load diff