xiongzhu 10 månader sedan
förälder
incheckning
d594145e10

+ 32 - 0
app/Controllers/Http/OcrDevicesController.ts

@@ -0,0 +1,32 @@
+import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
+import PaginationService from 'App/Services/PaginationService'
+import { schema } from '@ioc:Adonis/Core/Validator'
+import OcrDevice from 'App/Models/OcrDevice'
+export default class OcrDevicesController {
+    private paginationService = new PaginationService(OcrDevice)
+
+    public async index({ request }: HttpContextContract) {
+        return await this.paginationService.paginate(request.all())
+    }
+
+    public async store({ request, bouncer }: HttpContextContract) {
+        // await bouncer.authorize('admin')
+        const data = await request.validate({
+            schema: schema.create({
+                id: schema.string(),
+                platform: schema.string(),
+                channel: schema.string(),
+                deviceInfo: schema.string.optional(),
+                total: schema.number(),
+                scanned: schema.number()
+            })
+        })
+        const device = await OcrDevice.findBy('id', data.id)
+        if (device) {
+            device.merge(data)
+            return await device.save()
+        } else {
+            return await OcrDevice.create(data)
+        }
+    }
+}

+ 29 - 0
app/Models/OcrDevice.ts

@@ -0,0 +1,29 @@
+import AppBaseModel from 'App/Models/AppBaseModel'
+import { column } from '@ioc:Adonis/Lucid/Orm'
+import { DateTime } from 'luxon'
+
+export default class OcrDevice extends AppBaseModel {
+    @column({ isPrimary: true })
+    public id: string
+
+    @column.dateTime({ autoCreate: true })
+    public createdAt: DateTime
+
+    @column.dateTime({ autoCreate: true, autoUpdate: true })
+    public updatedAt: DateTime
+
+    @column()
+    public platform: string
+
+    @column()
+    public deviceInfo: string
+
+    @column()
+    public channel: string
+
+    @column()
+    public total: number
+
+    @column()
+    public scanned: number
+}

+ 22 - 0
database/migrations/1741251032761_ocr_devices.ts

@@ -0,0 +1,22 @@
+import BaseSchema from '@ioc:Adonis/Lucid/Schema'
+
+export default class extends BaseSchema {
+    protected tableName = 'ocr_devices'
+
+    public async up() {
+        this.schema.createTable(this.tableName, (table) => {
+            table.string('id', 70).primary()
+            table.timestamp('created_at', { useTz: true })
+            table.timestamp('updated_at', { useTz: true })
+            table.string('platform').notNullable()
+            table.string('device_info').nullable()
+            table.string('channel').notNullable()
+            table.integer('total').defaultTo(0)
+            table.integer('scanned').defaultTo(0)
+        })
+    }
+
+    public async down() {
+        this.schema.dropTable(this.tableName)
+    }
+}

+ 5 - 0
start/routes.ts

@@ -121,9 +121,14 @@ Route.group(() => {
         Route.get('ocrChannel', 'OcrChannelController.index')
         Route.get('ocrChannel', 'OcrChannelController.index')
         Route.post('ocrChannel', 'OcrChannelController.store')
         Route.post('ocrChannel', 'OcrChannelController.store')
     }).middleware('auth:api')
     }).middleware('auth:api')
+
     Route.group(() => {
     Route.group(() => {
         Route.get('/plusDevice/:id', 'OcrChannelController.plusDeviceNum')
         Route.get('/plusDevice/:id', 'OcrChannelController.plusDeviceNum')
         Route.get('/plusRecord/:id', 'OcrChannelController.plusRecordNum')
         Route.get('/plusRecord/:id', 'OcrChannelController.plusRecordNum')
         Route.get('/plusScan/:id/:scanCount', 'OcrChannelController.plusScanNum')
         Route.get('/plusScan/:id/:scanCount', 'OcrChannelController.plusScanNum')
     }).prefix('/ocrChannel')
     }).prefix('/ocrChannel')
+
+    Route.group(() => {
+        Route.post('/', 'OcrDevicesController.store')
+    }).prefix('/ocrDevice')
 }).prefix('/api')
 }).prefix('/api')