← urwebplay

Comments

f63b1680cb18dcabc7fd04949264161179e458f5 · ovidiucs · Dec 28, 2015 12:20

+131 −19 · 4 files changed

  • urblog.sql +11 −8
  • urblog.ur +17 −11
  • urblog.urp +1 −0
  • urbloga.ur +102 −0
diff --git a/urblog.sql b/urblog.sql
index 849fd6f..e0e0560 100644
--- a/urblog.sql
+++ b/urblog.sql
@@ -5,12 +5,15 @@ CREATE TABLE uw_Urblog_entry(uw_id int8 NOT NULL, uw_title text NOT NULL,
   
  );
  
- CREATE TABLE uw_Urblog_comment(uw_id int8 NOT NULL, uw_entry int8 NOT NULL, 
-                                 uw_created timestamp NOT NULL, 
-                                 uw_author text NOT NULL, uw_body text NOT NULL,
-  PRIMARY KEY (uw_id),
-   CONSTRAINT uw_Urblog_comment_Entry
-    FOREIGN KEY (uw_entry) REFERENCES uw_Urblog_entry (uw_id)
-  );
+ CREATE SEQUENCE uw_Urblog_commentS;
   
-  
\ No newline at end of file
+  CREATE TABLE uw_Urblog_comment(uw_id int8 NOT NULL, uw_entry int8 NOT NULL, 
+                                  uw_created timestamp NOT NULL, 
+                                  uw_author text NOT NULL, 
+                                  uw_body text NOT NULL,
+   PRIMARY KEY (uw_id),
+    CONSTRAINT uw_Urblog_comment_Entry
+     FOREIGN KEY (uw_entry) REFERENCES uw_Urblog_entry (uw_id)
+   );
+   
+   
\ No newline at end of file
diff --git a/urblog.ur b/urblog.ur
index 5f6064d..bd00a35 100644
--- a/urblog.ur
+++ b/urblog.ur
@@ -1,6 +1,7 @@
 table entry : { Id : int, Title : string, Created : time, Author : string, Body : string }
   PRIMARY KEY Id
 
+sequence commentS
 table comment : {Id : int, Entry : int, Created : time, Author : string, Body : string }
 	PRIMARY KEY Id,
 	CONSTRAINT Entry FOREIGN KEY Entry REFERENCES entry(Id)
@@ -18,16 +19,21 @@ fun comments i : transaction xbody =
 					<p>By {[row.Comment.Author]} at {[row.Comment.Created]}</p>
 				</div>
 			</xml>)
-and handler entry r = detail entry 
+
+and handler entry r = 
+    id <- nextval commentS;
+        dml (INSERT INTO comment (Id, Entry, Author, Body, Created) VALUES ({[id]}, {[entry]}, {[r.Author]}, {[r.Body]}, CURRENT_TIMESTAMP));
+    detail entry
+
 and mkCommentForm (id:int) s : xbody =
     <xml><form>
-        <p>Your Name:<br/></p>"<textbox{#Author}/><br/>
+        <p>Your Name:<br/></p><textbox{#Author}/><br/>
         <p>Your Comment:<br/></p><textarea{#Body}/>
         <br/><br/>
         <submit value="Add Comment" action={handler id}/>
-        <button value="Cancel" onclick ={ fn _ => set s False}/></form></xml>
+        <button value="Cancel" onclick={ fn _ => set s False}/></form></xml>
 
-fun list () =
+and list () =
     rows <- queryX (SELECT * FROM entry)
             (fn row => 
 				<xml>
@@ -42,7 +48,7 @@ fun list () =
 	 	<xml>
 		  <head>
 				<title>All Entries</title>
-            <link rel="stylesheet" type="text/css" href="http://expdev.net/urtutorial/step3/style.css"/>
+            <link rel="stylesheet" type="text/css" href="http://expdev.net/urtutorial/step5/style.css"/>
 		  </head>
 		  <body>
 			<h1>All Entries</h1>
@@ -64,25 +70,25 @@ and detail (i:int) =
       | Some r => <xml>
                    <head>
                      <title>{[r.Entry.Title]}</title>
-                     <link rel="stylesheet" type="text/css" href="http://expdev.net/urtutorial/step4/style.css"/>
+                     <link rel="stylesheet" type="text/css" href="http://expdev.net/urtutorial/step5/style.css"/>
 				   </head>
 				   <body>
 				   <h1>{[r.Entry.Title]}</h1>
                    <h2>By {[r.Entry.Author]} at {[r.Entry.Created]}</h2>
                    <div class={blogEntry}>
                    <p>{[r.Entry.Body]}</p>
-				   <button value="Add Comment" onclick={ fn _ => set commentSource True }/>
+				   <button value="Add Comment" onclick={ fn _ => set commentSource True}/>
                    </div>
 				   <div class={commentForm}>
 				   <dyn signal={s <- signal commentSource;
-			if s them
-							return {mkCommentForm i commentSource}
-			else return <xml/>}/>
+                         if s then 
+                                    return (mkCommentForm i commentSource) 
+                          else return <xml/>}/>
 				   </div>
 				   {comm}
 				   <a link={list ()}>Back to all entries</a>
                    </body>
-                 </xml>)	
+                 </xml>)
 fun main () = return <xml>
   <head>
     <title>UrBlog</title>
diff --git a/urblog.urp b/urblog.urp
index f53dbfa..d18b2e9 100644
--- a/urblog.urp
+++ b/urblog.urp
@@ -1,5 +1,6 @@
 allow url http://expdev.net/urtutorial/step3/style.css
 allow url http://expdev.net/urtutorial/step4/style.css
+allow url http://expdev.net/urtutorial/step5/style.css
 rewrite style Urblog/blogEntry blogEntry
 rewrite style Urblog/blogComment blogComment
 database dbname=urblog
diff --git a/urbloga.ur b/urbloga.ur
new file mode 100644
index 0000000..5cb0534
--- /dev/null
+++ b/urbloga.ur
@@ -0,0 +1,102 @@
+table entry : { Id : int, Title : string, Created : time, Author : string, Body : string }
+  PRIMARY KEY Id
+
+sequence commentS
+table comment : {Id : int, Entry : int, Created : time, Author : string, Body : string }
+	PRIMARY KEY Id,
+	CONSTRAINT Entry FOREIGN KEY Entry REFERENCES entry(Id)
+
+style blogEntry
+style blogComment
+style commentForm
+
+fun comments i : transaction xbody =
+	queryX (SELECT * FROM comment WHERE comment.Entry = {[i]})
+		(fn row =>
+			<xml>
+				<div class={blogComment}>
+					<p>{[row.Comment.Body]}</p>
+					<p>By {[row.Comment.Author]} at {[row.Comment.Created]}</p>
+				</div>
+			</xml>)
+
+and handler entry r = 
+	    id <- nextval commentS;
+    		dml (INSERT INTO comment (Id, Entry, Author, Body, Created) VALUES ({[id]}, {[entry]}, {[r.Author]}, {[r.Body]}, CURRENT_TIMESTAMP));
+		detail entry
+
+and mkCommentForm (id:int) s : xbody =
+	<xml><form>
+		<p>Your Name:<br/></p><textbox{#Author}/><br/>
+		<p>Your Comment:<br/></p><textarea{#Body}/>
+		<br/><br/>
+      <submit value="Add Comment" action={handler id}/>
+		<button value="Cancel" onclick={ fn _ => set s False}/></form></xml>
+
+and list () =
+    rows <- queryX (SELECT * FROM entry)
+            (fn row => 
+				<xml>
+					<div class={blogEntry}>
+						<h1><a link={detail row.Entry.Id}>{[row.Entry.Title]}</a></h1>
+							<h2>By {[row.Entry.Author]} at {[row.Entry.Created]}</h2>
+						<p>{[row.Entry.Body]}</p>
+					</div>
+				</xml>
+            );
+    return 
+	 	<xml>
+		  <head>
+				<title>All Entries</title>
+				<link rel="stylesheet" type="text/css" href="http://expdev.net/urtutorial/step5/style.css"/>
+		  </head>
+		  <body>
+			<h1>All Entries</h1>
+        {rows}
+		  </body>
+    	</xml>
+
+and detail (i:int) =
+	res <- oneOrNoRows (SELECT * FROM entry WHERE entry.Id = {[i]});
+	comm <- comments i;
+	commentSource <- source False;
+	return
+	(case res of
+		None => <xml>
+					<head><title>Entry Not Found</title></head>
+					<body>
+						<h1>Entry not found</h1>
+					</body>
+				  </xml>
+	 | Some r => <xml>
+						<head>
+							<title>{[r.Entry.Title]}</title>
+							<link rel="stylesheet" type="text/css" href="http://expdev.net/urtutorial/step5/style.css"/>
+						</head>
+						<body>
+						<h1>{[r.Entry.Title]}</h1>
+						<h2>By {[r.Entry.Author]} at {[r.Entry.Created]}</h2>
+						<div class={blogEntry}>
+						<p>{[r.Entry.Body]}</p>
+						<button value="Add Comment" onclick={ fn _ => set commentSource True}/>
+						</div>
+						<div class={commentForm}>
+						<dyn signal={s <- signal commentSource;
+                         if s then 
+								 	return (mkCommentForm i commentSource) 
+								 else return <xml/>}/>
+						</div>
+						{comm}
+						<a link={list ()}>Back to all entries</a>
+						</body>
+					 </xml>)
+
+fun main () = return <xml>
+  <head>
+    <title>UrBlog</title>
+  </head>
+
+  <body>
+    <h1>UrBlog</h1>
+  </body>
+</xml>