Saving and fetching of documents

This commit is contained in:
Jori Lallo
2016-05-19 20:46:34 -07:00
parent 58e588a6fd
commit 4430a3129e
14 changed files with 332 additions and 16 deletions

63
server/api/documents.js Normal file
View 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;

View File

@@ -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.