-
repo: https://github.com/fzhnf/api-endpoint-adonis i don't know what's the problem, i can update the row but i can't delete it, i keep configuring the migration and model of request
response
routes.tsrouter
.group(() => {
...
router
.put('/threads/:id', [ThreadController, 'update'])
.as('threads.update')
.use(middleware.auth())
router
.delete('/treads/:id', [ThreadController, 'destroy'])
.as('threads.destroy')
.use(middleware.auth())
...
})
.prefix('/api') Thread tableexport default class extends BaseSchema {
protected tableName = 'threads'
async up() {
this.schema.createTable(this.tableName, (table) => {
table.increments('id')
table
.integer('user_id')
.unsigned()
.references('id')
.inTable('users')
.notNullable()
.onDelete('CASCADE')
table
.integer('category_id')
.unsigned()
.references('id')
.inTable('categories')
.onDelete('SET NULL')
table.string('title').notNullable()
table.text('content').notNullable()
table.timestamp('created_at')
table.timestamp('updated_at')
})
}
async down() {
this.schema.dropTable(this.tableName)
}
} Thread controllerexport default class ThreadController {
...
async update({ params, auth, request, response }: HttpContext) {
try {
const thread = await Thread.findOrFail(params.id)
if (auth.user?.id !== thread.userId) {
return response.status(401).json({
message: 'Unauthorized to update this thread',
})
}
const validateData = await request.validateUsing(updateThreadValidator)
await thread.merge(validateData).save()
await thread?.load('category')
await thread?.load('user')
return response.status(200).json({
message: 'Thread updated successfully',
data: thread,
})
} catch (error) {
return response.status(400).json({
message: error.message,
})
}
}
async destroy({ params, auth, response }: HttpContext) {
try {
const thread = await Thread.findOrFail(params.id)
if (auth.user?.id !== thread.userId) {
return response.status(401).json({
message: 'Unauthorized to update this thread',
})
}
await thread.delete()
return response.status(200).json({
message: 'Thread deleted successfully',
})
} catch (error) {
return response.status(500).json({
error: error,
})
}
...
}
} response: even though i configuring route and controller mostly the same, somehow i can update the data but can't delete it |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
branch: https://github.com/fzhnf/api-endpoint-adonis/tree/feature/delete-tread |
Beta Was this translation helpful? Give feedback.
no way, it just a typo guys, lol
i can't see it for a whole day because there is no linting error or hint what so ever, because the routes is defined with string, now i can delete from database