@@ -1,4 +1,5 @@
|
||||
import type { HttpContext } from '@adonisjs/core/http'
|
||||
import { DateTime } from 'luxon'
|
||||
import { storeComponentValidator } from '#validators/Components/store'
|
||||
import { updateComponentValidator } from '#validators/Components/update'
|
||||
import Component from '#models/component'
|
||||
@@ -26,6 +27,7 @@ export default class ComponentsController {
|
||||
for (let i = 0; i < quantity; i++) {
|
||||
await Component.create({
|
||||
...componentData,
|
||||
purchaseDate: DateTime.fromFormat(componentData.purchaseDate, 'yyyy-MM-dd'),
|
||||
userId: user.id,
|
||||
})
|
||||
}
|
||||
@@ -63,7 +65,10 @@ export default class ComponentsController {
|
||||
.where('userId', user.id)
|
||||
.andWhere('id', params.id)
|
||||
.firstOrFail()
|
||||
component.merge(componentData)
|
||||
component.merge({
|
||||
...componentData,
|
||||
purchaseDate: DateTime.fromFormat(componentData.purchaseDate, 'yyyy-MM-dd'),
|
||||
})
|
||||
await component.save()
|
||||
return response.redirect('/components')
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { BaseModel } from '@adonisjs/lucid/orm'
|
||||
import type User from '#models/user'
|
||||
|
||||
export default class UserTransformer {
|
||||
static exclude = ['password']
|
||||
|
||||
transform(user: BaseModel) {
|
||||
static transform(user: typeof User.prototype) {
|
||||
return {
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
import vine from '@vinejs/vine'
|
||||
|
||||
export const updateComponentValidator = vine.compile(
|
||||
vine.object({
|
||||
name: vine.string().trim().minLength(1),
|
||||
price: vine.number().decimal([0, 2]).min(0),
|
||||
typeId: vine.number().min(1),
|
||||
purchaseDate: vine.string().regex(/^\d{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$/),
|
||||
})
|
||||
)
|
||||
@@ -0,0 +1,14 @@
|
||||
import vine from '@vinejs/vine'
|
||||
|
||||
export const storeComputerValidator = vine.compile(
|
||||
vine.object({
|
||||
title: vine.string().trim().minLength(1),
|
||||
sellPrice: vine.number().decimal([0, 2]).min(0),
|
||||
gpuScore: vine.number().min(0),
|
||||
cpuScore: vine.number().min(0),
|
||||
globalScore: vine.number().min(0),
|
||||
stateId: vine.number().min(1),
|
||||
componentsId: vine.array(vine.number()),
|
||||
pictures: vine.array(vine.object({})),
|
||||
})
|
||||
)
|
||||
@@ -0,0 +1,14 @@
|
||||
import vine from '@vinejs/vine'
|
||||
|
||||
export const updateComputerValidator = vine.compile(
|
||||
vine.object({
|
||||
title: vine.string().trim().minLength(1),
|
||||
sellPrice: vine.number().decimal([0, 2]).min(0),
|
||||
gpuScore: vine.number().min(0),
|
||||
cpuScore: vine.number().min(0),
|
||||
globalScore: vine.number().min(0),
|
||||
stateId: vine.number().min(1),
|
||||
componentsId: vine.array(vine.number()),
|
||||
pictures: vine.array(vine.object({})),
|
||||
})
|
||||
)
|
||||
Reference in New Issue
Block a user