Fixing pr11280
This commit is contained in:
parent
2f56b23b21
commit
09fbfcf5a9
|
@ -62,9 +62,9 @@ By the end of this tutorial, we'll have a simple blogging application.
|
|||
We start by considering our data definitions. We want to represent a
|
||||
list of posts. Let's say that a post is:
|
||||
|
||||
@racketblock[(define-struct post (title body))]
|
||||
@racketblock[(struct post (title body))]
|
||||
|
||||
@(defstruct post ([title string?] [body string?]))
|
||||
@(defstruct* post ([title string?] [body string?]))
|
||||
|
||||
@bold{Exercise.} Make a few examples of posts.
|
||||
|
||||
|
@ -75,8 +75,8 @@ A blog, then, will be a list of posts:
|
|||
As a very simple example of a blog:
|
||||
|
||||
@racketblock[
|
||||
(define BLOG (list (make-post "First Post!"
|
||||
"Hey, this is my first post!")))
|
||||
(define BLOG (list (post "First Post!"
|
||||
"Hey, this is my first post!")))
|
||||
]
|
||||
|
||||
Now that we have a sample blog structure, let's get our web
|
||||
|
@ -172,7 +172,7 @@ an @racket[html-response] representing that content.
|
|||
As an example, we want:
|
||||
|
||||
@racketblock[
|
||||
(render-post (make-post "First post!" "This is a first post."))
|
||||
(render-post (post "First post!" "This is a first post."))
|
||||
]
|
||||
|
||||
to produce:
|
||||
|
@ -229,8 +229,8 @@ should produce:
|
|||
While
|
||||
|
||||
@racketblock[
|
||||
(render-posts (list (make-post "Post 1" "Body 1")
|
||||
(make-post "Post 2" "Body 2")))
|
||||
(render-posts (list (post "Post 1" "Body 1")
|
||||
(post "Post 2" "Body 2")))
|
||||
]
|
||||
|
||||
should produce:
|
||||
|
@ -441,9 +441,9 @@ Earlier, we had said that a @racket[blog] was a list of @racket[post]s,
|
|||
but because we want to allow the blog to be changed, let's revisit our
|
||||
definition so that a blog is a mutable structure:
|
||||
|
||||
@racketblock[(define-struct blog (posts) #:mutable)]
|
||||
@racketblock[(struct blog (posts) #:mutable)]
|
||||
|
||||
@defstruct[blog ([posts (listof post?)])]
|
||||
@defstruct*[blog ([posts (listof post?)])]
|
||||
|
||||
Mutable structures provide functions to change the fields of a
|
||||
structure; in this case, we now have a structure mutator called
|
||||
|
@ -484,7 +484,7 @@ the same blog.
|
|||
Next, let's extend the application so that each post can hold a list
|
||||
of comments. We refine the data definition of a blog to be:
|
||||
|
||||
@defstruct[post ([title string?] [body string?] [comments (listof string?)]) #:mutable]
|
||||
@defstruct*[post ([title string?] [body string?] [comments (listof string?)]) #:mutable]
|
||||
|
||||
@bold{Exercise.} Write the updated data structure definition for posts. Make
|
||||
sure to make the structure mutable, since we intend to add comments to
|
||||
|
@ -504,7 +504,7 @@ comments in an itemized list.
|
|||
|
||||
@bold{Exercise.} Because we've extended a post to include comments, other
|
||||
post-manipulating parts of the application may need to be adjusted,
|
||||
such as uses of @racket[make-post]. Identify and fix any other part of the
|
||||
such as uses of @racket[post]. Identify and fix any other part of the
|
||||
application that needs to accommodate the post's new structure.
|
||||
|
||||
@centerline{------------}
|
||||
|
@ -736,8 +736,8 @@ between the model of our blog, and the web application that uses that
|
|||
model. Let's isolate the model: it's all the stuff near the top:
|
||||
|
||||
@racketblock[
|
||||
(define-struct blog (posts) #:mutable)
|
||||
(define-struct post (title body comments) #:mutable)
|
||||
(struct blog (posts) #:mutable)
|
||||
(struct post (title body comments) #:mutable)
|
||||
(define BLOG ...)
|
||||
(define (blog-insert-post! ...) ...)
|
||||
(define (post-insert-comment! ...) ...)
|
||||
|
@ -794,7 +794,7 @@ started running---which is exactly what we want when restoring the blog data fro
|
|||
Our blog structure definition now looks like:
|
||||
|
||||
@racketblock[
|
||||
(define-struct blog (posts) #:mutable #:prefab)
|
||||
(struct blog (posts) #:mutable #:prefab)
|
||||
]
|
||||
|
||||
Now @racket[blog] structures can be read from the outside world with @racket[read] and written
|
||||
|
@ -809,7 +809,7 @@ At this point, we @emph{can} read and write the blog to disk. Now let's actually
|
|||
First, we'll make a place to record in the model where the blog lives on disk. So, we need to change
|
||||
the blog structure again. Now it will be:
|
||||
|
||||
@defstruct[blog ([home string?] [posts (listof post?)]) #:mutable]
|
||||
@defstruct*[blog ([home string?] [posts (listof post?)]) #:mutable]
|
||||
|
||||
@bold{Exercise.} Write the new structure definition for blogs.
|
||||
|
||||
|
@ -820,14 +820,14 @@ Then, we'll make a function that allows our application to initialize the blog:
|
|||
@code:comment{Reads a blog from a path, if not present, returns default}
|
||||
(define (initialize-blog! home)
|
||||
(local [(define (log-missing-exn-handler exn)
|
||||
(make-blog
|
||||
(blog
|
||||
(path->string home)
|
||||
(list (make-post "First Post"
|
||||
"This is my first post"
|
||||
(list "First comment!"))
|
||||
(make-post "Second Post"
|
||||
"This is another post"
|
||||
(list)))))
|
||||
(list (post "First Post"
|
||||
"This is my first post"
|
||||
(list "First comment!"))
|
||||
(post "Second Post"
|
||||
"This is another post"
|
||||
(list)))))
|
||||
(define the-blog
|
||||
(with-handlers ([exn? log-missing-exn-handler])
|
||||
(with-input-from-file home read)))]
|
||||
|
@ -983,7 +983,7 @@ By adding a new comments table, we are more in accord with the relational style.
|
|||
|
||||
A @racket[blog] structure will simply be a container for the database handle:
|
||||
|
||||
@defstruct[blog ([db sqlite:db?])]
|
||||
@defstruct*[blog ([db sqlite:db?])]
|
||||
|
||||
@bold{Exercise.} Write the @racket[blog] structure definition. (It does not need to be mutable or serializable.)
|
||||
|
||||
|
@ -993,7 +993,7 @@ We can now write the code to initialize a @racket[blog] structure:
|
|||
@code:comment{Sets up a blog database (if it doesn't exist)}
|
||||
(define (initialize-blog! home)
|
||||
(define db (sqlite:open home))
|
||||
(define the-blog (make-blog db))
|
||||
(define the-blog (blog db))
|
||||
(with-handlers ([exn? void])
|
||||
(sqlite:exec/ignore db
|
||||
(string-append
|
||||
|
@ -1056,7 +1056,7 @@ However, we cannot tell from this structure
|
|||
what blog this posts belongs to, and therefore, what database; so, we could not extract the title or body values,
|
||||
since we do not know what to query. Therefore, we should associate the blog with each post:
|
||||
|
||||
@defstruct[post ([blog blog?] [id integer?])]
|
||||
@defstruct*[post ([blog blog?] [id integer?])]
|
||||
|
||||
@bold{Exercise.} Write the structure definition for posts.
|
||||
|
||||
|
@ -1067,7 +1067,7 @@ The only function that creates posts is @racket[blog-posts]:
|
|||
@code:comment{Queries for the post ids}
|
||||
(define (blog-posts a-blog)
|
||||
(local [(define (row->post a-row)
|
||||
(make-post
|
||||
(post
|
||||
a-blog
|
||||
(vector-ref a-row 0)))
|
||||
(define rows (sqlite:select
|
||||
|
|
|
@ -3,17 +3,17 @@
|
|||
|
||||
;; A blog is a (make-blog db)
|
||||
;; where db is an sqlite database handle
|
||||
(define-struct blog (db))
|
||||
(struct blog (db))
|
||||
|
||||
;; A post is a (make-post blog id)
|
||||
;; where blog is a blog and id is an integer?
|
||||
(define-struct post (blog id))
|
||||
(struct post (blog id))
|
||||
|
||||
;; initialize-blog! : path? -> blog?
|
||||
;; Sets up a blog database (if it doesn't exist)
|
||||
(define (initialize-blog! home)
|
||||
(define db (sqlite:open home))
|
||||
(define the-blog (make-blog db))
|
||||
(define the-blog (blog db))
|
||||
(with-handlers ([exn? void])
|
||||
(sqlite:exec/ignore db
|
||||
(string-append
|
||||
|
@ -35,7 +35,7 @@
|
|||
;; Queries for the post ids
|
||||
(define (blog-posts a-blog)
|
||||
(local [(define (row->post a-row)
|
||||
(make-post a-blog (string->number (vector-ref a-row 0))))
|
||||
(post a-blog (string->number (vector-ref a-row 0))))
|
||||
(define rows (sqlite:select
|
||||
(blog-db a-blog)
|
||||
"SELECT id FROM posts"))]
|
||||
|
|
|
@ -2,20 +2,20 @@
|
|||
|
||||
;; A blog is a (listof post)
|
||||
;; and a post is a (make-post title body)
|
||||
(define-struct post (title body))
|
||||
(struct post (title body))
|
||||
|
||||
;; BLOG: blog
|
||||
;; The static blog.
|
||||
(define BLOG
|
||||
(list (make-post "First Post" "This is my first post")
|
||||
(make-post "Second Post" "This is another post")))
|
||||
(list (post "First Post" "This is my first post")
|
||||
(post "Second Post" "This is another post")))
|
||||
|
||||
;; start: request -> html-response
|
||||
;; Consumes a request, and produces a page that displays all of the
|
||||
;; web content.
|
||||
(define (start request)
|
||||
(render-blog-page BLOG request))
|
||||
|
||||
|
||||
;; render-blog-page: blog request -> html-response
|
||||
;; Consumes a blog and a request, and produces an html-response page
|
||||
;; of the content of the blog.
|
||||
|
@ -23,7 +23,7 @@
|
|||
`(html (head (title "My Blog"))
|
||||
(body (h1 "My Blog")
|
||||
,(render-posts a-blog))))
|
||||
|
||||
|
||||
;; render-post: post -> html-response
|
||||
;; Consumes a post, produces an html-response fragment of the post.
|
||||
(define (render-post a-post)
|
||||
|
|
|
@ -2,13 +2,13 @@
|
|||
|
||||
;; A blog is a (listof post)
|
||||
;; and a post is a (make-post title body)
|
||||
(define-struct post (title body))
|
||||
(struct post (title body))
|
||||
|
||||
;; BLOG: blog
|
||||
;; The static blog.
|
||||
(define BLOG
|
||||
(list (make-post "First Post" "This is my first post")
|
||||
(make-post "Second Post" "This is another post")))
|
||||
(list (post "First Post" "This is my first post")
|
||||
(post "Second Post" "This is another post")))
|
||||
|
||||
;; start: request -> html-response
|
||||
;; Consumes a request and produces a page that displays all of the
|
||||
|
@ -21,7 +21,7 @@
|
|||
[else
|
||||
BLOG]))]
|
||||
(render-blog-page a-blog request)))
|
||||
|
||||
|
||||
|
||||
;; can-parse-post?: bindings -> boolean
|
||||
;; Produces true if bindings contains values for 'title and 'body.
|
||||
|
@ -33,8 +33,8 @@
|
|||
;; parse-post: bindings -> post
|
||||
;; Consumes a bindings, and produces a post out of the bindings.
|
||||
(define (parse-post bindings)
|
||||
(make-post (extract-binding/single 'title bindings)
|
||||
(extract-binding/single 'body bindings)))
|
||||
(post (extract-binding/single 'title bindings)
|
||||
(extract-binding/single 'body bindings)))
|
||||
|
||||
;; render-blog-page: blog request -> html-response
|
||||
;; Consumes a blog and a request, and produces an html-response page
|
||||
|
@ -49,8 +49,6 @@
|
|||
(input ((name "body")))
|
||||
(input ((type "submit")))))))
|
||||
|
||||
|
||||
|
||||
;; render-post: post -> html-response
|
||||
;; Consumes a post, produces an html-response fragment of the post.
|
||||
(define (render-post a-post)
|
||||
|
|
|
@ -2,25 +2,25 @@
|
|||
|
||||
;; A blog is a (listof post)
|
||||
;; and a post is a (make-post title body)
|
||||
(define-struct post (title body))
|
||||
(struct post (title body))
|
||||
|
||||
;; BLOG: blog
|
||||
;; The static blog.
|
||||
(define BLOG
|
||||
(list (make-post "First Post" "This is my first post")
|
||||
(make-post "Second Post" "This is another post")))
|
||||
(list (post "First Post" "This is my first post")
|
||||
(post "Second Post" "This is another post")))
|
||||
|
||||
;; start: request -> html-response
|
||||
;; Consumes a request and produces a page that displays all of the
|
||||
;; web content.
|
||||
(define (start request)
|
||||
(render-blog-page BLOG request))
|
||||
|
||||
|
||||
;; parse-post: bindings -> post
|
||||
;; Extracts a post out of the bindings.
|
||||
(define (parse-post bindings)
|
||||
(make-post (extract-binding/single 'title bindings)
|
||||
(extract-binding/single 'body bindings)))
|
||||
(post (extract-binding/single 'title bindings)
|
||||
(extract-binding/single 'body bindings)))
|
||||
|
||||
;; render-blog-page: blog request -> html-response
|
||||
;; Consumes a blog and a request, and produces an html-response page
|
||||
|
@ -33,16 +33,16 @@
|
|||
,(render-posts a-blog)
|
||||
(form ((action
|
||||
,(make-url insert-post-handler)))
|
||||
(input ((name "title")))
|
||||
(input ((name "body")))
|
||||
(input ((type "submit")))))))
|
||||
(input ((name "title")))
|
||||
(input ((name "body")))
|
||||
(input ((type "submit")))))))
|
||||
|
||||
(define (insert-post-handler request)
|
||||
(render-blog-page
|
||||
(cons (parse-post (request-bindings request))
|
||||
a-blog)
|
||||
request))]
|
||||
|
||||
|
||||
(send/suspend/dispatch response-generator)))
|
||||
|
||||
;; render-post: post -> html-response
|
||||
|
|
|
@ -2,25 +2,25 @@
|
|||
|
||||
;; A blog is a (make-blog posts)
|
||||
;; where posts is a (listof post)
|
||||
(define-struct blog (posts) #:mutable)
|
||||
(struct blog (posts) #:mutable)
|
||||
|
||||
;; and post is a (make-post title body)
|
||||
;; where title is a string, and body is a string
|
||||
(define-struct post (title body))
|
||||
(struct post (title body))
|
||||
|
||||
;; BLOG: blog
|
||||
;; The initial BLOG.
|
||||
(define BLOG
|
||||
(make-blog
|
||||
(list (make-post "First Post" "This is my first post")
|
||||
(make-post "Second Post" "This is another post"))))
|
||||
(blog
|
||||
(list (post "First Post" "This is my first post")
|
||||
(post "Second Post" "This is another post"))))
|
||||
|
||||
;; blog-insert-post!: blog post -> void
|
||||
;; Consumes a blog and a post, adds the post at the top of the blog.
|
||||
(define (blog-insert-post! a-blog a-post)
|
||||
(set-blog-posts! a-blog
|
||||
(cons a-post (blog-posts a-blog))))
|
||||
|
||||
|
||||
;; start: request -> html-response
|
||||
;; Consumes a request and produces a page that displays
|
||||
;; all of the web content.
|
||||
|
@ -30,8 +30,8 @@
|
|||
;; parse-post: bindings -> post
|
||||
;; Extracts a post out of the bindings.
|
||||
(define (parse-post bindings)
|
||||
(make-post (extract-binding/single 'title bindings)
|
||||
(extract-binding/single 'body bindings)))
|
||||
(post (extract-binding/single 'title bindings)
|
||||
(extract-binding/single 'body bindings)))
|
||||
|
||||
;; render-blog-page: request -> html-response
|
||||
;; Produces an html-response page of the content of the BLOG.
|
||||
|
@ -43,15 +43,15 @@
|
|||
,(render-posts)
|
||||
(form ((action
|
||||
,(make-url insert-post-handler)))
|
||||
(input ((name "title")))
|
||||
(input ((name "body")))
|
||||
(input ((type "submit")))))))
|
||||
(input ((name "title")))
|
||||
(input ((name "body")))
|
||||
(input ((type "submit")))))))
|
||||
|
||||
(define (insert-post-handler request)
|
||||
(blog-insert-post!
|
||||
BLOG (parse-post (request-bindings request)))
|
||||
(render-blog-page request))]
|
||||
|
||||
|
||||
(send/suspend/dispatch response-generator)))
|
||||
|
||||
;; render-post: post -> html-response
|
||||
|
|
|
@ -2,23 +2,23 @@
|
|||
|
||||
;; A blog is a (make-blog posts)
|
||||
;; where posts is a (listof post)
|
||||
(define-struct blog (posts) #:mutable)
|
||||
(struct blog (posts) #:mutable)
|
||||
|
||||
;; and post is a (make-post title body comments)
|
||||
;; where title is a string, body is a string,
|
||||
;; and comments is a (listof string)
|
||||
(define-struct post (title body comments) #:mutable)
|
||||
(struct post (title body comments) #:mutable)
|
||||
|
||||
;; BLOG: blog
|
||||
;; The initial BLOG.
|
||||
(define BLOG
|
||||
(make-blog
|
||||
(list (make-post "First Post"
|
||||
"This is my first post"
|
||||
(list "First comment!"))
|
||||
(make-post "Second Post"
|
||||
"This is another post"
|
||||
(list)))))
|
||||
(blog
|
||||
(list (post "First Post"
|
||||
"This is my first post"
|
||||
(list "First comment!"))
|
||||
(post "Second Post"
|
||||
"This is another post"
|
||||
(list)))))
|
||||
|
||||
;; blog-insert-post!: blog post -> void
|
||||
;; Consumes a blog and a post, adds the post at the top of the blog.
|
||||
|
@ -52,22 +52,22 @@
|
|||
,(render-posts make-url)
|
||||
(form ((action
|
||||
,(make-url insert-post-handler)))
|
||||
(input ((name "title")))
|
||||
(input ((name "body")))
|
||||
(input ((type "submit")))))))
|
||||
(input ((name "title")))
|
||||
(input ((name "body")))
|
||||
(input ((type "submit")))))))
|
||||
|
||||
;; parse-post: bindings -> post
|
||||
;; Extracts a post out of the bindings.
|
||||
(define (parse-post bindings)
|
||||
(make-post (extract-binding/single 'title bindings)
|
||||
(extract-binding/single 'body bindings)
|
||||
(list)))
|
||||
(post (extract-binding/single 'title bindings)
|
||||
(extract-binding/single 'body bindings)
|
||||
(list)))
|
||||
|
||||
(define (insert-post-handler request)
|
||||
(blog-insert-post!
|
||||
BLOG (parse-post (request-bindings request)))
|
||||
(render-blog-page request))]
|
||||
|
||||
|
||||
(send/suspend/dispatch response-generator)))
|
||||
|
||||
;; render-post-detail-page: post request -> html-response
|
||||
|
@ -86,7 +86,7 @@
|
|||
,(make-url insert-comment-handler)))
|
||||
(input ((name "comment")))
|
||||
(input ((type "submit")))))))
|
||||
|
||||
|
||||
(define (parse-comment bindings)
|
||||
(extract-binding/single 'comment bindings))
|
||||
|
||||
|
@ -94,8 +94,8 @@
|
|||
(post-insert-comment!
|
||||
a-post (parse-comment (request-bindings a-request)))
|
||||
(render-post-detail-page a-post a-request))]
|
||||
|
||||
|
||||
|
||||
|
||||
(send/suspend/dispatch response-generator)))
|
||||
|
||||
|
||||
|
|
|
@ -2,23 +2,23 @@
|
|||
|
||||
;; A blog is a (make-blog posts)
|
||||
;; where posts is a (listof post)
|
||||
(define-struct blog (posts) #:mutable)
|
||||
(struct blog (posts) #:mutable)
|
||||
|
||||
;; and post is a (make-post title body comments)
|
||||
;; where title is a string, body is a string,
|
||||
;; and comments is a (listof string)
|
||||
(define-struct post (title body comments) #:mutable)
|
||||
(struct post (title body comments) #:mutable)
|
||||
|
||||
;; BLOG: blog
|
||||
;; The initial BLOG.
|
||||
(define BLOG
|
||||
(make-blog
|
||||
(list (make-post "First Post"
|
||||
"This is my first post"
|
||||
(list "First comment!"))
|
||||
(make-post "Second Post"
|
||||
"This is another post"
|
||||
(list)))))
|
||||
(blog
|
||||
(list (post "First Post"
|
||||
"This is my first post"
|
||||
(list "First comment!"))
|
||||
(post "Second Post"
|
||||
"This is another post"
|
||||
(list)))))
|
||||
|
||||
;; blog-insert-post!: blog post -> void
|
||||
;; Consumes a blog and a post, adds the post at the top of the blog.
|
||||
|
@ -52,22 +52,22 @@
|
|||
,(render-posts make-url)
|
||||
(form ((action
|
||||
,(make-url insert-post-handler)))
|
||||
(input ((name "title")))
|
||||
(input ((name "body")))
|
||||
(input ((type "submit")))))))
|
||||
(input ((name "title")))
|
||||
(input ((name "body")))
|
||||
(input ((type "submit")))))))
|
||||
|
||||
;; parse-post: bindings -> post
|
||||
;; Extracts a post out of the bindings.
|
||||
(define (parse-post bindings)
|
||||
(make-post (extract-binding/single 'title bindings)
|
||||
(extract-binding/single 'body bindings)
|
||||
(list)))
|
||||
(post (extract-binding/single 'title bindings)
|
||||
(extract-binding/single 'body bindings)
|
||||
(list)))
|
||||
|
||||
(define (insert-post-handler request)
|
||||
(blog-insert-post!
|
||||
BLOG (parse-post (request-bindings request)))
|
||||
(render-blog-page request))]
|
||||
|
||||
|
||||
(send/suspend/dispatch response-generator)))
|
||||
|
||||
;; render-post-detail-page: post request -> html-response
|
||||
|
@ -101,7 +101,7 @@
|
|||
|
||||
(define (back-handler request)
|
||||
(render-blog-page request))]
|
||||
|
||||
|
||||
(send/suspend/dispatch response-generator)))
|
||||
|
||||
;; render-confirm-add-comment-page :
|
||||
|
@ -130,7 +130,7 @@
|
|||
|
||||
(define (cancel-handler request)
|
||||
(render-post-detail-page a-post request))]
|
||||
|
||||
|
||||
(send/suspend/dispatch response-generator)))
|
||||
|
||||
;; render-post: post (handler -> string) -> html-response
|
||||
|
|
|
@ -2,23 +2,23 @@
|
|||
|
||||
;; A blog is a (make-blog posts)
|
||||
;; where posts is a (listof post)
|
||||
(define-struct blog (posts) #:mutable)
|
||||
(struct blog (posts) #:mutable)
|
||||
|
||||
;; and post is a (make-post title body comments)
|
||||
;; where title is a string, body is a string,
|
||||
;; and comments is a (listof string)
|
||||
(define-struct post (title body comments) #:mutable)
|
||||
(struct post (title body comments) #:mutable)
|
||||
|
||||
;; BLOG: blog
|
||||
;; The initial BLOG.
|
||||
(define BLOG
|
||||
(make-blog
|
||||
(list (make-post "First Post"
|
||||
"This is my first post"
|
||||
(list "First comment!"))
|
||||
(make-post "Second Post"
|
||||
"This is another post"
|
||||
(list)))))
|
||||
(blog
|
||||
(list (post "First Post"
|
||||
"This is my first post"
|
||||
(list "First comment!"))
|
||||
(post "Second Post"
|
||||
"This is another post"
|
||||
(list)))))
|
||||
|
||||
;; blog-insert-post!: blog post -> void
|
||||
;; Consumes a blog and a post, adds the post at the top of the blog.
|
||||
|
@ -52,22 +52,22 @@
|
|||
,(render-posts make-url)
|
||||
(form ((action
|
||||
,(make-url insert-post-handler)))
|
||||
(input ((name "title")))
|
||||
(input ((name "body")))
|
||||
(input ((type "submit")))))))
|
||||
(input ((name "title")))
|
||||
(input ((name "body")))
|
||||
(input ((type "submit")))))))
|
||||
|
||||
;; parse-post: bindings -> post
|
||||
;; Extracts a post out of the bindings.
|
||||
(define (parse-post bindings)
|
||||
(make-post (extract-binding/single 'title bindings)
|
||||
(extract-binding/single 'body bindings)
|
||||
(list)))
|
||||
(post (extract-binding/single 'title bindings)
|
||||
(extract-binding/single 'body bindings)
|
||||
(list)))
|
||||
|
||||
(define (insert-post-handler request)
|
||||
(blog-insert-post!
|
||||
BLOG (parse-post (request-bindings request)))
|
||||
(render-blog-page (redirect/get)))]
|
||||
|
||||
|
||||
(send/suspend/dispatch response-generator)))
|
||||
|
||||
;; render-post-detail-page: post request -> html-response
|
||||
|
@ -98,10 +98,10 @@
|
|||
(parse-comment (request-bindings request))
|
||||
a-post
|
||||
request))
|
||||
|
||||
|
||||
(define (back-handler request)
|
||||
(render-blog-page request))]
|
||||
|
||||
|
||||
(send/suspend/dispatch response-generator)))
|
||||
|
||||
;; render-confirm-add-comment-page :
|
||||
|
@ -130,7 +130,7 @@
|
|||
|
||||
(define (cancel-handler request)
|
||||
(render-post-detail-page a-post request))]
|
||||
|
||||
|
||||
(send/suspend/dispatch response-generator)))
|
||||
|
||||
;; render-post: post (handler -> string) -> html-response
|
||||
|
|
|
@ -19,22 +19,22 @@
|
|||
,(render-posts make-url)
|
||||
(form ((action
|
||||
,(make-url insert-post-handler)))
|
||||
(input ((name "title")))
|
||||
(input ((name "body")))
|
||||
(input ((type "submit")))))))
|
||||
(input ((name "title")))
|
||||
(input ((name "body")))
|
||||
(input ((type "submit")))))))
|
||||
|
||||
;; parse-post: bindings -> post
|
||||
;; Extracts a post out of the bindings.
|
||||
(define (parse-post bindings)
|
||||
(make-post (extract-binding/single 'title bindings)
|
||||
(extract-binding/single 'body bindings)
|
||||
(list)))
|
||||
(post (extract-binding/single 'title bindings)
|
||||
(extract-binding/single 'body bindings)
|
||||
(list)))
|
||||
|
||||
(define (insert-post-handler request)
|
||||
(blog-insert-post!
|
||||
BLOG (parse-post (request-bindings request)))
|
||||
(render-blog-page (redirect/get)))]
|
||||
|
||||
|
||||
(send/suspend/dispatch response-generator)))
|
||||
|
||||
;; render-post-detail-page: post request -> html-response
|
||||
|
@ -65,10 +65,10 @@
|
|||
(parse-comment (request-bindings request))
|
||||
a-post
|
||||
request))
|
||||
|
||||
|
||||
(define (back-handler request)
|
||||
(render-blog-page request))]
|
||||
|
||||
|
||||
(send/suspend/dispatch response-generator)))
|
||||
|
||||
;; render-confirm-add-comment-page :
|
||||
|
@ -97,7 +97,7 @@
|
|||
|
||||
(define (cancel-handler request)
|
||||
(render-post-detail-page a-post request))]
|
||||
|
||||
|
||||
(send/suspend/dispatch response-generator)))
|
||||
|
||||
;; render-post: post (handler -> string) -> html-response
|
||||
|
|
|
@ -2,25 +2,25 @@
|
|||
|
||||
;; A blog is a (make-blog home posts)
|
||||
;; where home is a string, posts is a (listof post)
|
||||
(define-struct blog (home posts) #:mutable #:prefab)
|
||||
(struct blog (home posts) #:mutable #:prefab)
|
||||
|
||||
;; and post is a (make-post blog title body comments)
|
||||
;; where title is a string, body is a string,
|
||||
;; and comments is a (listof string)
|
||||
(define-struct post (title body comments) #:mutable #:prefab)
|
||||
(struct post (title body comments) #:mutable #:prefab)
|
||||
|
||||
;; initialize-blog! : path? -> blog
|
||||
;; Reads a blog from a path, if not present, returns default
|
||||
(define (initialize-blog! home)
|
||||
(local [(define (log-missing-exn-handler exn)
|
||||
(make-blog
|
||||
(blog
|
||||
(path->string home)
|
||||
(list (make-post "First Post"
|
||||
"This is my first post"
|
||||
(list "First comment!"))
|
||||
(make-post "Second Post"
|
||||
"This is another post"
|
||||
(list)))))
|
||||
(list (post "First Post"
|
||||
"This is my first post"
|
||||
(list "First comment!"))
|
||||
(post "Second Post"
|
||||
"This is another post"
|
||||
(list)))))
|
||||
(define the-blog
|
||||
(with-handlers ([exn? log-missing-exn-handler])
|
||||
(with-input-from-file home read)))]
|
||||
|
@ -41,7 +41,7 @@
|
|||
(define (blog-insert-post! a-blog title body)
|
||||
(set-blog-posts!
|
||||
a-blog
|
||||
(cons (make-post title body empty) (blog-posts a-blog)))
|
||||
(cons (post title body empty) (blog-posts a-blog)))
|
||||
(save-blog! a-blog))
|
||||
|
||||
;; post-insert-comment!: blog post string -> void
|
||||
|
|
|
@ -3,17 +3,17 @@
|
|||
|
||||
;; A blog is a (make-blog db)
|
||||
;; where db is an sqlite database handle
|
||||
(define-struct blog (db))
|
||||
(struct blog (db))
|
||||
|
||||
;; A post is a (make-post blog id)
|
||||
;; where blog is a blog and id is an integer?
|
||||
(define-struct post (blog id))
|
||||
(struct post (blog id))
|
||||
|
||||
;; initialize-blog! : path? -> blog?
|
||||
;; Sets up a blog database (if it doesn't exist)
|
||||
(define (initialize-blog! home)
|
||||
(define db (sqlite:open home))
|
||||
(define the-blog (make-blog db))
|
||||
(define the-blog (blog db))
|
||||
(with-handlers ([exn? void])
|
||||
(sqlite:exec/ignore db
|
||||
(string-append
|
||||
|
@ -35,7 +35,7 @@
|
|||
;; Queries for the post ids
|
||||
(define (blog-posts a-blog)
|
||||
(local [(define (row->post a-row)
|
||||
(make-post
|
||||
(post
|
||||
a-blog
|
||||
(vector-ref a-row 0)))
|
||||
(define rows (sqlite:select
|
||||
|
|
|
@ -2,23 +2,23 @@
|
|||
|
||||
;; A blog is a (make-blog posts)
|
||||
;; where posts is a (listof post)
|
||||
(define-struct blog (posts) #:mutable)
|
||||
(struct blog (posts) #:mutable)
|
||||
|
||||
;; and post is a (make-post title body comments)
|
||||
;; where title is a string, body is a string,
|
||||
;; and comments is a (listof string)
|
||||
(define-struct post (title body comments) #:mutable)
|
||||
(struct post (title body comments) #:mutable)
|
||||
|
||||
;; BLOG: blog
|
||||
;; The initial BLOG.
|
||||
(define BLOG
|
||||
(make-blog
|
||||
(list (make-post "First Post"
|
||||
"This is my first post"
|
||||
(list "First comment!"))
|
||||
(make-post "Second Post"
|
||||
"This is another post"
|
||||
(list)))))
|
||||
(blog
|
||||
(list (post "First Post"
|
||||
"This is my first post"
|
||||
(list "First comment!"))
|
||||
(post "Second Post"
|
||||
"This is another post"
|
||||
(list)))))
|
||||
|
||||
;; blog-insert-post!: blog post -> void
|
||||
;; Consumes a blog and a post, adds the post at the top of the blog.
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
;; A roster is a (make-roster names)
|
||||
;; where names is a list of string.
|
||||
(define-struct roster (names) #:mutable)
|
||||
(struct roster (names) #:mutable)
|
||||
|
||||
;; roster-add-name!: roster string -> void
|
||||
;; Given a roster and a name, adds the name
|
||||
|
@ -12,7 +12,7 @@
|
|||
(append (roster-names a-roster)
|
||||
(list a-name))))
|
||||
|
||||
(define ROSTER (make-roster '("kathi" "shriram" "dan")))
|
||||
(define ROSTER (roster '("kathi" "shriram" "dan")))
|
||||
|
||||
;; start: request -> html-response
|
||||
(define (start request)
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
;; A roster is a (make-roster names)
|
||||
;; where names is a list of string.
|
||||
(define-struct roster (names) #:mutable)
|
||||
(struct roster (names) #:mutable)
|
||||
|
||||
;; roster-add-name!: roster string -> void
|
||||
;; Given a roster and a name, adds the name
|
||||
|
@ -12,7 +12,7 @@
|
|||
(append (roster-names a-roster)
|
||||
(list a-name))))
|
||||
|
||||
(define ROSTER (make-roster '("kathi" "shriram" "dan")))
|
||||
(define ROSTER (roster '("kathi" "shriram" "dan")))
|
||||
|
||||
;; start: request -> html-response
|
||||
(define (start request)
|
||||
|
|
Loading…
Reference in New Issue
Block a user