This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
32 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}); | ||
}; |