commit
c61a447592
@ -0,0 +1,71 @@
|
||||
import {Controller, view, Inject, Injectable, SecurityContext, api, make, Logging} from '@extollo/lib'
|
||||
import {EditorPage as Page} from '../../../models/EditorPage.model'
|
||||
|
||||
/**
|
||||
* EditorPage Controller
|
||||
* ------------------------------------
|
||||
* Put some description here.
|
||||
*/
|
||||
@Injectable()
|
||||
export class EditorPage extends Controller {
|
||||
@Inject()
|
||||
protected readonly security!: SecurityContext
|
||||
|
||||
@Inject()
|
||||
protected readonly logging!: Logging
|
||||
|
||||
public async list() {
|
||||
const pages = await Page.query<Page>()
|
||||
.where('user_id', '=', this.security.user().getUniqueIdentifier())
|
||||
.get()
|
||||
.toArray()
|
||||
|
||||
return api.many(pages)
|
||||
}
|
||||
|
||||
public async load() {
|
||||
const pageId = this.request.safe('pageId').integer()
|
||||
const page = await Page.query<Page>()
|
||||
.where('user_id', '=', this.security.user().getUniqueIdentifier())
|
||||
.whereKey(pageId)
|
||||
.first()
|
||||
|
||||
if ( !page ) {
|
||||
return api.error('Page not found.')
|
||||
}
|
||||
|
||||
return api.one(page)
|
||||
}
|
||||
|
||||
public async save() {
|
||||
// Check if page_id is specified
|
||||
const serialData = this.request.safe('serialData').string()
|
||||
const pageId = String(this.request.input('pageId') ?? '')
|
||||
|
||||
// If so, look it up and update it
|
||||
if ( pageId ) {
|
||||
const page = await Page.query<Page>()
|
||||
.whereKey(parseInt(pageId, 10))
|
||||
.where('user_id', '=', this.security.user().getUniqueIdentifier())
|
||||
.first()
|
||||
|
||||
if ( !page ) {
|
||||
return api.error('Invalid pageId.')
|
||||
}
|
||||
|
||||
page.serialData = serialData
|
||||
await page.save()
|
||||
return api.one(page)
|
||||
}
|
||||
|
||||
// If not, create new page and save it
|
||||
this.logging.debug('userId:')
|
||||
this.logging.debug(this.security.user().getUniqueIdentifier())
|
||||
const page = make<Page>(Page)
|
||||
page.userId = String(this.security.user().getUniqueIdentifier())
|
||||
page.serialData = serialData
|
||||
|
||||
await page.save()
|
||||
return api.one(page)
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
import {Injectable, Migration, Inject, DatabaseService, FieldType} from '@extollo/lib'
|
||||
|
||||
/**
|
||||
* CreateEditorPagesTableMigration
|
||||
* ----------------------------------
|
||||
* Put some description here.
|
||||
*/
|
||||
@Injectable()
|
||||
export default class CreateEditorPagesTableMigration extends Migration {
|
||||
@Inject()
|
||||
protected readonly db!: DatabaseService
|
||||
|
||||
/**
|
||||
* Apply the migration.
|
||||
*/
|
||||
async up(): Promise<void> {
|
||||
const schema = this.db.get().schema()
|
||||
const table = await schema.table('editor_pages')
|
||||
|
||||
table.primaryKey('page_id').required()
|
||||
|
||||
table.column('user_id')
|
||||
.type(FieldType.varchar)
|
||||
.required()
|
||||
|
||||
table.column('serial_data')
|
||||
.type(FieldType.text)
|
||||
.required()
|
||||
|
||||
await schema.commit(table)
|
||||
}
|
||||
|
||||
/**
|
||||
* Undo the migration.
|
||||
*/
|
||||
async down(): Promise<void> {
|
||||
const schema = this.db.get().schema()
|
||||
const table = await schema.table('editor_pages')
|
||||
|
||||
table.dropIfExists()
|
||||
|
||||
await schema.commit(table)
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
import {Field, FieldType, Injectable, Model} from '@extollo/lib'
|
||||
|
||||
/**
|
||||
* EditorPage Model
|
||||
*/
|
||||
@Injectable()
|
||||
export class EditorPage extends Model<EditorPage> {
|
||||
protected static table = 'editor_pages'
|
||||
protected static key = 'page_id'
|
||||
|
||||
@Field(FieldType.serial, 'page_id')
|
||||
public pageId?: number
|
||||
|
||||
@Field(FieldType.varchar, 'user_id')
|
||||
public userId!: string
|
||||
|
||||
@Field(FieldType.text, 'serial_data')
|
||||
public serialData!: string
|
||||
}
|
Loading…
Reference in new issue