Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions app/controllers/LabelController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// start/controllers/labels_controller.ts
import { HttpContext } from '@adonisjs/core/http'
import Label from '#models/label'
import { createLabelValidator } from '#validators/labels/create_label_validator'

export default class LabelsController {
// GET /labels - List all labels for the authenticated user
public async index({ auth }: HttpContext) {
await auth.check()
const user = auth.user!
return await Label.query().where('userId', user.id)
}

// POST /labels - Create a label
public async store({ request, auth }: HttpContext) {
await auth.check()
const user = auth.user!
const payload = await request.validateUsing(createLabelValidator)
return await Label.create({ ...payload, userId: user.id })
}

// DELETE /labels/:id - Delete a label
public async destroy({ params, auth, response }: HttpContext) {
await auth.check()
const user = auth.user!

const label = await Label.findOrFail(params.id)
if (label.userId !== user.id) {
return response.unauthorized({ message: 'Not allowed to delete this label' })
}

await label.delete()
return response.noContent()
}
}
Loading