Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Define get-record in terms of find-record.

  • Loading branch information...
commit 669e85d19a9facb05376d3209a8abf21cbae18c7 1 parent 185165d
@duelinmarkers authored
Showing with 13 additions and 15 deletions.
  1. +13 −15 src/clj_record/core.clj
View
28 src/clj_record/core.clj
@@ -64,21 +64,6 @@
(sql/with-query-results rows [(id-query-for (db-spec-for model-name) (table-name model-name))]
(val (first (first rows))))))
-(defn get-record
- "Retrieves record by id, throwing if not found."
- [model-name id]
- (connected (db-spec-for model-name)
- (sql/with-query-results rows [(format "select * from %s where id = ?" (table-name model-name)) id]
- (if (empty? rows) (throw (IllegalArgumentException. "Record does not exist")))
- (run-callbacks (merge {} (first rows)) model-name :after-load))))
-
-(defn create
- "Inserts a record populated with attributes and returns it."
- [model-name attributes]
- (let [id (insert model-name attributes)]
- (connected (db-spec-for model-name)
- (get-record model-name id))))
-
(defn find-by-sql
"Returns a vector of matching records.
select-query-and-values should be something like
@@ -106,6 +91,19 @@
[model-name attributes-or-where-params]
(first (find-records model-name attributes-or-where-params)))
+(defn get-record
+ "Retrieves record by id, throwing if not found."
+ [model-name id]
+ (or (find-record model-name {:id id})
+ (throw (IllegalArgumentException. "Record does not exist"))))
+
+(defn create
+ "Inserts a record populated with attributes and returns it."
+ [model-name attributes]
+ (let [id (insert model-name attributes)]
+ (connected (db-spec-for model-name)
+ (get-record model-name id))))
+
(defn update
"Updates by (partial-record :id), updating only those columns included in partial-record."
[model-name partial-record]
Please sign in to comment.
Something went wrong with that request. Please try again.