mirror of
https://github.com/ArthurDanjou/artdanj-api.git
synced 2026-01-14 12:14:33 +01:00
31 lines
1.1 KiB
TypeScript
31 lines
1.1 KiB
TypeScript
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
|
import Redis from '@ioc:Adonis/Addons/Redis'
|
|
import StateSleepingValidator from 'App/Validators/states/StateSleepingValidator'
|
|
import { getCurrentPlayingFromCache } from 'App/Utils/SongUtils'
|
|
|
|
export default class StatesController {
|
|
public async index({ response }: HttpContextContract) {
|
|
const sleeping = this.formatValue(await Redis.get('states:sleeping'))
|
|
const developing = this.formatValue(await Redis.get('states:developing'))
|
|
return response.status(200).send({
|
|
sleeping,
|
|
developing,
|
|
listening_music: await getCurrentPlayingFromCache(),
|
|
})
|
|
}
|
|
|
|
public async setSleeping({ request, response }: HttpContextContract) {
|
|
const { value } = await request.validate(StateSleepingValidator)
|
|
await Redis.set('states:sleeping', String(value))
|
|
await Redis.set('states:developing', String(!value))
|
|
return response.status(200).send({
|
|
message: 'State was successfully set!',
|
|
value: this.formatValue(String(value)),
|
|
})
|
|
}
|
|
|
|
public formatValue(value: string | null): string {
|
|
return value === 'true' ? 'Yes' : 'No'
|
|
}
|
|
}
|