Added more views and atlas APIs
This commit is contained in:
52
server/api/atlases.js
Normal file
52
server/api/atlases.js
Normal file
@@ -0,0 +1,52 @@
|
||||
import Router from 'koa-router';
|
||||
import httpErrors from 'http-errors';
|
||||
|
||||
import auth from './authentication';
|
||||
import pagination from './middlewares/pagination';
|
||||
import { presentAtlas } from '../presenters';
|
||||
import { Team, Atlas } from '../models';
|
||||
|
||||
const router = new Router();
|
||||
|
||||
router.post('atlases.info', auth(), async (ctx) => {
|
||||
let { id } = ctx.request.body;
|
||||
ctx.assertPresent(id, 'id is required');
|
||||
|
||||
const team = await ctx.state.user.getTeam();
|
||||
const atlas = await Atlas.findOne({
|
||||
where: {
|
||||
id: id,
|
||||
teamId: team.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!atlas) throw httpErrors.NotFound();
|
||||
|
||||
ctx.body = {
|
||||
data: presentAtlas(atlas),
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
router.post('atlases.list', auth(), pagination(), async (ctx) => {
|
||||
let { teamId } = ctx.request.body;
|
||||
ctx.assertPresent(teamId, 'teamId is required');
|
||||
|
||||
const team = await ctx.state.user.getTeam();
|
||||
const atlases = await Atlas.findAll({
|
||||
where: {
|
||||
teamId: teamId,
|
||||
},
|
||||
offset: ctx.state.pagination.offset,
|
||||
limit: ctx.state.pagination.limit,
|
||||
});
|
||||
|
||||
ctx.body = {
|
||||
pagination: ctx.state.pagination,
|
||||
data: atlases.map((atlas) => {
|
||||
return presentAtlas(atlas);
|
||||
})
|
||||
};
|
||||
});
|
||||
|
||||
export default router;
|
||||
@@ -6,6 +6,7 @@ import Sequelize from 'sequelize';
|
||||
|
||||
import auth from './auth';
|
||||
import user from './user';
|
||||
import atlases from './atlases';
|
||||
|
||||
import validation from './validation';
|
||||
|
||||
@@ -42,6 +43,7 @@ api.use(validation());
|
||||
|
||||
router.use('/', auth.routes());
|
||||
router.use('/', user.routes());
|
||||
router.use('/', atlases.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.
|
||||
|
||||
@@ -4,9 +4,19 @@ import {
|
||||
} from '../sequelize';
|
||||
import Team from './Team';
|
||||
|
||||
const allowedAtlasTypes = [['atlas', 'journal']];
|
||||
|
||||
const Atlas = sequelize.define('atlas', {
|
||||
id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true },
|
||||
name: DataTypes.STRING,
|
||||
description: DataTypes.STRING,
|
||||
type: { type: DataTypes.STRING, validate: { isIn: allowedAtlasTypes }},
|
||||
}, {
|
||||
instanceMethods: {
|
||||
getRecentDocuments() {
|
||||
return [];
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
Atlas.belongsTo(Team);
|
||||
|
||||
@@ -23,6 +23,9 @@ const User = sequelize.define('user', {
|
||||
getJwtToken() {
|
||||
return JWT.sign({ id: this.id }, this.jwtSecret);
|
||||
},
|
||||
async getTeam() {
|
||||
return this.team;
|
||||
}
|
||||
},
|
||||
indexes: [
|
||||
{
|
||||
|
||||
@@ -14,3 +14,13 @@ export function presentTeam(team) {
|
||||
name: team.name,
|
||||
};
|
||||
}
|
||||
|
||||
export function presentAtlas(atlas) {
|
||||
return {
|
||||
id: atlas.id,
|
||||
name: atlas.name,
|
||||
description: atlas.description,
|
||||
type: atlas.type,
|
||||
recentDocuments: atlas.getRecentDocuments(),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user