src/newseasons/models/users.clj @ dc8ba07e8157
Jesus. Christ.
author |
Steve Losh <steve@stevelosh.com> |
date |
Wed, 28 Sep 2011 23:40:36 -0400 |
parents |
ead78c7e9a4d |
children |
e22577b16eb5 |
(ns newseasons.models.users
(:require [noir.util.crypt :as crypt])
(:use [aleph.redis :only (redis-client)]))
(def r (redis-client {:host "localhost" :password "devpass"}))
; "Schema" --------------------------------------------------------------------
;
; Users are stored as Redis hashes, with their watched shows as a separate set.
;
; user:<email address> = {
; email: the email address for ease of use
; pass: the user's hashed password
; }
; user:<email address>:shows = #(show-id, ...)
; Code ------------------------------------------------------------------------
(defn- user-key [email]
(str "users:" email))
(defn- user-key-shows [email]
(str "users:" email ":shows"))
(defn user-get [email]
(let [user (apply hash-map @(r [:hgetall (user-key email)]))]
(when (not (empty? user))
(merge {:email (user "email") :pass (user "pass")}
{:shows @(r [:smembers (user-key-shows email)])}))))
(defn user-set-email! [email new-email]
@(r [:hset (user-key email) "email" new-email]))
(defn user-set-pass! [email new-pass]
@(r [:hset (user-key email) "pass" (crypt/encrypt new-pass)]))
(defn user-add-show! [email show-id]
@(r [:sadd (user-key-shows email) show-id]))
(defn user-rem-show! [email show-id]
@(r [:srem (user-key-shows email) show-id]))