在Clojure doseq函数中间暂停



我的程序允许用户选择要导入数据库的文件。在此之前,它只允许他们一次导入一个文件。让他们一次导入多个文件很容易。但我遇到的问题是,如果数据库中已经包含具有相同标题的页面,我希望在用要导入的版本覆盖数据库中的版本之前显示警告并得到确认。

这是我迄今为止所拥有的。它在很大程度上遵循了我导入单个文件的做法。

;; Handler for the POST "/import" route.
(defn- post-import-page
"Import the file(s) specified in the upload dialog. Checks the page
title of the import file against existing pages in the database. If
a page of the same name already exists, asks for confirmation before
importing."
[{{file-info "file-info"
referer   "referer"} :multipart-params :as req}]
(let [file-vec (if (map? file-info)
(conj [] file-info)
file-info)]
(doseq [fm file-vec]
(let [file-name (:filename fm)
import-map (files/load-markdown-from-file (:tempfile fm))
page-title (get-in import-map [:meta :title])
id-exists? (db/title->page-id page-title)]
(if id-exists?
(build-response
(layout/compose-import-existing-page-warning
import-map file-name referer) req)
(do-the-import import-map file-name req))))))

此函数导入数据库中不存在的任何文件,但不会导入任何会覆盖具有相同标题的现有数据库条目的文件。它也从未显示要求确认的警告页面。

警告页面构造如下:

(defn compose-import-existing-page-warning
"Return a page stating that a page with the same title already exists 
in the wiki. Ask the user to proceed or cancel the import."
[import-map file-name referer]
(short-form-template
[:div {:class "cwiki-form"}
(form-to {:enctype      "multipart/form-data"
:autocomplete "off"}
[:post "proceed-with-import"]
(hidden-field "import-map" import-map)
(hidden-field "file-name" file-name)
(hidden-field "referer" referer)
[:p {:class "form-title"} "Page Already Exists"]
[:div {:class "form-group"}
[:p (str "A page with the title "" (get-in import-map [:meta :title])
"" already exists in the wiki.")]
[:p (str "Click "Proceed" to delete the existing page and "
"replace it with the contents of the imported file.")]
[:div {:class "button-bar-container"}
(submit-button {:id    "proceed-with-import-button"
:class "form-button button-bar-item"}
"Proceed")
[:input {:type      "button" :name "cancel-button"
:value     "Cancel"
:class     "form-button button-bar-item"
:autofocus "autofocus"
:onclick   "window.history.back();"}]]])]))

程序如何在doseq(或其他循环功能(中间暂停以显示确认页面并等待用户进行选择?

只需在循环中间使用read-line,然后使用if来选择所需的分支。以下是您可能会发现有用的其他文档列表,尤其是Clojure CheatSheet。

最新更新