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
47 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,16 +1,61 @@ | ||
'use strict'; | ||
|
||
const ApiResult = require('../../../../helpers/apiResult'); | ||
const Post = require('../../../../documentModels/post'); | ||
const User = require('../../../../documentModels/user'); | ||
|
||
// TODO: 不完全な実装 | ||
|
||
exports.get = async (request) => { | ||
const result = await request.checkRequestAsync({ | ||
query: [], | ||
permissions: ['postRead', 'userRead'] | ||
}); | ||
|
||
if (result != null) { | ||
return result; | ||
} | ||
|
||
return new ApiResult(501, 'not implemented'); | ||
// user | ||
const user = await User.findByIdAsync(request.params.id, request.db, request.config); | ||
if (user == null) { | ||
return new ApiResult(404, 'user as premise not found'); | ||
} | ||
|
||
// limit | ||
let limit = request.query.limit; | ||
if (limit != null) { | ||
limit = parseInt(limit); | ||
if (isNaN(limit) || limit <= 0 || limit > 100) { | ||
return new ApiResult(400, 'limit is invalid'); | ||
} | ||
} | ||
else { | ||
limit = 30; | ||
} | ||
|
||
let posts; | ||
|
||
try { | ||
posts = await Post.findArrayAsync({ | ||
$and: [ | ||
{userId: user.document._id}, | ||
{type: 'status'} | ||
] | ||
}, false, limit, request.db, request.config); | ||
} | ||
catch(err) { | ||
console.dir(err); | ||
} | ||
|
||
if (posts == null || posts.length == 0) { | ||
return new ApiResult(204); | ||
} | ||
|
||
const serializedPosts = []; | ||
|
||
for (const post of posts) { | ||
serializedPosts.push(await post.serializeAsync(true)); | ||
} | ||
|
||
return new ApiResult(200, {posts: serializedPosts}); | ||
}; |