47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
|
import User from 'App/Models/User'
|
|
import Database from '@ioc:Adonis/Lucid/Database'
|
|
|
|
export default class UsersController {
|
|
public async index({ inertia, bouncer }: HttpContextContract) {
|
|
|
|
await bouncer.with('UserPolicy').authorize('index')
|
|
|
|
const users = await Database
|
|
.from('users')
|
|
.select('id', 'username', 'is_admin')
|
|
|
|
return inertia.render('Users/Index', { users })
|
|
}
|
|
|
|
public async create({ auth, inertia }: HttpContextContract) {
|
|
if(auth.user?.isAdmin) {
|
|
inertia.render('Users/Create')
|
|
} else {
|
|
|
|
}
|
|
}
|
|
|
|
public async store({}: HttpContextContract) {}
|
|
|
|
public async show({}: HttpContextContract) {}
|
|
|
|
public async edit({}: HttpContextContract) {}
|
|
|
|
public async update({}: HttpContextContract) {}
|
|
|
|
public async destroy({ auth, response, params }: HttpContextContract) {
|
|
if (auth.user?.isAdmin){
|
|
const user = await User.findOrFail(params.id)
|
|
await user.delete()
|
|
.then(
|
|
() => {
|
|
response.redirect('users.index')
|
|
},
|
|
)
|
|
// TODO implement reasonable error handling.
|
|
.catch(error => console.log(error))
|
|
}
|
|
}
|
|
}
|