Initial commit: PC Builder AdonisJS project
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
import type { HttpContext } from '@adonisjs/core/http'
|
||||
import { storeComponentValidator } from '#validators/Components/store'
|
||||
import { updateComponentValidator } from '#validators/Components/update'
|
||||
import Component from '#models/component'
|
||||
import ComponentType from '#models/component_type'
|
||||
|
||||
export default class ComponentsController {
|
||||
async index({ inertia, auth }: HttpContext) {
|
||||
const user = await auth.authenticate()
|
||||
const components = await Component.query()
|
||||
.preload('type')
|
||||
.where('userId', user.id)
|
||||
return inertia.render('components/index', { components })
|
||||
}
|
||||
|
||||
async create({ inertia }: HttpContext) {
|
||||
const types = await ComponentType.all()
|
||||
return inertia.render('components/create', { types })
|
||||
}
|
||||
|
||||
async store({ request, response, auth }: HttpContext) {
|
||||
const data = request.all()
|
||||
const user = await auth.authenticate()
|
||||
const { quantity, ...componentData } = await storeComponentValidator.validate(data)
|
||||
await ComponentType.findOrFail(componentData.typeId)
|
||||
for (let i = 0; i < quantity; i++) {
|
||||
await Component.create({
|
||||
...componentData,
|
||||
userId: user.id,
|
||||
})
|
||||
}
|
||||
response.redirect('/components')
|
||||
}
|
||||
|
||||
async show({ params, auth, inertia }: HttpContext) {
|
||||
const user = await auth.authenticate()
|
||||
const componentId = params.id
|
||||
const component = await Component.query()
|
||||
.preload('type')
|
||||
.where('userId', user.id)
|
||||
.andWhere('id', componentId)
|
||||
.firstOrFail()
|
||||
return inertia.render('components/show', { component })
|
||||
}
|
||||
|
||||
async edit({ params, auth, inertia }: HttpContext) {
|
||||
const user = await auth.authenticate()
|
||||
const types = await ComponentType.all()
|
||||
const componentId = params.id
|
||||
const component = await Component.query()
|
||||
.preload('type')
|
||||
.where('userId', user.id)
|
||||
.andWhere('id', componentId)
|
||||
.firstOrFail()
|
||||
return inertia.render('components/edit', { component, types })
|
||||
}
|
||||
|
||||
async update({ params, auth, request, response }: HttpContext) {
|
||||
const data = request.all()
|
||||
const componentData = await updateComponentValidator.validate(data)
|
||||
const user = await auth.authenticate()
|
||||
const component = await Component.query()
|
||||
.where('userId', user.id)
|
||||
.andWhere('id', params.id)
|
||||
.firstOrFail()
|
||||
component.merge(componentData)
|
||||
await component.save()
|
||||
return response.redirect('/components')
|
||||
}
|
||||
|
||||
async destroy({ params, response }: HttpContext) {
|
||||
const componentId = params.id
|
||||
const component = await Component.findOrFail(componentId)
|
||||
await component.delete()
|
||||
return response.redirect('/components')
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user