Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
implement #23
Browse files Browse the repository at this point in the history
  • Loading branch information
marihachi committed May 7, 2017
1 parent 6e49868 commit 9d13cc3
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/routes/posts/post_article.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,45 @@
'use strict';

const ApiResult = require('../../helpers/apiResult');
const stringSize = require('../../helpers/stringSize');

exports.post = async (request) => {
const result = await request.checkRequestAsync({
params: [],
params: [
{name: 'title', type: 'string'},
{name: 'text', type: 'string'}
],
permissions: ['postWrite']
});

if (result != null)
return result;

return new ApiResult(501, 'not implemented');
const userId = request.user.document._id;
const title = request.body.title;
const text = request.body.text;

if (/^\s*$/.test(title) || stringSize(text) > 64)
return new ApiResult(400, 'title is invalid format. max 64bytes');

if (/^\s*$/.test(text) || stringSize(text) > 10000)
return new ApiResult(400, 'text is invalid format. max 10,000bytes');

let postArticle;
try {
postArticle = await request.db.posts.createAsync({ // TODO: move to document models
type: 'article',
userId: userId,
title: title,
text: text
});
}
catch(err) {
console.log(err.trace);
}

if (postArticle == null)
return new ApiResult(500, 'faild to create postArticle');

return new ApiResult(200, {postArticle: postArticle.serialize()});
};

0 comments on commit 9d13cc3

Please sign in to comment.