Initial commit: PC Builder AdonisJS project

This commit is contained in:
Kevin
2026-06-28 10:41:51 +02:00
commit 648d0af538
144 changed files with 18570 additions and 0 deletions
+25
View File
@@ -0,0 +1,25 @@
import Component from '#models/component'
import Computer from '#models/computer'
async function handleComponents(componentsId: number[], computer: Computer) {
const currentComponents = await computer.related('components').query()
const currentComponentsIds = currentComponents.map((component) => component.id)
const componentsToAdd = componentsId.filter((id) => !currentComponentsIds.includes(id))
const componentsToRemove = currentComponentsIds.filter((id) => !componentsId.includes(id))
await Promise.all(
componentsToAdd.map(async (id: number) => {
const component = await Component.findOrFail(id)
component.computerId = computer.id
await component.save()
})
)
await Promise.all(
componentsToRemove.map(async (id: number) => {
const component = await Component.findOrFail(id)
component.computerId = null
await component.save()
})
)
}
export { handleComponents }