Skip to content

Commit fe9c49e

Browse files
committed
refactored doc etag stuff
1 parent e57d917 commit fe9c49e

File tree

1 file changed

+56
-80
lines changed

1 file changed

+56
-80
lines changed

src/main/com/yetanalytics/lrs/pedestal/routes/documents.cljc

Lines changed: 56 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -274,94 +274,70 @@
274274
request
275275
com.yetanalytics/lrs]
276276
:as ctx}]
277-
(if (p/document-resource-async? lrs)
278-
;; Async
279-
(a/go
280-
(try
281-
(let [{:keys [headers]} request
282-
doc (->doc request)]
283-
(if-let [[_ params]
284-
(find xapi :xapi.activities.state.PUT.request/params)]
285-
(let [{hif-match "if-match"
286-
hif-none-match "if-none-match"}
287-
headers]
288-
;; State document
289-
(if (or hif-match hif-none-match)
277+
(let [[_ params]
278+
(or (find xapi :xapi.activities.state.PUT.request/params)
279+
(find-some xapi
280+
:xapi.activities.profile.PUT.request/params
281+
:xapi.agents.profile.PUT.request/params))
282+
{:keys [headers]} request
283+
{hif-match "if-match"
284+
hif-none-match "if-none-match"} headers
285+
doc (->doc request)]
286+
(if (p/document-resource-async? lrs)
287+
;; Async
288+
(a/go
289+
(try
290+
(if (or hif-match hif-none-match)
291+
;; has been checked by preproc, OK to proceed
292+
(put-response ctx
293+
(a/<! (lrs/set-document-async
294+
lrs
295+
auth-identity
296+
params
297+
doc
298+
false)))
299+
;; no headers, so we check to see if a doc exists
300+
(let [doc-res (a/<! (lrs/get-document-async
301+
lrs
302+
auth-identity params))]
303+
(if (and (not (:error doc-res))
304+
(nil? (:document doc-res)))
290305
(put-response ctx
291306
(a/<! (lrs/set-document-async
292307
lrs
293308
auth-identity
294309
params
295310
doc
296311
false)))
297-
(let [err-res (a/<! (lrs/get-document-async
298-
lrs
299-
auth-identity params))]
300-
(put-err-response ctx err-res))))
301-
;; Activity/Agent profile document
302-
(let [[_params-spec params]
303-
(find-some xapi
304-
:xapi.activities.profile.PUT.request/params
305-
:xapi.agents.profile.PUT.request/params)
306-
{hif-match "if-match"
307-
hif-none-match "if-none-match"}
308-
headers]
309-
(if (or hif-match hif-none-match)
310-
(put-response ctx (a/<! (lrs/set-document-async
311-
lrs
312-
auth-identity
313-
params
314-
doc
315-
false)))
316-
;; if neither header is present
317-
(let [err-res (a/<! (lrs/get-document-async
318-
lrs
319-
auth-identity params))]
320-
(put-err-response ctx err-res))))))
321-
(catch #?(:clj Exception :cljs js/Error) ex
322-
(assoc ctx :io.pedestal.interceptor.chain/error ex))))
323-
;; Sync
324-
(try
325-
(let [{:keys [headers]} request
326-
doc (->doc request)]
327-
(if-let [[_ params]
328-
(find xapi
329-
:xapi.activities.state.PUT.request/params)]
330-
(let [{hif-match "if-match"
331-
hif-none-match "if-none-match"}
332-
headers]
333-
(if (or hif-match hif-none-match)
334-
;; State document
335-
(put-response ctx
336-
(lrs/set-document
337-
lrs
338-
auth-identity
339-
params
340-
doc
341-
false))
342-
;; if neither header is present
343-
(let [err-res (lrs/get-document lrs auth-identity params)]
344-
(put-err-response ctx err-res))))
345-
;; Activity/Agent profile document
346-
(let [[_params-spec params]
347-
(find-some xapi
348-
:xapi.activities.profile.PUT.request/params
349-
:xapi.agents.profile.PUT.request/params)
350-
{hif-match "if-match"
351-
hif-none-match "if-none-match"} headers]
352-
(if (or hif-match hif-none-match)
312+
(put-err-response ctx doc-res))))
313+
(catch #?(:clj Exception :cljs js/Error) ex
314+
(assoc ctx :io.pedestal.interceptor.chain/error ex))))
315+
;; Sync
316+
(try
317+
(if (or hif-match hif-none-match)
318+
(put-response ctx
319+
(lrs/set-document
320+
lrs
321+
auth-identity
322+
params
323+
doc
324+
false))
325+
;; if neither header is present
326+
(let [doc-res (a/<! (lrs/get-document-async
327+
lrs
328+
auth-identity params))]
329+
(if (and (not (:error doc-res))
330+
(nil? (:document doc-res)))
353331
(put-response ctx
354-
(lrs/set-document
355-
lrs
356-
auth-identity
357-
params
358-
doc
359-
false))
360-
;; if neither header is present
361-
(let [err-res (lrs/get-document lrs auth-identity params)]
362-
(put-err-response ctx err-res))))))
363-
(catch #?(:clj Exception :cljs js/Error) ex
364-
(assoc ctx :io.pedestal.interceptor.chain/error ex))))))})
332+
(a/<! (lrs/set-document-async
333+
lrs
334+
auth-identity
335+
params
336+
doc
337+
false)))
338+
(put-err-response ctx doc-res))))
339+
(catch #?(:clj Exception :cljs js/Error) ex
340+
(assoc ctx :io.pedestal.interceptor.chain/error ex)))))))})
365341

366342
(s/fdef post-response
367343
:args (s/cat :ctx map?

0 commit comments

Comments
 (0)