Add endpoints for working with apps

This commit is contained in:
William Brawner 2020-09-01 15:33:10 +00:00
parent 727865bc00
commit 6fc06365c6
4 changed files with 147 additions and 15 deletions

7
app.js
View file

@ -2,6 +2,9 @@ import { randomId } from './util.js';
export default class App {
id = randomId(32);
name = '';
users = [];
name;
constructor(name) {
this.name = name;
}
}

82
db.js
View file

@ -11,6 +11,13 @@ const pool = mysql.createPool({
database: process.env.DB_NAME || 'flayre'
});
pool.query(`CREATE TABLE IF NOT EXISTS apps (
id VARCHAR(32) PRIMARY KEY,
name VARCHAR(256) UNIQUE NOT NULL
)`, (err, res) => {
if (err) console.error(err);
});
pool.query(`CREATE TABLE IF NOT EXISTS events (
id VARCHAR(32) PRIMARY KEY,
appId VARCHAR(32) NOT NULL,
@ -22,13 +29,80 @@ pool.query(`CREATE TABLE IF NOT EXISTS events (
version VARCHAR(32),
locale VARCHAR(8),
sessionId VARCHAR(32),
data JSON DEFAULT NULL,
element VARCHAR(256) DEFAULT NULL,
data TEXT DEFAULT NULL,
type VARCHAR(256) DEFAULT NULL,
stacktrace VARCHAR(2048) DEFAULT NULL,
fatal BOOLEAN DEFAULT NULL
FOREIGN KEY (appId)
REFERENCES apps(id)
ON DELETE CASCADE
)`);
export class AppRepository {
static getApps() {
return new Promise((resolve, reject) => {
pool.query('SELECT * FROM apps', (err, res) => {
if (err) {
reject(err);
return;
}
resolve(res);
});
})
}
static getApp(appId) {
return new Promise((resolve, reject) => {
pool.query('SELECT * FROM apps WHERE id = ? LIMIT 1', appId, (err, res) => {
if (err) {
reject(err);
return;
}
resolve(res[0]);
});
})
}
static createApp(app) {
return new Promise((resolve, reject) => {
pool.query('INSERT INTO apps SET ?', app, (err, res, fields) => {
if (err) {
reject(err);
return;
}
resolve(app);
});
})
}
static updateApp(appId, name) {
return new Promise((resolve, reject) => {
pool.query('UPDATE apps SET name = ? WHERE id = ?', [name, appId], (err, res, fields) => {
if (err) {
reject(err);
return;
}
resolve(res.affectedRows === 1);
});
})
}
static deleteApp(appId) {
return new Promise((resolve, reject) => {
pool.query('DELETE FROM apps WHERE id = ?', appId, (err, res) => {
if (err) {
reject(err);
return;
}
resolve(res);
});
})
}
}
export class EventRepository {
static getEvents(
appId,

View file

@ -1,7 +1,8 @@
import express from 'express';
import Event from './event.js';
import { randomId, firstOfMonth, lastOfMonth } from './util.js';
import { EventRepository } from './db.js';
import { AppRepository, EventRepository } from './db.js';
import App from './app.js';
const app = express();
app.use(express.json());
@ -17,6 +18,68 @@ app.get('/id', (req, res) => {
res.send(randomId(length));
});
app.get('/apps', (req, res) => {
AppRepository.getApps()
.then((apps) => {
res.json(apps);
}).catch((err) => {
console.error(err);
res.status(500).send();
})
});
app.post('/apps', (req, res) => {
const name = req.body.name;
if (!name) {
res.status(400).send({ message: 'Invalid app name' });
return;
}
AppRepository.createApp(new App(name))
.then((app) => {
res.json(app);
}).catch((err) => {
res.status(500).send();
})
});
app.get('/apps/:appId', (req, res) => {
AppRepository.getApp(req.params.appId)
.then((app) => {
if (!app) {
res.sendStatus(404);
} else {
res.json(app);
}
}).catch((err) => {
console.error(err);
res.status(500).send();
})
})
app.patch('/apps/:appId', (req, res) => {
AppRepository.updateApp(req.params.appId, req.body.name)
.then((app) => {
if (!app) {
res.sendStatus(404);
} else {
res.sendStatus(204);
}
}).catch((err) => {
console.error(err);
res.status(500).send();
})
})
app.delete('/apps/:appId', (req, res) => {
AppRepository.deleteApp(req.params.appId)
.then((app) => {
res.send(204);
}).catch((err) => {
res.status(500).send();
})
})
app.get('/events', (req, res) => {
const appId = req.query.appId;
if (!appId) {

View file

@ -1,8 +0,0 @@
import { randomId } from './util.js';
export default class User {
id = randomId(8);
name = '';
email = '';
password = '';
}