Skip to content

Commit

Permalink
feat(services): make use of WebSocket controller to send data
Browse files Browse the repository at this point in the history
  • Loading branch information
Melchyore committed May 25, 2019
1 parent 433284d commit 87c0b24
Showing 1 changed file with 49 additions and 2 deletions.
51 changes: 49 additions & 2 deletions src/Services/EntryService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
* Copyright (c) 2019 Paradox.
*/

import { EntryStore, Entry } from '../Contracts'
import { EntryStore, Entry } from '../Contracts/Entry'
import AdoscopeEntryOptions from '../AdoscopeEntryOptions'

// @ts-ignore
const Entry = use('Adoscope/App/Models/AdoscopeEntry')
const Ws = use('Ws')

/**
* Class used to handle Adoscope's queries.
Expand Down Expand Up @@ -40,12 +42,32 @@ class EntryService {
*/
public static async store (data: EntryStore): Promise<Entry> {
try {
return await Entry.create(data)
const entry = await Entry.create(data)
const subscriber = Ws.getChannel('adoscope')

if (subscriber) {
const topic = subscriber.topic('adoscope')

if (topic) {
topic.broadcast(data.type, entry)
}
}

return entry
} catch (e) {
throw e
}
}

public static async get (type: string, options: AdoscopeEntryOptions): Promise<Array<Entry>> {
return await Entry
.query()
.options(type, options)
.limit(options.limit)
.orderBy('id', 'desc')
.fetch()
}

/**
* Update entry using condition.
*
Expand Down Expand Up @@ -116,6 +138,31 @@ class EntryService {
}
}

public static async getAll () : Promise<object> {
try {
return {
requests: await EntryService.findAllBy(['type', 'request']),
queries: await EntryService.findAllBy(['type', 'query']),
models: await EntryService.findAllBy(['type', 'model']),
schedules: await EntryService.findAllBy(['type', 'schedule']),
commands: await EntryService.findAllBy(['type', 'command'])
}
} catch (e) {
throw e
}
}

public static async findAllBy (condition: [string, any]): Promise<Array<Entry>> {
try {
return await Entry
.query()
.where(condition[0], condition[1])
.fetch()
} catch (e) {
throw e
}
}

}

module.exports = EntryService

0 comments on commit 87c0b24

Please sign in to comment.