ATC_SIMPLE/BACKEND/app/controllers/stations_controller.ts

125 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) {
const payload = request.only(Array.from(Station.$columnsDefinitions.keys()))
const lines: Line[] = request.body().lines || []
try {
// Kiểm tra trùng name hoặc ip
if (await Station.findBy('name', payload.name))
return response.status(400).json({ message: 'Station name exist!' })
if (await Station.findBy('ip', payload.ip))
return response.status(400).json({ message: 'Ip of station is exist!' })
// Tạo station
const station = await Station.create(payload)
// Xử lý lines (chờ từng dòng)
const newLines: Line[] = []
for (const line of lines) {
if (line.id) {
const existing = await Line.find(line.id)
if (existing) {
Object.assign(existing, line)
await existing.save()
newLines.push(existing)
continue
}
}
// Tạo mới nếu không tồn tại
const created = await Line.create({ ...line, stationId: station.id })
newLines.push(created)
}
// Lấy lại station kèm lines
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) {
console.error(error)
return response.badRequest({
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 })
}
}
}