207 lines
5.8 KiB
TypeScript
207 lines
5.8 KiB
TypeScript
import Scenario from '#models/scenario'
|
|
import type { HttpContext } from '@adonisjs/core/http'
|
|
import db from '@adonisjs/lucid/services/db'
|
|
|
|
export default class ScenariosController {
|
|
/**
|
|
* List all scenarios
|
|
*/
|
|
async get({ request, response, auth }: HttpContext) {
|
|
try {
|
|
const search = request.input('search', '')
|
|
const perPage = request.input('per_page', 10)
|
|
const page = request.input('page', 1)
|
|
|
|
const query = Scenario.query().preload('brand').preload('category')
|
|
|
|
const scenarios = await query.orderBy('scenarios.created_at', 'asc').paginate(page, perPage)
|
|
return response.ok({
|
|
status: true,
|
|
data: scenarios,
|
|
})
|
|
} catch (error) {
|
|
return response.internalServerError({
|
|
status: false,
|
|
message: 'Failed to fetch scenarios',
|
|
error,
|
|
})
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create a new scenario
|
|
*/
|
|
async create({ request, response, auth }: HttpContext) {
|
|
try {
|
|
const payload = await request.all()
|
|
const trx = await db.transaction()
|
|
try {
|
|
// Check exist series
|
|
const inputSeries: string[] = payload.series.map((s: string) => s.trim().toUpperCase())
|
|
|
|
const existedScenarios = await Scenario.query().select('id', 'series')
|
|
|
|
const duplicatedSeries: string[] = []
|
|
|
|
for (const sc of existedScenarios) {
|
|
const scSeries: string[] = JSON.parse(sc.series || '[]').map((s: string) =>
|
|
s.trim().toUpperCase()
|
|
)
|
|
|
|
for (const s of inputSeries) {
|
|
if (scSeries.includes(s)) {
|
|
duplicatedSeries.push(s)
|
|
}
|
|
}
|
|
}
|
|
|
|
if (duplicatedSeries.length) {
|
|
return response.badRequest({
|
|
status: false,
|
|
message: 'Series already exists in another scenario',
|
|
duplicatedSeries: [...new Set(duplicatedSeries)],
|
|
})
|
|
}
|
|
|
|
const scenario = await Scenario.create(
|
|
{
|
|
title: payload.title.trim(),
|
|
body: JSON.stringify(payload.body),
|
|
timeout: payload.timeout,
|
|
isReboot: payload.isReboot,
|
|
send_result: payload.send_result,
|
|
brandId: payload.brandId,
|
|
categoryId: payload.categoryId,
|
|
note: payload.note,
|
|
series: JSON.stringify(payload.series),
|
|
},
|
|
{ client: trx }
|
|
)
|
|
|
|
await trx.commit()
|
|
|
|
// Lấy lại station kèm lines
|
|
const resScenario = await Scenario.query()
|
|
.where('id', scenario.id)
|
|
.preload('brand')
|
|
.preload('category')
|
|
|
|
return response.ok({
|
|
status: true,
|
|
message: 'Scenario created successfully',
|
|
data: resScenario,
|
|
})
|
|
} catch (error) {
|
|
await trx.rollback()
|
|
|
|
return response.internalServerError({
|
|
status: false,
|
|
message: 'Failed to create scenario, please try again!',
|
|
error,
|
|
})
|
|
}
|
|
} catch (error) {
|
|
return response.internalServerError({
|
|
status: false,
|
|
message: 'Failed to create scenario',
|
|
error,
|
|
})
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get a single scenario by ID
|
|
*/
|
|
async show({ params, response }: HttpContext) {
|
|
try {
|
|
const scenario = await Scenario.findOrFail(params.id)
|
|
return response.ok({ status: true, data: scenario })
|
|
} catch (error) {
|
|
return response.notFound({ status: false, message: 'Scenario not found' })
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update a scenario
|
|
*/
|
|
async update({ request, response, auth }: HttpContext) {
|
|
try {
|
|
const scenarioId = request.body().id
|
|
const payload = request.body()
|
|
const scenario = await Scenario.find(scenarioId)
|
|
if (!scenario) return response.status(404).json({ message: 'Scenario not found' })
|
|
// Check exist series
|
|
const inputSeries: string[] = payload.series.map((s: string) => s.trim().toUpperCase())
|
|
const existedScenarios = await Scenario.query().select('id', 'series')
|
|
const duplicatedSeries: string[] = []
|
|
for (const sc of existedScenarios) {
|
|
if (sc.id === scenarioId) continue
|
|
const scSeries: string[] = JSON.parse(sc.series || '[]').map((s: string) =>
|
|
s.trim().toUpperCase()
|
|
)
|
|
|
|
for (const s of inputSeries) {
|
|
if (scSeries.includes(s)) {
|
|
duplicatedSeries.push(s)
|
|
}
|
|
}
|
|
}
|
|
|
|
if (duplicatedSeries.length) {
|
|
return response.badRequest({
|
|
status: false,
|
|
message: 'Series already exists in another scenario',
|
|
duplicatedSeries: [...new Set(duplicatedSeries)],
|
|
})
|
|
}
|
|
|
|
payload.body = JSON.stringify(payload.body)
|
|
payload.series = JSON.stringify(payload.series)
|
|
scenario.merge(payload)
|
|
await scenario.save()
|
|
|
|
// Lấy lại station kèm lines
|
|
const resScenario = await Scenario.query()
|
|
.where('id', scenario.id)
|
|
.preload('brand')
|
|
.preload('category')
|
|
|
|
return response.ok({
|
|
status: true,
|
|
message: 'Scenario updated successfully',
|
|
data: resScenario,
|
|
})
|
|
} catch (error) {
|
|
return response.internalServerError({
|
|
status: false,
|
|
message: 'Failed to update scenario',
|
|
error,
|
|
})
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Delete a scenario
|
|
*/
|
|
async delete({ request, response }: HttpContext) {
|
|
try {
|
|
const scenarioId = request.body().id
|
|
const scenario = await Scenario.findOrFail(scenarioId)
|
|
|
|
if (!scenario) {
|
|
return response.notFound({ message: 'Scenario not found' })
|
|
}
|
|
|
|
await scenario.delete()
|
|
|
|
return response.ok({ status: true, message: 'Scenario deleted successfully' })
|
|
} catch (error) {
|
|
return response.internalServerError({
|
|
status: false,
|
|
message: 'Failed to delete scenario',
|
|
error,
|
|
})
|
|
}
|
|
}
|
|
}
|