Resolved errors bug and fixed the permanote controller - also working toward renaming all instances of PermaLink to permanote
This commit is contained in:
48
src/controllers/permaNoteController.js
Normal file
48
src/controllers/permaNoteController.js
Normal file
@@ -0,0 +1,48 @@
|
||||
import {Logger} from '../models/logger';
|
||||
import {ControllerHandler} from './controllerHandler';
|
||||
import {API} from './api/api';
|
||||
import {Database} from '../models/database/database'
|
||||
import {PermaLink} from '../models/permalinks/permalink';
|
||||
|
||||
export class PermaNoteController extends ControllerHandler {
|
||||
static async newPermaNote(req, res, next) {
|
||||
const errors = new API.errors(res);
|
||||
|
||||
const content = req.body.content || undefined;
|
||||
if (!content) {
|
||||
errors.addError(422, 'Unprocessable entity', 'There is no content');
|
||||
errors.endpoint();
|
||||
return;
|
||||
}
|
||||
|
||||
const uid = await PermaLink.genUID() || new Date().getTime();
|
||||
const endpoint = await PermaLink.genEndpoint();
|
||||
|
||||
const success = await Database.PermaNotes.newNote(uid, endpoint, content);
|
||||
if (success == -1) {
|
||||
errors.addError(500, 'Internal server error');
|
||||
errors.endpoint();
|
||||
return;
|
||||
}
|
||||
|
||||
new API.permalink(res, content, uid, endpoint).endpoint();
|
||||
next();
|
||||
}
|
||||
|
||||
static async getPermaNote(req, res, next) {
|
||||
const endpoint = req.params.endpoint || undefined;
|
||||
|
||||
if (!endpoint) {
|
||||
return;
|
||||
}
|
||||
|
||||
const data = await Database.PermaNotes.getNoteByEndpoint(endpoint);
|
||||
if (data == -1) {
|
||||
res.status(404).end('404 Not Found');
|
||||
return;
|
||||
}
|
||||
|
||||
res.end(data.text);
|
||||
next();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user