-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #868 from Hylozoic/861-post-collections
Add support for post Collections
- Loading branch information
Showing
13 changed files
with
482 additions
and
18 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
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
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { isEmpty, mapKeys, pick, snakeCase } from 'lodash' | ||
const { GraphQLYogaError } = require('@graphql-yoga/node') | ||
|
||
export async function addPostToCollection(userId, collectionId, postId) { | ||
const collection = await Collection.findValidCollectionForUser(userId, collectionId) | ||
|
||
// TODO: Validate that the post can be added to the collection | ||
const post = await Post.find(postId) | ||
|
||
if (!post) { | ||
throw new GraphQLYogaError('Not a valid post') | ||
} | ||
|
||
const order = await CollectionsPost.query(q => { | ||
q.select(bookshelf.knex.raw('max("order") as max_order')) | ||
q.where({ collection_id: collectionId }) | ||
}) | ||
.fetch() | ||
.then(result => result.get('max_order')) | ||
|
||
await new CollectionsPost({ user_id: userId, collection_id: collectionId, post_id: post.id, order: order ? order + 1 : 0 }).save() | ||
|
||
return { success: true } | ||
} | ||
|
||
export function createCollection(userId, data) { | ||
const whitelist = mapKeys(pick(data, ['name', 'groupId']), (v, k) => snakeCase(k)) | ||
if (isEmpty(whitelist)) return Promise.resolve(null) | ||
return Collection.create({ userId, ...data }) | ||
} | ||
|
||
export async function removePostFromCollection(userId, collectionId, postId) { | ||
const collection = await Collection.findValidCollectionForUser(userId, collectionId) | ||
|
||
const linkedPost = await CollectionsPost.query(q => q.where({ collection_id: collectionId, post_id: postId })).fetch() | ||
|
||
if (!linkedPost) { | ||
throw new GraphQLYogaError('Not a valid post') | ||
} | ||
|
||
await linkedPost.destroy() | ||
|
||
return { success: true } | ||
} | ||
|
||
export async function reorderPostInCollection(userId, collectionId, postId, newOrderIndex) { | ||
const collection = await Collection.findValidCollectionForUser(userId, collectionId) | ||
const linkedPost = await CollectionsPost.query(q => q.where({ collection_id: collectionId, post_id: postId })).fetch() | ||
|
||
if (!linkedPost) { | ||
throw new GraphQLYogaError('Not a valid post') | ||
} | ||
|
||
const oldOrder = linkedPost.get('order') | ||
|
||
if (oldOrder > newOrderIndex) { | ||
await CollectionsPost.query() | ||
.where({ collection_id: collectionId }) | ||
.andWhere('order', '>=', newOrderIndex) | ||
.andWhere('order', '<', oldOrder) | ||
.update({ order: bookshelf.knex.raw('?? + 1', ['order']) }) | ||
} else if (oldOrder < newOrderIndex) { | ||
await CollectionsPost.query() | ||
.where({ collection_id: collectionId }) | ||
.andWhere('order', '<=', newOrderIndex) | ||
.andWhere('order', '>', oldOrder) | ||
.update({ order: bookshelf.knex.raw('?? - 1', ['order']) }) | ||
} | ||
|
||
await linkedPost.save({ order: newOrderIndex }) | ||
|
||
return { success: true } | ||
} |
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
Oops, something went wrong.