ATC_SIMPLE/BACKEND/app/controllers/brands_controller.ts

40 lines
978 B
TypeScript

import Brand from '#models/brand'
import type { HttpContext } from '@adonisjs/core/http'
export default class BrandsController {
// GET /models
async index({}: HttpContext) {
const brands = await Brand.all()
return brands.sort((a, b) => a.id - b.id)
}
// POST /models
async store({ request }: HttpContext) {
const data = request.only(['name'])
const model = await Brand.create(data)
return model
}
// GET /models/:id
async show({ params }: HttpContext) {
const model = await Brand.findOrFail(params.id)
return model
}
// PUT /models/:id
async update({ params, request }: HttpContext) {
const model = await Brand.findOrFail(params.id)
const data = request.only(['name'])
model.merge(data)
await model.save()
return model
}
// DELETE /models/:id
async destroy({ params }: HttpContext) {
const model = await Brand.findOrFail(params.id)
await model.delete()
return { success: true }
}
}