2019-05-21 20:45:27 +00:00
|
|
|
const sanitize = require('sanitize-filename')
|
|
|
|
const graphHelper = require('../../helpers/graph')
|
2019-05-13 05:15:27 +00:00
|
|
|
|
|
|
|
/* global WIKI */
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
Query: {
|
|
|
|
async assets() { return {} }
|
|
|
|
},
|
|
|
|
Mutation: {
|
|
|
|
async assets() { return {} }
|
|
|
|
},
|
|
|
|
AssetQuery: {
|
|
|
|
async list(obj, args, context) {
|
2019-05-18 06:27:30 +00:00
|
|
|
let cond = {
|
2019-05-21 20:45:27 +00:00
|
|
|
folderId: args.folderId === 0 ? null : args.folderId
|
2019-05-18 06:27:30 +00:00
|
|
|
}
|
|
|
|
if (args.kind !== 'ALL') {
|
|
|
|
cond.kind = args.kind.toLowerCase()
|
|
|
|
}
|
|
|
|
const result = await WIKI.models.assets.query().where(cond)
|
2019-05-13 05:15:27 +00:00
|
|
|
return result.map(a => ({
|
|
|
|
...a,
|
|
|
|
kind: a.kind.toUpperCase()
|
|
|
|
}))
|
2019-05-21 20:45:27 +00:00
|
|
|
},
|
|
|
|
async folders(obj, args, context) {
|
|
|
|
const result = await WIKI.models.assetFolders.query().where({
|
|
|
|
parentId: args.parentFolderId === 0 ? null : args.parentFolderId
|
|
|
|
})
|
|
|
|
// TODO: Filter by page rules
|
|
|
|
return result
|
2019-05-13 05:15:27 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
AssetMutation: {
|
2019-05-21 20:45:27 +00:00
|
|
|
async createFolder(obj, args, context) {
|
|
|
|
try {
|
|
|
|
const folderSlug = sanitize(args.slug).toLowerCase()
|
|
|
|
const parentFolderId = args.parentFolderId === 0 ? null : args.parentFolderId
|
|
|
|
const result = await WIKI.models.assetFolders.query().where({
|
|
|
|
parentId: parentFolderId,
|
|
|
|
slug: folderSlug
|
|
|
|
}).first()
|
|
|
|
if (!result) {
|
|
|
|
await WIKI.models.assetFolders.query().insert({
|
|
|
|
slug: folderSlug,
|
|
|
|
name: folderSlug,
|
|
|
|
parentId: parentFolderId
|
|
|
|
})
|
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('Asset Folder has been created successfully.')
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new WIKI.Error.AssetFolderExists()
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return graphHelper.generateError(err)
|
|
|
|
}
|
|
|
|
}
|
2019-05-13 05:15:27 +00:00
|
|
|
// deleteFile(obj, args) {
|
|
|
|
// return WIKI.models.File.destroy({
|
|
|
|
// where: {
|
|
|
|
// id: args.id
|
|
|
|
// },
|
|
|
|
// limit: 1
|
|
|
|
// })
|
|
|
|
// },
|
|
|
|
// renameFile(obj, args) {
|
|
|
|
// return WIKI.models.File.update({
|
|
|
|
// filename: args.filename
|
|
|
|
// }, {
|
|
|
|
// where: { id: args.id }
|
|
|
|
// })
|
|
|
|
// },
|
|
|
|
// moveFile(obj, args) {
|
|
|
|
// return WIKI.models.File.findById(args.fileId).then(fl => {
|
|
|
|
// if (!fl) {
|
|
|
|
// throw new gql.GraphQLError('Invalid File ID')
|
|
|
|
// }
|
|
|
|
// return WIKI.models.Folder.findById(args.folderId).then(fld => {
|
|
|
|
// if (!fld) {
|
|
|
|
// throw new gql.GraphQLError('Invalid Folder ID')
|
|
|
|
// }
|
|
|
|
// return fl.setFolder(fld)
|
|
|
|
// })
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
// File: {
|
|
|
|
// folder(fl) {
|
|
|
|
// return fl.getFolder()
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
}
|