                     WHERE b.status = 'accepted' AND b.deleted = 0 AND b.revoked = 0 AND (b.expires_on IS NULL OR b.expires_on > UNIX_TIMESTAMP())"
                    where
                    " GROUP BY bc.id, bc.name, bc.image_file, bc.description, ic.name, ic.url, b.badge_content_id
-                    ORDER BY ctime DESC
-                    LIMIT 100")
+                    ORDER BY ctime DESC")
         badgesearch (jdbc/with-db-connection
                   [conn (:connection (get-db ctx))]
                   (jdbc/query conn (into [query] params)))

        assochelper (fn [user recipients] (assoc user  :recipients (get recipientsmap (:badge_content_id user))))]
    (map assochelper badgesearch recipients)))

(defn user-country
  "Return user's country id"
  [ctx user-id]

          :db (hikari-cp.core/make-datasource {:adapter "mysql",
                                               :username "root",
                                               :password "isokala",
                                               :database-name "salavareal",
                                               :server-name "localhost"})})





(def hakujenhaku
  "select distinct  bc.name, ic.name AS issuer_content_name, count(distinct b.id) as owners, MAX(b.ctime) AS ctime
FROM badge_content as bc 
INNER JOIN badge as b on bc.id = b.badge_content_id AND  b.status = 'accepted' AND b.deleted = 0 AND b.revoked = 0 AND (b.expires_on IS NULL OR b.expires_on > UNIX_TIMESTAMP())
INNER JOIN issuer_content AS ic ON b.issuer_content_id = ic.id 
group by bc.id
ORDER BY owners desc")

(defn get-badge-adverts [ctx country tags badge-name issuer-name order id recipient-name]
  (let [where-params (badge-adverts-where-params country badge-name issuer-name recipient-name)
        where  (apply str (keys where-params))
        params  (vec (vals where-params))  ;add user-id to params 
        ;tags (vec (vals tags))
        order (cond
                ;(= order "mtime") "ORDER BY ba.mtime DESC"
                (= order "name") "ORDER BY bc.name"
                (= order "issuer_content_name") "ORDER BY ic.name"
                (= order "owners") "ORDER BY owners DESC"
                :else "ORDER BY ctime DESC") 
        query (str
               "select bc.id, bc.name, ic.name AS issuer_content_name, count(distinct b.id) as owners, MAX(b.ctime) AS ctime
FROM badge_content as bc 
INNER JOIN badge as b on bc.id = b.badge_content_id AND  b.status = 'accepted' AND b.deleted = 0 AND b.revoked = 0 AND (b.expires_on IS NULL OR b.expires_on > UNIX_TIMESTAMP())
INNER JOIN issuer_content AS ic ON b.issuer_content_id = ic.id
INNER JOIN user as u on b.user_id =  u.id
WHERE bc.id IN
	(SELECT DISTINCT badge_content_id FROM badge WHERE visibility != 'private' AND  status = 'accepted' AND deleted = 0 AND revoked = 0 AND (expires_on IS NULL OR expires_on > UNIX_TIMESTAMP())) 
 "

                 

                  where
                   
                   "GROUP BY bc.id "
                   order
                   )
        search (jdbc/with-db-connection
                 [conn (:connection (get-db ctx))]
                 (jdbc/query conn (into [query] params)))]
    (dump where)
    search
    #_(if (not-empty tags)
      (filter-tags search tags)
      search)))


(time  (count (get-badge-adverts ctx nil nil nil nil "owners" nil nil) ))

(:name (first (get-badge-adverts ctx nil nil nil nil "mtime" nil nil nil nil)))

(defn public-badges-by-user
