feat: save page

This commit is contained in:
NGPixel
2018-07-22 00:29:39 -04:00
parent c7b675bb1c
commit cb84df7a53
13 changed files with 192 additions and 66 deletions

View File

@@ -150,16 +150,16 @@ exports.up = knex => {
table.integer('authorId').unsigned().references('id').inTable('users')
})
.table('pages', table => {
table.string('editor').references('key').inTable('editors')
table.string('locale', 2).references('code').inTable('locales')
table.string('editorKey').references('key').inTable('editors')
table.string('localeCode', 2).references('code').inTable('locales')
table.integer('authorId').unsigned().references('id').inTable('users')
})
.table('users', table => {
table.string('provider').references('key').inTable('authentication').notNullable().defaultTo('local')
table.string('locale', 2).references('code').inTable('locales').notNullable().defaultTo('en')
table.string('providerKey').references('key').inTable('authentication').notNullable().defaultTo('local')
table.string('localeCode', 2).references('code').inTable('locales').notNullable().defaultTo('en')
table.string('defaultEditor').references('key').inTable('editors').notNullable().defaultTo('markdown')
table.unique(['provider', 'email'])
table.unique(['providerKey', 'email'])
})
}

View File

@@ -49,11 +49,19 @@ module.exports = class Page extends Model {
to: 'users.id'
}
},
editor: {
relation: Model.BelongsToOneRelation,
modelClass: require('./editors'),
join: {
from: 'pages.editorKey',
to: 'editors.key'
}
},
locale: {
relation: Model.BelongsToOneRelation,
modelClass: require('./locales'),
join: {
from: 'users.locale',
from: 'pages.localeCode',
to: 'locales.code'
}
}

View File

@@ -23,13 +23,11 @@ module.exports = class User extends Model {
id: {type: 'integer'},
email: {type: 'string', format: 'email'},
name: {type: 'string', minLength: 1, maxLength: 255},
provider: {type: 'string', minLength: 1, maxLength: 255},
providerId: {type: 'number'},
password: {type: 'string'},
role: {type: 'string', enum: ['admin', 'guest', 'user']},
tfaIsActive: {type: 'boolean', default: false},
tfaSecret: {type: 'string'},
locale: {type: 'string'},
jobTitle: {type: 'string'},
location: {type: 'string'},
pictureUrl: {type: 'string'},
@@ -52,6 +50,30 @@ module.exports = class User extends Model {
},
to: 'groups.id'
}
},
provider: {
relation: Model.BelongsToOneRelation,
modelClass: require('./authentication'),
join: {
from: 'users.providerKey',
to: 'authentication.key'
}
},
defaultEditor: {
relation: Model.BelongsToOneRelation,
modelClass: require('./editors'),
join: {
from: 'users.editorKey',
to: 'editors.key'
}
},
locale: {
relation: Model.BelongsToOneRelation,
modelClass: require('./locales'),
join: {
from: 'users.localeCode',
to: 'locales.code'
}
}
}
}

View File

@@ -11,35 +11,44 @@ module.exports = {
},
PageQuery: {
async list(obj, args, context, info) {
return WIKI.db.groups.query().select(
'groups.*',
WIKI.db.groups.relatedQuery('users').count().as('userCount')
return WIKI.db.pages.query().select(
'pages.*',
WIKI.db.pages.relatedQuery('users').count().as('userCount')
)
},
async single(obj, args, context, info) {
return WIKI.db.groups.query().findById(args.id)
return WIKI.db.pages.query().findById(args.id)
}
},
PageMutation: {
async create(obj, args) {
const group = await WIKI.db.pages.query().insertAndFetch({
name: args.name
async create(obj, args, context) {
const page = await WIKI.db.pages.query().insertAndFetch({
path: args.path,
title: args.title,
description: args.description,
isPrivate: false,
isPublished: args.isPublished,
publishStartDate: args.publishStartDate,
publishEndDate: args.publishEndDate,
localeCode: args.locale,
editorKey: args.editor,
authorId: context.req.user.id
})
return {
responseResult: graphHelper.generateSuccess('Group created successfully.'),
group
responseResult: graphHelper.generateSuccess('Page created successfully.'),
page
}
},
async delete(obj, args) {
await WIKI.db.groups.query().deleteById(args.id)
return {
responseResult: graphHelper.generateSuccess('Group has been deleted.')
responseResult: graphHelper.generateSuccess('Page has been deleted.')
}
},
async update(obj, args) {
await WIKI.db.groups.query().patch({ name: args.name }).where('id', args.id)
return {
responseResult: graphHelper.generateSuccess('Group has been updated.')
responseResult: graphHelper.generateSuccess('Page has been updated.')
}
}
},

View File

@@ -21,7 +21,10 @@ type PageQuery {
): [PageMinimal]
single(
id: Int!
id: Int
path: String
locale: String
isPrivate: Boolean
): Page
}
@@ -32,8 +35,10 @@ type PageQuery {
type PageMutation {
create(
description: String
isPublished: Boolean
locale: String
editor: String
isPublished: Boolean!
isPrivate: Boolean
locale: String!
path: String!
publishEndDate: Date
publishStartDate: Date
@@ -43,7 +48,15 @@ type PageMutation {
update(
id: Int!
name: String!
description: String
editor: String
isPublished: Boolean
locale: String
path: String
publishEndDate: Date
publishStartDate: Date
tags: [String]
title: String
): DefaultResponse
delete(

View File

@@ -15,7 +15,7 @@ module.exports = {
}, (uEmail, uPassword, done) => {
WIKI.db.users.query().findOne({
email: uEmail,
provider: 'local'
providerKey: 'local'
}).then((user) => {
if (user) {
return user.verifyPassword(uPassword).then(() => {

View File

@@ -325,7 +325,7 @@ module.exports = () => {
// Create root administrator
WIKI.logger.info('Creating root administrator...')
await WIKI.db.users.query().delete().where({
provider: 'local',
providerKey: 'local',
email: req.body.adminEmail
})
await WIKI.db.users.query().insert({
@@ -342,7 +342,7 @@ module.exports = () => {
// Create Guest account
WIKI.logger.info('Creating guest account...')
const guestUsr = await WIKI.db.users.query().findOne({
provider: 'local',
providerKey: 'local',
email: 'guest@example.com'
})
if (!guestUsr) {