diff --git a/src/api/controller.js b/src/api/controller.js index 04c09d5b..ca41cbf2 100644 --- a/src/api/controller.js +++ b/src/api/controller.js @@ -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; diff --git a/src/api/routing.js b/src/api/routing.js index e649fe45..59a45a85 100644 --- a/src/api/routing.js +++ b/src/api/routing.js @@ -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);