133 lines
		
	
	
		
			3.4 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
			
		
		
	
	
			133 lines
		
	
	
		
			3.4 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()
 | 
						|
 | 
						|
      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 {
 | 
						|
        const scenario = await Scenario.create(
 | 
						|
          {
 | 
						|
            title: payload.title.trim(),
 | 
						|
            body: JSON.stringify(payload.body),
 | 
						|
            timeout: payload.timeout,
 | 
						|
            isReboot: payload.isReboot,
 | 
						|
          },
 | 
						|
          { client: trx }
 | 
						|
        )
 | 
						|
 | 
						|
        await trx.commit()
 | 
						|
 | 
						|
        return response.ok({
 | 
						|
          status: true,
 | 
						|
          message: 'Scenario created successfully',
 | 
						|
          data: scenario,
 | 
						|
        })
 | 
						|
      } 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' })
 | 
						|
      payload.body = JSON.stringify(payload.body)
 | 
						|
      scenario.merge(payload)
 | 
						|
      await scenario.save()
 | 
						|
 | 
						|
      return response.ok({ status: true, message: 'Scenario updated successfully', data: scenario })
 | 
						|
    } 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,
 | 
						|
      })
 | 
						|
    }
 | 
						|
  }
 | 
						|
}
 |