I have the following data:
({:seriesId "series 0", :episodeId "0"}
{:seriesId "series 1", :episodeId "1"}
{:seriesId "series 1", :episodeId "2"}
{:seriesId "series 2", :episodeId "3"}
{:seriesId "series 2", :episodeId "4"}
{:seriesId "series 2", :episodeId "5"})
And would like to associate each episode to its series, like this:
[{:put-request
{:item {:seriesId "series 0", :episodeCount 1, :episodeIds #{"0"}}}}
{:put-request
{:item {:seriesId "series 1", :episodeCount 2, :episodeIds #{"1" "2"}}}}
{:put-request
{:item {:seriesId "series 2", :episodeCount 3, :episodeIds #{"3" "4" "5"}}}}]
Currently I am stuck with the following:
[{:put-request
{:item {:seriesId "series 0", :episodeCount 1, :episodeIds #{"0"}}}}
{:put-request
{:item {:seriesId "series 1", :episodeCount 1, :episodeIds #{"1"}}}}
{:put-request
{:item {:seriesId "series 1", :episodeCount 1, :episodeIds #{"2"}}}}
{:put-request
{:item {:seriesId "series 2", :episodeCount 1, :episodeIds #{"3"}}}}
{:put-request
{:item {:seriesId "series 2", :episodeCount 1, :episodeIds #{"4"}}}}
{:put-request
{:item {:seriesId "series 2", :episodeCount 1, :episodeIds #{"5"}}}}]
I am using the create-or-update-series
function. I don't know how to find/get a previously added series (if added!) using the seriesId
. I tried many things but these were dead-end tracks.
(ns clojure-sscce.core
(:gen-class)
(:require clojure.pprint))
(defn create-or-update-series
([episodes]
(create-or-update-series episodes []))
([episodes result]
(if (zero? (count episodes))
result
(create-or-update-series (rest episodes)
(conj result {
:put-request {
:item {
:seriesId (:seriesId (first episodes))
:episodeCount 1
:episodeIds #{(:episodeId (first episodes))}}}})))))
;; Tests
(defn -main [& args]
(let
[series0 (mapv (fn [episode-id] {
:seriesId "series 0"
:episodeId (str episode-id)}) (range 0 1))
series1 (mapv (fn [episode-id] {
:seriesId "series 1"
:episodeId (str episode-id)}) (range 1 3))
series2 (mapv (fn [episode-id] {
:seriesId "series 2"
:episodeId (str episode-id)}) (range 3 6))]
(clojure.pprint/pprint
(concat series0 series1 series2))
(clojure.pprint/pprint
(create-or-update-series (concat series0 series1 series2)))))
Note that {:put-request {:item { ...
is needed because the new maps are expected to be PUT to DynamoDB.
Would love your help!
seriesId
andepisodeId
must be strings. – Stéphane Bruckert