Skip to content

Instantly share code, notes, and snippets.

@adamfowleruk
Created November 1, 2012 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamfowleruk/3995365 to your computer and use it in GitHub Desktop.
Save adamfowleruk/3995365 to your computer and use it in GitHub Desktop.
article controller main function
declare function c:main() as item()*
{
let $acount := xdmp:estimate((fn:doc()/article))
return
(
ch:add-value("articlecount",$acount),
ch:add-value("title", "Articles")
)
};
xquery version "1.0-ml";
import module namespace vh = "http://marklogic.com/roxy/view-helper"
at "/roxy/lib/view-helper.xqy";
declare option xdmp:mapping "false";
<div xmlns="http://www.w3.org/1999/xhtml" class="articles main">
<h2>{fn:string(vh:get("title"))}</h2>
<p>There are currently {fn:string(vh:get("articlecount"))}
articles in the database.
</p>
<form action="/articles/doupload.html" method="POST"
enctype="multipart/form-data">
<h2>Upload article XML</h2>
<label for="upload">File: </label>
<input type="file" id="upload" name="upload" size="50"/>
<br/>
<input type="submit" value="Upload"/>
</form>
</div>
(: Must be invoked with a POST in order to be allowed to add a doc :)
declare function c:doupload() as item()*
{
(: 1. upload doc :)
let $file := xdmp:get-request-field("upload")
(: Now get XML described doc from our file upload :)
let $xml := xdmp:unquote($file,"","repair-full")
let $uri := fn:concat("/articles/",xdmp:random(),".xml")
let $docresult :=
xdmp:document-insert($uri,$xml,xdmp:default-permissions(),
(xdmp:default-collections(),"articles")
)
return
xdmp:redirect-response("/articles")
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment