artanis
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Art draw controller


From: Nala Ginrut
Subject: Re: Art draw controller
Date: Sat, 04 Jan 2020 00:43:09 +0800
User-agent: mu4e 1.3.5; emacs 26.1

Hi Michael!

Michael Vehrs writes:

> When you define a controller with "art draw controller", how do you
> request a database connection or a regex url or an url with parameters?

There're several ways to use DB in Artanis. Now that you use MVC, then
you may create a model now:
--------------------------------------
art draw model your_prefer_table_name
--------------------------------------
Let's say you have made a table named "article", now you should modify
app/models/article.scm
---------------------------------------------
(create-artanis-model                                                           
                                                                   article      
                                                                                
                                                    (id auto (#:not-null 
#:primary-key))                                                                 
                                            (title char-field (#:maxlen 30 
#:not-null))                                                                    
                                  (date char-field (#:maxlen 8))                
                                                                                
                   (content text))
---------------------------------------------
Please notice that if the table doesn't exist, then the model system
will create it for you by the definition you gave.

If the DB name is correctly specified in conf/artanis.conf,
then you can just import (app models people) in your controller, and use
$article (the $ will be added by the model generator automatically) to control 
the people table:
--------------------------------------
($article 'set #:title "hello" #:content "hello world")
;; or query
($article 'get #:columns '(content) #:condition (where #:title "hello"))
--------------------------------------

For example in app/controllers/article.scm:
------------------------------------------------------------------
(define-artanis-controller article) ; DO NOT REMOVE THIS LINE!!!
(use-modules (app models article)
(srfi srfi-1))

(article-define
add/:title/:content
(lambda (rc)
($article 'set #:title (params rc "title")
#:content (params rc "content"))
"ok"))

(article-define                                                                 
                                                                   show/:title  
                                                                                
                                                    (lambda (rc)                
                                                                                
                                       (tpl->html                               
                                                                                
                         (fold (lambda (x p)                                    
                                                                                
                  `(,@p                                                         
                                                                                
     ((h1 ,(params rc "title"))                                                 
                                                                       (p 
,(assoc-ref x "content")))))                                                    
                                                         '()                    
                                                                                
                                          ($article 'get #:columns '(content)   
                                                                                
                                     #:condition (where #:title (params rc 
"title")))))))                                                                  
      
-------------------------------------------------------------------------------

run 'art work -d'
-d means enbale DB, or you may enable "db.enable=true" in conf/artanis.conf

Now you can visit:
--------------------------------------------------------
curl "localhost:3000/article/add/hello/hello world"
curl localhost:3000/article/show/hello
--------------------------------------------------------


I've taken a glance at the manual, and I have to apologize that I forget
to add Model chapter. I'm too busy these days, on my startup
venture.
I'll do my best to make more tutorials on blogs and YouTube this year.

Best regards.

--
GNU Powered it
GPL Protected it
GOD Blessed it
HFG - NalaGinrut
Fingerprint F53B 4C56 95B5 E4D5 6093 4324 8469 6772 846A 0058

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]