126 lines
3.9 KiB
TypeScript
126 lines
3.9 KiB
TypeScript
import ConfigRam from '#models/config_ram'
|
|
import type { HttpContext } from '@adonisjs/core/http'
|
|
|
|
export default class ConfigRamController {
|
|
/**
|
|
* Display a list of resource
|
|
*/
|
|
async get({}: HttpContext) {
|
|
const configRams = await ConfigRam.all()
|
|
return { status: true, data: configRams }
|
|
}
|
|
|
|
/**
|
|
* Display form to create a new record
|
|
*/
|
|
async create({ auth, request, response }: HttpContext) {
|
|
let payload = request.only([...Array.from(ConfigRam.$columnsDefinitions.keys())])
|
|
try {
|
|
// Check exist models
|
|
const inputModels: string[] = payload.models.map((s: string) => s.trim().toUpperCase())
|
|
const existedConfigs = await ConfigRam.query().select('id', 'models')
|
|
const duplicatedModels: string[] = []
|
|
for (const sc of existedConfigs) {
|
|
const scModels: string[] = JSON.parse(sc.models || '[]').map((s: string) =>
|
|
s.trim().toUpperCase()
|
|
)
|
|
for (const s of inputModels) {
|
|
if (scModels.includes(s)) {
|
|
duplicatedModels.push(s)
|
|
}
|
|
}
|
|
}
|
|
if (duplicatedModels.length) {
|
|
return response.badRequest({
|
|
status: false,
|
|
message: 'Models already exists in another config',
|
|
duplicatedModels: [...new Set(duplicatedModels)],
|
|
})
|
|
}
|
|
|
|
const configRam = await ConfigRam.create({
|
|
...payload,
|
|
models: JSON.stringify(payload.models),
|
|
})
|
|
return response.created({
|
|
status: true,
|
|
message: 'Config created successfully',
|
|
data: configRam,
|
|
})
|
|
} catch (error) {
|
|
return response.badRequest({ error: error, message: 'Config create failed', status: false })
|
|
}
|
|
}
|
|
|
|
async update({ request, response, auth }: HttpContext) {
|
|
let payload = request.only(
|
|
Array.from(ConfigRam.$columnsDefinitions.keys()).filter(
|
|
(f) => f !== 'created_at' && f !== 'updated_at'
|
|
)
|
|
)
|
|
try {
|
|
const configRam = await ConfigRam.find(request.body().id)
|
|
if (!configRam) {
|
|
return response.status(404).json({ message: 'Config not found' })
|
|
}
|
|
|
|
// Check exist models
|
|
const inputModels: string[] = payload.models.map((s: string) => s.trim().toUpperCase())
|
|
const existedConfigRams = await ConfigRam.query().select('id', 'models')
|
|
const duplicatedModels: string[] = []
|
|
for (const sc of existedConfigRams) {
|
|
if (sc.id === configRam.id) continue
|
|
const scModels: string[] = JSON.parse(sc.models || '[]').map((s: string) =>
|
|
s.trim().toUpperCase()
|
|
)
|
|
|
|
for (const s of inputModels) {
|
|
if (scModels.includes(s)) {
|
|
duplicatedModels.push(s)
|
|
}
|
|
}
|
|
}
|
|
|
|
if (duplicatedModels.length) {
|
|
return response.badRequest({
|
|
status: false,
|
|
message: 'Models already exists in another config',
|
|
duplicatedModels: [...new Set(duplicatedModels)],
|
|
})
|
|
}
|
|
|
|
Object.assign(configRam, { ...payload, models: JSON.stringify(payload.models) })
|
|
await configRam.save()
|
|
return response.ok({ status: true, message: 'Config update successfully', data: configRam })
|
|
} catch (error) {
|
|
return response.badRequest({ error: error, message: 'Config update failed', status: false })
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Delete record
|
|
*/
|
|
async delete({ auth, request, response }: HttpContext) {
|
|
try {
|
|
const configRam = await ConfigRam.find(request.body().id)
|
|
if (!configRam) {
|
|
return response.status(404).json({ message: 'Config not found' })
|
|
}
|
|
|
|
// Delete the configRam
|
|
await configRam.delete()
|
|
return response.ok({
|
|
status: true,
|
|
message: 'Config Ram delete successfully',
|
|
data: configRam,
|
|
})
|
|
} catch (error) {
|
|
return response.badRequest({
|
|
error: error,
|
|
message: 'Config Ram delete failed',
|
|
status: false,
|
|
})
|
|
}
|
|
}
|
|
}
|