61 lines
1.5 KiB
TypeScript
61 lines
1.5 KiB
TypeScript
import { DateTime } from 'luxon'
|
|
import { BaseModel, belongsTo, column, computed, hasMany } from '@adonisjs/lucid/orm'
|
|
import type { BelongsTo, HasMany } from '@adonisjs/lucid/types/relations'
|
|
import ComputerState from '#models/computer_state'
|
|
import ComputerPicture from '#models/computer_picture'
|
|
import Component from '#models/component'
|
|
|
|
export default class Computer extends BaseModel {
|
|
@column({ isPrimary: true })
|
|
declare id: number
|
|
|
|
@column()
|
|
declare title: string
|
|
|
|
@column()
|
|
declare sellPrice: number
|
|
|
|
@column()
|
|
declare gpuScore: number
|
|
|
|
@column()
|
|
declare cpuScore: number
|
|
|
|
@column()
|
|
declare globalScore: number
|
|
|
|
@column({ serializeAs: null })
|
|
declare stateId: number
|
|
|
|
@belongsTo(() => ComputerState, {
|
|
foreignKey: 'stateId',
|
|
})
|
|
declare state: BelongsTo<typeof ComputerState>
|
|
|
|
@hasMany(() => Component)
|
|
declare components: HasMany<typeof Component>
|
|
|
|
@hasMany(() => ComputerPicture, {
|
|
foreignKey: 'computerId',
|
|
})
|
|
declare pictures: HasMany<typeof ComputerPicture>
|
|
|
|
@column({ serializeAs: null })
|
|
declare userId: number
|
|
|
|
@column.dateTime({ autoCreate: true })
|
|
declare createdAt: DateTime
|
|
|
|
@column.dateTime({ autoCreate: true, autoUpdate: true })
|
|
declare updatedAt: DateTime | null
|
|
|
|
@computed()
|
|
get cost(): number {
|
|
if (!this.$preloaded.components) {
|
|
console.warn('Components not preloaded, cost may be incorrect')
|
|
return 0
|
|
}
|
|
return this.components.reduce((total, component) => total + Number(component.price), 0)
|
|
}
|
|
}
|