Skip to content
This repository has been archived by the owner on Oct 1, 2019. It is now read-only.

Commit

Permalink
[#790] Add CRUD controllers for user posts
Browse files Browse the repository at this point in the history
  • Loading branch information
voidxnull committed Feb 14, 2017
1 parent bb519cc commit 300d088
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
98 changes: 98 additions & 0 deletions src/api/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3342,8 +3342,106 @@ export default class ApiController {
ctx.body = messages;
}

getUserPost = async (ctx) => {
const UserPost = this.bookshelf.model('UserPost');

try {
const post = await new UserPost({ id: ctx.params.id, user_id: ctx.params.user_id })
.fetch({ require: true });

ctx.body = post;
} catch (e) {
ctx.status = 404;
}
}

getUserPosts = async (ctx) => {
const UserPost = this.bookshelf.model('UserPost');

const posts = await UserPost.collection()
.where('user_id', ctx.params.user_id)
.fetch();

ctx.body = posts;
}

createUserPost = async (ctx) => {
if (!ctx.session || !ctx.session.user) {
ctx.status = 403;
ctx.body = { error: 'You are not authorized' };
return;
}

const UserPost = this.bookshelf.model('UserPost');

const post = new UserPost(_.pick(ctx.request.body, ['text', 'type', 'more']));

try {
await post.save();
ctx.body = await post.fetch();
} catch (e) {
this.processError(ctx, e);
}
}

updateUserPost = async (ctx) => {
if (!ctx.session || !ctx.session.user) {
ctx.status = 403;
ctx.body = { error: 'You are not authorized' };
return;
}

const UserPost = this.bookshelf.model('UserPost');

const post = new UserPost({ id: ctx.params.id, user_id: ctx.session.user });

try {
await post.save(_.pick(ctx.request.body, ['text', 'type', 'more']), { patch: true });
ctx.body = await post.fetch();
} catch (e) {
this.processError(ctx, e);
}
}

deleteUserPost = async (ctx) => {
const UserPost = this.bookshelf.model('UserPost');

try {
await new UserPost({ id: ctx.params.id, user_id: ctx.params.user_id })
.destroy({ require: true });
} catch (e) {
ctx.status = 404;
}
}

// ========== Helpers ==========

/**
* Sets the response body in case of error
* ```
* {
* errors: { fieldName: ['Some message']] }, // Only if e is a Checkit.Error
* error: 'Human readable message'
* }
* ```
* @param ctx Koa context
* @param {Error} e
*/
processError(ctx, e) {
ctx.status = 500;

if (e instanceof Checkit.Error) {
ctx.body = {
...e.toJSON(),
error: e.toString()
};
} else {
ctx.body = {
error: e.message
};
}
}

async areMutuallyFollowed(user1Id, user2Id) {
const knex = this.bookshelf.knex;

Expand Down
6 changes: 6 additions & 0 deletions src/api/routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ export function initApi(bookshelf, sphinx) {
api.post('/user/:username/follow', controller.followUser);
api.post('/user/:username/unfollow', controller.unfollowUser);
api.post('/user/:username/ignore', controller.ignoreUser);
api.get('/user/:user_id/user-posts', controller.getUserPosts);
api.get('/user/:user_id/user-posts/:id', controller.getUserPost);
api.post('/user/:user_id/user-posts', controller.createUserPost);
api.post('/user/:user_id/user-posts/:id', controller.updateUserPost);
api.delete('/user/:user_id/user-posts/:id', controller.deleteUserPost);


api.get('/user/verify/:hash', controller.verifyEmail);
api.post('/user/', controller.updateUser);
Expand Down

0 comments on commit 300d088

Please sign in to comment.