import PaginationService from 'App/Services/PaginationService'
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import TextRecord from 'App/Models/TextRecord'
import { schema } from '@ioc:Adonis/Core/Validator'

export default class TextRecordController {
    private paginationService = new PaginationService(TextRecord)

    public async index({ request }: HttpContextContract) {
        return await this.paginationService.paginate(request.all())
    }

    public async store({ request, bouncer }: HttpContextContract) {
        await bouncer.authorize('admin')
        // 数据校验
        await request.validate({
            schema: schema.create({
                deviceId: schema.string(),
                appName: schema.string(),
                record: schema.string()
            })
        })
        // 数据提取
        // const payload = request.only(['deviceId', 'appName', 'record'])
        return await TextRecord.create(request.all())
    }
}
