Add endpoints to save, load, and list user pages
This commit is contained in:
parent
8cd1409eb6
commit
b17d8a18a5
71
src/app/http/controllers/api/EditorPage.controller.ts
Normal file
71
src/app/http/controllers/api/EditorPage.controller.ts
Normal file
@ -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)
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
import {Route, AuthRequiredMiddleware, GuestRequiredMiddleware, SessionAuthMiddleware} from '@extollo/lib'
|
||||
import {Home} from '../controllers/main/Home.controller'
|
||||
import {Login} from '../controllers/api/Login.controller'
|
||||
import {EditorPage} from '../controllers/api/EditorPage.controller'
|
||||
|
||||
Route.group('/', () => {
|
||||
Route.get('/')
|
||||
@ -12,6 +13,17 @@ Route.group('/', () => {
|
||||
success: true,
|
||||
}))
|
||||
|
||||
Route.group('/editor', () => {
|
||||
Route.post('/page')
|
||||
.calls<EditorPage>(EditorPage, page => page.save)
|
||||
|
||||
Route.get('/page')
|
||||
.calls<EditorPage>(EditorPage, page => page.load)
|
||||
|
||||
Route.get('/pages')
|
||||
.calls<EditorPage>(EditorPage, page => page.list)
|
||||
}).pre(AuthRequiredMiddleware)
|
||||
|
||||
Route.group('/login', () => {
|
||||
Route.post('/')
|
||||
.pre(GuestRequiredMiddleware)
|
||||
@ -23,7 +35,7 @@ Route.group('/', () => {
|
||||
Route.get('/user')
|
||||
.pre(AuthRequiredMiddleware)
|
||||
.calls<Login>(Login, login => login.user)
|
||||
|
||||
|
||||
})
|
||||
|
||||
Route.post('/register')
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
19
src/app/models/EditorPage.model.ts
Normal file
19
src/app/models/EditorPage.model.ts
Normal file
@ -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
Block a user