(ns wanijo.user.db
  (:require [clojure.spec.alpha :as spec]
            [wanijo.infrastructure.neo4j :as neo4j]))

(spec/def ::ident
  (spec/and string? not-empty))
(spec/def ::name
  (spec/or :empty #(or (empty? %) (nil? %))
           :given (spec/and string? some?)))
(spec/def ::password
  (spec/and string? #(> 7 (count %))))

(neo4j/defquery
  find-user
  "MATCH (n:user)
   WHERE n.ident = {ident}
   RETURN n")

(defn find! [ident]
  (->>
   (neo4j/exec-query!
    find-user
    {:ident ident})
   first
   :n))

(neo4j/defquery
  all
  "MATCH (u:user)
   RETURN u
   ORDER BY u.ident")

(defn all! []
  (map :u
       (neo4j/exec-query!
        all
        {})))
