Maintaing Database in Clojure for New registered User


Clojure is the most popular Lisp dialect today for starting new development.

The Clojure web stack is based on the Ring and Compojure libraries. Ring is the base HTTP library, while Compojure provides routing on top of it. Clojure makes a number of powerful libraries available, and we can put these libraries together in a way that makes sense for our particular project.

The below mentioned steps would help you in maintaining a database using Luminus Framework for your web development project:

Design the html page for registration using Selmer Clojure library under views/template clojure directory structure.

form action="{{servlet-context}}/register" method="POST">
<label for="id">
<input id="id" name="id" placeholder="User Name"  type="text" value={{id}}></input>
</label>
<label for="email">
<input id="email" name="email" placeholder="Email"  type="text" value={{email}}></input>
</label>
<label for="pass">
<input id="pass" name="pass" placeholder="Password"  type="password" value={{pass}}></input>
</label>
<label for="confirm-pass">
<input id="confirm-pass" name="confirm-pass" placeholder="Confirm Password"  type="password" value={{confirm-pass}}></input>
</label>
<button type="submit" >Submit</button>
</form>

Code for rendering “registration.html” page and logic for for creating a new user and maintaining a new session

(defn register [& [id]]
(layout/render
"registration.html" )
(defn handle-registration [id email pass confirm-pass]
(do
(db/create-user {:id id :email email :pass (crypt/encrypt pass)})
(session/put! :user-id id)
(resp/redirect "/"))
(register id email))

(defn logout []
(session/clear!)
(resp/redirect "/"))
(defroutes authentication-routes(POST "/register" [id email pass confirm-pass]
(handle-registration id email pass confirm-pass))

Creating a new database connection.

(db.clj)

(defentity users)
(defn create-user [user]
(insert users
(values user))

 Create Schema for the database table.

(schema.clj);


(defn create-users-table
[]
(sql/with-connection db-spec
(sql/create-table
:users
[:id "varchar(50) PRIMARY KEY"]
[:first_name "varchar(30)"]
[:last_name "varchar(30)"]
[:email "varchar(50)"]
[:admin :boolean "DEFAULT FALSE"]
[:pass "varchar(100)"])))

(defn create-tables
"creates the database tables used by the application"
[]
(create-users-table))



 

Advertisements
This entry was posted in Clojure. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s