forked from seiichiusa/ml-start
-
Notifications
You must be signed in to change notification settings - Fork 1
/
add-book.xqy
111 lines (103 loc) · 3.59 KB
/
add-book.xqy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
xquery version "1.0-ml";
declare option xdmp:output "method = html";
declare function local:saveBook(
$title as xs:string,
$author as xs:string?,
$year as xs:string?,
$price as xs:string?,
$category as xs:string?
) as xs:string {
let $id as xs:string := local:generateID()
let $book as element(book) :=
element book {
attribute category { $category },
attribute id { $id },
element title { $title },
element author { $author },
element year { $year },
element price { $price }
}
let $uri := '/bookstore/book-' || $id || '.xml'
let $save := xdmp:document-insert($uri, $book)
return
$id
};
declare function local:generateID(
) as xs:string {
let $hash :=
xs:string(
xdmp:hash64(
fn:concat(
xs:string(xdmp:host()),
xs:string(fn:current-dateTime()),
xs:string(xdmp:random())
)
)
)
return
local:padString($hash, 20, fn:false())
};
declare function local:padString(
$string as xs:string,
$length as xs:integer,
$padLeft as xs:boolean
) as xs:string {
if (fn:string-length($string) = $length) then (
$string
) else if (fn:string-length($string) < $length) then (
if ($padLeft) then (
local:padString(fn:concat("0", $string), $length, $padLeft)
) else (
local:padString(fn:concat($string, "0"), $length, $padLeft)
)
) else (
fn:substring($string, 1, $length)
)
};
declare function local:sanitizeInput($chars as xs:string?) {
fn:replace($chars,"[\]\[<>{}\\();%\+]","")
};
declare variable $id as xs:string? :=
if (xdmp:get-request-method() eq "POST") then (
let $title as xs:string? := local:sanitizeInput(xdmp:get-request-field("title"))
let $author as xs:string? := local:sanitizeInput(xdmp:get-request-field("author"))
let $year as xs:string? := local:sanitizeInput(xdmp:get-request-field("year"))
let $price as xs:string? := local:sanitizeInput(xdmp:get-request-field("price"))
let $category as xs:string? := local:sanitizeInput(xdmp:get-request-field("category"))
return
local:saveBook($title, $author, $year, $price, $category)
) else ();
(: build the html :)
xdmp:set-response-content-type("text/html"),
'<!DOCTYPE html>',
<html>
<head>
<title>Add Book</title>
</head>
<body>
{
if (fn:exists($id) and $id ne '') then (
<div class="message">Book Saved! ({$id})</div>
) else ()
}
<form name="add-book" action="add-book.xqy" method="post">
<fieldset>
<legend>Add Book</legend>
<label for="title">Title</label> <input type="text" id="title" name="title"/>
<label for="author">Author</label> <input type="text" id="author" name="author"/>
<label for="year">Year</label> <input type="text" id="year" name="year"/>
<label for="price">Price</label> <input type="text" id="price" name="price"/>
<label for="category">Category</label>
<select name="category" id="category">
<option/>
{
for $c in ('CHILDREN','FICTION','NON-FICTION')
return
<option value="{$c}">{$c}</option>
}
</select>
<input type="submit" value="Save"/>
</fieldset>
</form>
</body>
</html>