arrays are traversed in 04-databases/basic-blog

This commit is contained in:
programarivm 2019-09-27 17:54:54 +01:00
parent 9bbf76e914
commit a24c24776b
3 changed files with 56 additions and 71 deletions

View File

@ -1,18 +1,22 @@
fetch(n,data) fetchByComment(idComment,data)
n record n record
k data k data
i n="" q 0 i idComment="" q 0
s record=$get(^comments(n)) s lev1=""
s data("description")=$piece(record,"~",1) s lev2=""
s data("id_post")=$piece(record,"~",2) f s lev1=$o(^comments(lev1)) q:lev1="" d
q 1 . f s lev2=$o(^comments(lev1,lev2)) q:lev2="" d
. . i lev1=idComment d
. . . s record=$get(^comments(idComment,lev2))
. . . s data("description")=$piece(record,"~",1)
. . . q
q
set(n,data) set(idComment,idPost,data)
i n="" q 0 i idComment=""!idPost="" q 0
s description=$piece(data,"~",1) s description=$piece(data,"~",1)
s idPost=$piece(data,"~",2) s ^comments(idComment,idPost)=description
s ^comments(n)=description_"~"_idPost
q 1 q 1
remove(n) remove(n)

View File

@ -2,71 +2,48 @@
; create users ; create users
s ok=$$set^UserRoutines(1,"alice~password~alice-jones@no-reply.com~Alice Jones") s ok=$$set^UserRoutines(1,"alice~password~alice-jones@no-reply.com~Alice Jones")
i ok=1 d
. w "User successfully created!",!
e d
. w "Whoops! The user could not be created.",!
s ok=$$set^UserRoutines(2,"bob~password~bob-smith@no-reply.com~Bob Smith") s ok=$$set^UserRoutines(2,"bob~password~bob-smith@no-reply.com~Bob Smith")
i ok=1 d
. w "User successfully created!",!
e d
. w "Whoops! The user could not be created.",!
; create posts ; create posts
s ok=$$set^PostRoutines(1,"lorem-ipsum~Lorem ipsum~Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.~1") s ok=$$set^PostRoutines(1,1,"lorem-ipsum~Lorem ipsum~Lorem ipsum dolor sit amet, consectetuer adipiscing elit.")
i ok=1 d s ok=$$set^PostRoutines(2,1,"donec-pede-justo~Donec pede justo~Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.")
. w "Post successfully created!",! s ok=$$set^PostRoutines(3,2,"curabitur-ullamcorper-ultricies~Curabitur ullamcorper ultricies~ Curabitur ullamcorper ultricies nisi.")
e d s ok=$$set^PostRoutines(4,2,"donec-vitae-sapien~Donec vitae sapien~Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante.")
. w "Whoops! The post could not be created.",!
s ok=$$set^PostRoutines(2,"excepteur-sint-occaecat~Excepteur sint occaecat~Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.~2")
i ok=1 d
. w "Post successfully created!",!
e d
. w "Whoops! The post could not be created.",!
; create comments ; create comments
s ok=$$set^CommentRoutines(1,"This is awesome. Thank you.~1") s ok=$$set^CommentRoutines(1,1,"This is awesome! Thank you.")
i ok=1 d s ok=$$set^CommentRoutines(2,2,"Thank you so much for sharing this.")
. w "Comment successfully created!",!
e d
. w "Whoops! The comment could not be created.",!
s ok=$$set^CommentRoutines(2,"Thank you so much for sharing this.~2") ; fetch posts
i ok=1 d
. w "Comment successfully created!",!
e d
. w "Whoops! The comment could not be created.",!
; fetch a post do fetchByPost^PostRoutines(1,.data)
w "Post fetched:",!
w "slug: ",data("slug"),!
w "title: ",data("title"),!
w "description: ",data("description"),!
s ok=$$fetch^PostRoutines(1,.data) do fetchByPost^PostRoutines(2,.data)
i ok=1 d w !,"Post fetched:",!
. w !,"Post successfully fetched!",! w "slug: ",data("slug"),!
. w "slug: ",data("slug"),! w "title: ",data("title"),!
. w "title: ",data("title"),! w "description: ",data("description"),!
. w "description: ",data("description"),!
. w "id_user: ",data("id_user"),!
e d
. w "Whoops! The post could not be fetched.",!
; fetch a post ; fetch comments
s ok=$$fetch^CommentRoutines(2,.data) do fetchByComment^CommentRoutines(1,.data)
i ok=1 d w !,"Comment fetched:",!
. w !,"Comment successfully fetched!",! w "description: ",data("description"),!
. w "description: ",data("description"),!
. w "id_post: ",data("id_post"),!,! do fetchByComment^CommentRoutines(2,.data)
e d w !,"Comment fetched:",!
. w "Whoops! The post could not be fetched.",! w "description: ",data("description"),!
; drop database ; drop database
s ok=$$drop^DatabaseRoutines() s ok=$$drop^DatabaseRoutines()
i ok=1 d i ok=1 d
. w "Database successfully dropped!",! . w !,"Database successfully dropped!",!
e d e d
. w "Whoops! The database could not be dropped.",! . w "Whoops! The database could not be dropped.",!

View File

@ -1,22 +1,26 @@
fetch(n,data) fetchByPost(idPost,data)
n record n record
k data k data
i n="" q 0 i idPost="" q 0
s record=$get(^posts(n)) s lev1=""
s data("slug")=$piece(record,"~",1) s lev2=""
s data("title")=$piece(record,"~",2) f s lev1=$o(^posts(lev1)) q:lev1="" d
s data("description")=$piece(record,"~",3) . f s lev2=$o(^posts(lev1,lev2)) q:lev2="" d
s data("id_user")=$piece(record,"~",4) . . i lev1=idPost d
q 1 . . . s record=$get(^posts(idPost,lev2))
. . . s data("slug")=$piece(record,"~",1)
. . . s data("title")=$piece(record,"~",2)
. . . s data("description")=$piece(record,"~",3)
. . . q
q
set(n,data) set(idPost,idUser,data)
i n="" q 0 i idPost=""!idUser="" q 0
s slug=$piece(data,"~",1) s slug=$piece(data,"~",1)
s title=$piece(data,"~",2) s title=$piece(data,"~",2)
s description=$piece(data,"~",3) s description=$piece(data,"~",3)
s idUser=$piece(data,"~",4) s ^posts(idPost,idUser)=slug_"~"_title_"~"_description
s ^posts(n)=slug_"~"_title_"~"_description_"~"_idUser
q 1 q 1
remove(n) remove(n)