Saving and fetching of documents
This commit is contained in:
63
server/api/documents.js
Normal file
63
server/api/documents.js
Normal file
@@ -0,0 +1,63 @@
|
||||
import Router from 'koa-router';
|
||||
import httpErrors from 'http-errors';
|
||||
|
||||
import auth from './authentication';
|
||||
import pagination from './middlewares/pagination';
|
||||
import { presentDocument } from '../presenters';
|
||||
import { Document, Atlas } from '../models';
|
||||
|
||||
const router = new Router();
|
||||
|
||||
router.post('documents.info', auth(), async (ctx) => {
|
||||
let { id } = ctx.request.body;
|
||||
ctx.assertPresent(id, 'id is required');
|
||||
|
||||
const team = await ctx.state.user.getTeam();
|
||||
const document = await Document.findOne({
|
||||
where: {
|
||||
id: id,
|
||||
teamId: team.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!document) throw httpErrors.NotFound();
|
||||
|
||||
ctx.body = {
|
||||
data: await presentDocument(document, true),
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
router.post('documents.create', auth(), async (ctx) => {
|
||||
let {
|
||||
atlas,
|
||||
title,
|
||||
text,
|
||||
} = ctx.request.body;
|
||||
ctx.assertPresent(atlas, 'atlas is required');
|
||||
ctx.assertPresent(title, 'title is required');
|
||||
ctx.assertPresent(text, 'text is required');
|
||||
|
||||
const team = await ctx.state.user.getTeam();
|
||||
const ownerAtlas = await Atlas.findOne({
|
||||
where: {
|
||||
id: atlas,
|
||||
teamId: team.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!ownerAtlas) throw httpErrors.BadRequest();
|
||||
|
||||
const document = await Document.create({
|
||||
atlasId: ownerAtlas.id,
|
||||
teamId: team.id,
|
||||
title: title,
|
||||
text: text,
|
||||
});
|
||||
|
||||
ctx.body = {
|
||||
data: await presentDocument(document, true),
|
||||
};
|
||||
});
|
||||
|
||||
export default router;
|
||||
@@ -7,6 +7,7 @@ import Sequelize from 'sequelize';
|
||||
import auth from './auth';
|
||||
import user from './user';
|
||||
import atlases from './atlases';
|
||||
import documents from './documents';
|
||||
|
||||
import validation from './validation';
|
||||
|
||||
@@ -44,6 +45,7 @@ api.use(validation());
|
||||
router.use('/', auth.routes());
|
||||
router.use('/', user.routes());
|
||||
router.use('/', atlases.routes());
|
||||
router.use('/', documents.routes());
|
||||
|
||||
// Router is embedded in a Koa application wrapper, because koa-router does not
|
||||
// allow middleware to catch any routes which were not explicitly defined.
|
||||
|
||||
@@ -7,11 +7,11 @@ import Team from './Team';
|
||||
|
||||
const Document = sequelize.define('document', {
|
||||
id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true },
|
||||
name: DataTypes.STRING,
|
||||
content: DataTypes.STRING,
|
||||
title: DataTypes.STRING,
|
||||
text: DataTypes.TEXT,
|
||||
});
|
||||
|
||||
Document.belongsTo(Atlas);
|
||||
Document.belongsTo(Atlas, { as: 'atlas' });
|
||||
Document.belongsTo(Team);
|
||||
|
||||
export default Atlas;
|
||||
export default Document;
|
||||
|
||||
@@ -3,4 +3,9 @@ import Team from './Team';
|
||||
import Atlas from './Atlas';
|
||||
import Document from './Document';
|
||||
|
||||
export { User, Team, Atlas, Document };
|
||||
export {
|
||||
User,
|
||||
Team,
|
||||
Atlas,
|
||||
Document,
|
||||
};
|
||||
@@ -1,3 +1,5 @@
|
||||
var marked = require('marked');
|
||||
|
||||
export function presentUser(user) {
|
||||
return {
|
||||
id: user.id,
|
||||
@@ -15,7 +17,7 @@ export function presentTeam(team) {
|
||||
};
|
||||
}
|
||||
|
||||
export function presentAtlas(atlas) {
|
||||
export function presentAtlas(atlas, includeRecentDocuments=true) {
|
||||
return {
|
||||
id: atlas.id,
|
||||
name: atlas.name,
|
||||
@@ -23,4 +25,24 @@ export function presentAtlas(atlas) {
|
||||
type: atlas.type,
|
||||
recentDocuments: atlas.getRecentDocuments(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function presentDocument(document, includeAtlas=false) {
|
||||
const data = {
|
||||
id: document.id,
|
||||
title: document.title,
|
||||
text: document.text,
|
||||
html: marked(document.text),
|
||||
createdAt: document.createdAt,
|
||||
updatedAt: document.updatedAt,
|
||||
atlas: document.atlaId,
|
||||
team: document.teamId,
|
||||
}
|
||||
|
||||
if (includeAtlas) {
|
||||
const atlas = await document.getAtlas();
|
||||
data.atlas = presentAtlas(atlas, false);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user