mirror of
https://github.com/ArthurDanjou/artdanj-api.git
synced 2026-01-14 12:14:33 +01:00
Add States
This commit is contained in:
42
app/Controllers/Http/StatesController.ts
Normal file
42
app/Controllers/Http/StatesController.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import {HttpContextContract} from "@ioc:Adonis/Core/HttpContext";
|
||||
import Redis from "@ioc:Adonis/Addons/Redis";
|
||||
import StateSleepingValidator from "App/Validators/states/StateSleepingValidator";
|
||||
import StateDevelopingValidator from "App/Validators/states/StateDevelopingValidator";
|
||||
|
||||
export default class StatesController {
|
||||
|
||||
// Listening Music
|
||||
|
||||
public async index ({ response }: HttpContextContract) {
|
||||
const sleeping = this.formatValue(Boolean(await Redis.get('states:sleeping')) || false)
|
||||
const developing = this.formatValue(Boolean(await Redis.get('states:developing')) || false)
|
||||
return response.status(200).send({
|
||||
sleeping,
|
||||
developing,
|
||||
listening_music: "Soon"
|
||||
})
|
||||
}
|
||||
|
||||
public async setSleeping ({ request, response }: HttpContextContract) {
|
||||
const { value } = await request.validate(StateSleepingValidator)
|
||||
await Redis.set('states:sleeping', String(value))
|
||||
return response.status(200).send({
|
||||
message: 'State was successfully set!',
|
||||
value: this.formatValue(value)
|
||||
})
|
||||
}
|
||||
|
||||
public async setDeveloping ({ request, response }: HttpContextContract) {
|
||||
const { value } = await request.validate(StateDevelopingValidator)
|
||||
await Redis.set('states:developing', String(value))
|
||||
return response.status(200).send({
|
||||
message: 'State was successfully set!',
|
||||
value: this.formatValue(value)
|
||||
})
|
||||
}
|
||||
|
||||
public formatValue (value: boolean): string {
|
||||
return value ? 'Yes' : 'No'
|
||||
}
|
||||
|
||||
}
|
||||
15
app/Validators/states/StateDevelopingValidator.ts
Normal file
15
app/Validators/states/StateDevelopingValidator.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { schema } from '@ioc:Adonis/Core/Validator'
|
||||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||
|
||||
export default class StateDevelopingValidator {
|
||||
constructor (protected ctx: HttpContextContract) {
|
||||
}
|
||||
|
||||
public schema = schema.create({
|
||||
value: schema.boolean()
|
||||
})
|
||||
|
||||
public messages = {
|
||||
required: 'The field {{field}} is required',
|
||||
}
|
||||
}
|
||||
15
app/Validators/states/StateSleepingValidator.ts
Normal file
15
app/Validators/states/StateSleepingValidator.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { schema } from '@ioc:Adonis/Core/Validator'
|
||||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||
|
||||
export default class StateSleepingValidator {
|
||||
constructor (protected ctx: HttpContextContract) {
|
||||
}
|
||||
|
||||
public schema = schema.create({
|
||||
value: schema.boolean()
|
||||
})
|
||||
|
||||
public messages = {
|
||||
required: 'The field {{field}} is required',
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user