Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
Example backend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Thomas SAUVAGE
Example backend
Commits
12d043da
Commit
12d043da
authored
1 year ago
by
Thibaut DE SAIVRE
Committed by
Thomas SAUVAGE
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
update model relation
parent
71cce4db
No related branches found
Branches containing commit
No related tags found
1 merge request
!1
Idiomatic relations
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
app/Controllers/events.ts
+4
-16
4 additions, 16 deletions
app/Controllers/events.ts
app/Models/Event.ts
+8
-2
8 additions, 2 deletions
app/Models/Event.ts
database/migrations/1698921052424_events.ts
+2
-1
2 additions, 1 deletion
database/migrations/1698921052424_events.ts
with
14 additions
and
19 deletions
app/Controllers/events.ts
+
4
−
16
View file @
12d043da
import
type
{
HttpContextContract
}
from
'
@ioc:Adonis/Core/HttpContext
'
import
Event
from
'
App/Models/Event
'
import
{
Tree
}
from
'
App/Utils/types
'
/** Get all events */
export
const
getAllRoots
=
async
({
response
}:
HttpContextContract
)
=>
{
...
...
@@ -10,19 +9,10 @@ export const getAllRoots = async ({ response }: HttpContextContract) => {
}
/** Recursively gives a list of sons events */
const
getSons
=
async
(
id
:
number
)
:
Promise
<
Tree
<
Event
>>
=>
{
const
event
=
await
Event
.
find
(
id
)
const
getSons
=
async
(
id
:
number
)
=>
{
const
event
=
await
Event
.
find
OrFail
(
id
)
// Use this to throw an error automatically when the item is not found
if
(
!
event
)
{
throw
new
Error
(
'
Event not found
'
)
}
let
sons
:
Tree
<
Event
>
[]
=
[]
for
(
const
sonId
of
event
.
sonIds
)
{
sons
.
push
(
await
getSons
(
sonId
))
}
return
{
father
:
event
,
sons
}
return
event
.
related
(
'
children
'
).
query
()
// Query level 1 children like this (this is not recursive)
}
/** Get all sons of a given event, using params */
...
...
@@ -64,9 +54,7 @@ export const createEvent = async ({ request, response }: HttpContextContract) =>
return
response
.
badRequest
({
error
:
'
Father not found
'
})
}
const
event
=
await
Event
.
create
({
...
data
,
isRoot
:
false
})
father
.
sonIds
.
push
(
event
.
id
)
await
Event
.
create
({
...
data
,
isRoot
:
false
,
parentId
:
father
.
id
})
await
father
.
save
()
return
response
.
created
({
message
:
'
Son event created
'
})
...
...
This diff is collapsed.
Click to expand it.
app/Models/Event.ts
+
8
−
2
View file @
12d043da
import
{
BaseModel
,
column
}
from
'
@ioc:Adonis/Lucid/Orm
'
import
{
BaseModel
,
HasMany
,
column
,
hasMany
}
from
'
@ioc:Adonis/Lucid/Orm
'
import
{
DateTime
}
from
'
luxon
'
export
default
class
Event
extends
BaseModel
{
...
...
@@ -12,7 +12,13 @@ export default class Event extends BaseModel {
public
thumbnailId
:
number
@
column
()
public
sonIds
:
number
[]
// Represents the ids of the sons of the event (tree structure)
public
parentId
:
number
|
null
@
hasMany
(()
=>
Event
,
{
foreignKey
:
'
parentId
'
,
// An Event is a child of this one if its parentId is equal to this Event's id
localKey
:
'
id
'
,
})
public
children
:
HasMany
<
typeof
Event
>
@
column
()
public
isRoot
:
boolean
// Represents if the event is the root of the tree
...
...
This diff is collapsed.
Click to expand it.
database/migrations/1698921052424_events.ts
+
2
−
1
View file @
12d043da
...
...
@@ -9,7 +9,8 @@ export default class extends BaseSchema {
table
.
string
(
'
name
'
,
255
).
notNullable
()
table
.
bigint
(
'
thumbnail_id
'
).
notNullable
()
table
.
specificType
(
'
son_ids
'
,
'
integer[]
'
).
notNullable
().
defaultTo
(
'
{}
'
)
// NOTE : on delete cascade will delete child events when the parent id they point to becomes invalid
table
.
integer
(
'
parent_id
'
).
nullable
().
references
(
'
id
'
).
inTable
(
'
events
'
).
onDelete
(
'
CASCADE
'
)
table
.
boolean
(
'
is_root
'
).
notNullable
()
/**
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment