Luke Plant avatar Luke Plant committed 632bc9c

Implemented 'addEntryView'

Comments (0)

Files changed (1)

src/ConfirmCgi.hs

 import Control.Exception (catchDyn)
 import Database.HDBC (quickQuery, toSql, SqlError, commit)
 import Database.HDBC.Sqlite3 (connectSqlite3)
-
+import Maybe (isNothing)
 import Random (randomRs, newStdGen)
 
 -- Settings
 
 updateStatusStmnt = "UPDATE addresses SET send_email = ? WHERE id = ?;"
 queryByIdStmnt  = "SELECT email FROM addresses WHERE id = ?;"
+insertEntryStmnt = "INSERT INTO addresses (name, email, id) VALUES (?, ?, ?);"
 
 update :: Bool -> String -> IO Bool
 update addthem personid = do
   vals <- quickQuery conn queryByIdStmnt [toSql personid]
   return (length vals == 1)
 
+addEntry name email = do
+  conn <- connect
+  newid <- randomStr 10
+  quickQuery conn insertEntryStmnt [toSql name, toSql email, toSql newid]
+  commit conn
+
 -- Error handling
 
 sqlErrorHandler = \e -> do
                                    addContent content] utf8HtmlResponse
 accessDenied = forbidden "Access denied\n"
 
+invalidInput content = buildResponse [ setStatus 400
+                                     , addContent content] utf8HtmlResponse
+
 -- -- Decorators
 
 -- | Decorator that enforces a POST parameter 'password' to be present
 -- -- Admin URLs
 
 addEntryView req = do
-  return $ Just $ message "Not implemented!"
+  let name  = getPOST "name" req
+      email = getPOST "email" req
+  if any isNothing [name, email]
+     then return $ Just $ invalidInput "Please provide 'name' and 'email' parameters\n"
+     else do
+       addEntry name email
+       return $ Just $ message "Added!\n"
 
 -- Utilities
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.