119 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
			
		
		
	
	
			119 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
import type { HttpContext } from '@adonisjs/core/http'
 | 
						|
import Station from '#models/station'
 | 
						|
import Line from '#models/line'
 | 
						|
 | 
						|
export default class StationsController {
 | 
						|
  public async index({}: HttpContext) {
 | 
						|
    return await Station.query().preload('lines')
 | 
						|
  }
 | 
						|
 | 
						|
  public async store({ request, response }: HttpContext) {
 | 
						|
    let payload = request.only(Array.from(Station.$columnsDefinitions.keys()))
 | 
						|
    let lines: Line[] = request.body().lines || []
 | 
						|
 | 
						|
    try {
 | 
						|
      const stationName = await Station.findBy('name', payload.name)
 | 
						|
      if (stationName) return response.status(400).json({ message: 'Station name exist!' })
 | 
						|
 | 
						|
      const stationIP = await Station.findBy('ip', payload.ip)
 | 
						|
      if (stationIP) return response.status(400).json({ message: 'Ip of station is exist!' })
 | 
						|
 | 
						|
      const station = await Station.create(payload)
 | 
						|
 | 
						|
      const newLines: Line[] = []
 | 
						|
      if (lines && Array.isArray(lines)) {
 | 
						|
        lines.forEach(async (line) => {
 | 
						|
          if (line.id) {
 | 
						|
            const value = await Line.find(line.id)
 | 
						|
            if (value) {
 | 
						|
              Object.assign(value, line)
 | 
						|
              await value.save()
 | 
						|
              newLines.push(value)
 | 
						|
            } else {
 | 
						|
              const value1 = await Line.create({ ...line, stationId: station.id })
 | 
						|
              newLines.push(value1)
 | 
						|
            }
 | 
						|
          } else {
 | 
						|
            const value2 = await Line.create({ ...line, stationId: station.id })
 | 
						|
            newLines.push(value2)
 | 
						|
          }
 | 
						|
        })
 | 
						|
      }
 | 
						|
 | 
						|
      const resStation = await Station.query().where('id', station.id).preload('lines')
 | 
						|
 | 
						|
      return response.created({
 | 
						|
        status: true,
 | 
						|
        message: 'Station created successfully',
 | 
						|
        data: resStation.map((el) => ({ ...el.$original, lines: newLines })),
 | 
						|
      })
 | 
						|
    } catch (error) {
 | 
						|
      return response.badRequest({ error: error, message: 'Station create failed', status: false })
 | 
						|
    }
 | 
						|
  }
 | 
						|
 | 
						|
  public async show({ params }: HttpContext) {
 | 
						|
    return await Station.findOrFail(params.id)
 | 
						|
  }
 | 
						|
 | 
						|
  public async update({ request, response }: HttpContext) {
 | 
						|
    let payload = request.only(
 | 
						|
      Array.from(Station.$columnsDefinitions.keys()).filter(
 | 
						|
        (f) => f !== 'created_at' && f !== 'updated_at'
 | 
						|
      )
 | 
						|
    )
 | 
						|
    let lines: Line[] = request.body().lines || []
 | 
						|
 | 
						|
    try {
 | 
						|
      const station = await Station.find(request.body().id)
 | 
						|
 | 
						|
      // If the station does not exist, return a 404 response
 | 
						|
      if (!station) {
 | 
						|
        return response.status(404).json({ message: 'Station not found' })
 | 
						|
      }
 | 
						|
 | 
						|
      Object.assign(station, payload)
 | 
						|
 | 
						|
      await station.save()
 | 
						|
 | 
						|
      if (lines && Array.isArray(lines)) {
 | 
						|
        lines.forEach(async (line) => {
 | 
						|
          if (line.id) {
 | 
						|
            const value = await Line.find(line.id)
 | 
						|
            if (value) {
 | 
						|
              Object.assign(value, line)
 | 
						|
              await value.save()
 | 
						|
            } else await Line.create({ ...line, stationId: station.id })
 | 
						|
          } else await Line.create({ ...line, stationId: station.id })
 | 
						|
        })
 | 
						|
      }
 | 
						|
 | 
						|
      const resStation = await Station.query().where('id', station.id).preload('lines')
 | 
						|
 | 
						|
      return response.ok({ status: true, message: 'Station update successfully', data: resStation })
 | 
						|
    } catch (error) {
 | 
						|
      return response.badRequest({ error: error, message: 'Station update failed', status: false })
 | 
						|
    }
 | 
						|
  }
 | 
						|
 | 
						|
  public async destroy({ request, response }: HttpContext) {
 | 
						|
    try {
 | 
						|
      const station = await Station.find(request.body().id)
 | 
						|
 | 
						|
      // If the station does not exist, return a 404 response
 | 
						|
      if (!station) {
 | 
						|
        return response.status(404).json({ message: 'Station not found' })
 | 
						|
      }
 | 
						|
 | 
						|
      // Optionally, delete associated lines first
 | 
						|
      await station.related('lines').query().delete()
 | 
						|
 | 
						|
      // Delete the station
 | 
						|
      await station.delete()
 | 
						|
      return response.ok({ status: true, message: 'Station delete successfully', data: station })
 | 
						|
    } catch (error) {
 | 
						|
      return response.badRequest({ error: error, message: 'Station delete failed', status: false })
 | 
						|
    }
 | 
						|
  }
 | 
						|
}
 |