Skip to content

Commit 8c10c28

Browse files
authored
Merge pull request #122 from modulr-software/feat/integration-content-types
integration content types
2 parents fdecd75 + 2946287 commit 8c10c28

File tree

6 files changed

+140
-9
lines changed

6 files changed

+140
-9
lines changed

src/source/db/master.clj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@
6767
(tables/foreign-key :content-type-id :content-types :id)
6868
(tables/foreign-key :user-id :users :id)))
6969

70+
(def bundle-content-types
71+
(tables/create-table-sql
72+
:bundle-content-types
73+
(tables/table-id)
74+
[:bundle-id :integer :not nil]
75+
[:content-type-id :integer :not nil]
76+
(tables/foreign-key :bundle-id :bundles :id)
77+
(tables/foreign-key :content-type-id :content-types :id)))
78+
7079
(def feeds
7180
(tables/create-table-sql
7281
:feeds
@@ -203,6 +212,7 @@
203212
(sql/format categories)
204213
(sql/format baselines)
205214
(sql/format bundles)
215+
(sql/format bundle-content-types)
206216
(sql/format feeds)
207217
(sql/format feed-categories)
208218
(sql/format providers)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
(ns source.migrations.003-bundle-content-types
2+
(:require [source.db.master]
3+
[source.db.tables :as tables]
4+
[source.services.interface :as services]))
5+
6+
(defn run-up! [context]
7+
(let [ds-master (:db-master context)
8+
bundles (services/bundles ds-master)]
9+
10+
(tables/create-tables!
11+
ds-master
12+
:source.db.master
13+
[:bundle-content-types])
14+
15+
(run! (fn [{:keys [id content-type-id]}]
16+
(when (some? content-type-id)
17+
(services/insert-bundle-content-types! ds-master {:data {:bundle-id id
18+
:content-type-id content-type-id}})))
19+
bundles)))
20+
21+
(defn run-down! [context]
22+
(let [ds-master (:db-master context)
23+
bundle-content-types (services/bundles ds-master)]
24+
25+
(run! (fn [{:keys [bundle-id content-type-id]}]
26+
(services/update-bundle! ds-master {:id bundle-id
27+
:data {:content-type-id content-type-id}}))
28+
bundle-content-types)
29+
30+
(tables/drop-table! ds-master :bundle-content-types)))

src/source/routes/integration.clj

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,29 @@
2020
[:blog :int]
2121
[:hash [:maybe :string]]
2222
[:content-type-id :int]
23-
[:ts-and-cs [:maybe :int]]]}
23+
[:ts-and-cs [:maybe :int]]
24+
[:content-types [:vector
25+
[:map
26+
[:id :int]
27+
[:name :string]]]]]}
2428
401 {:body [:map [:message :string]]}
2529
403 {:body [:map [:message :string]]}}}
2630

2731
[{:keys [ds path-params] :as _request}]
28-
(res/response (services/bundle ds {:id (:id path-params)})))
32+
(let [integration (services/bundle ds {:id (:id path-params)})
33+
content-types (services/content-types-by-bundle ds {:bundle-id (:id path-params)})]
34+
(res/response (assoc integration :content-types content-types))))
2935

3036
(defn post
3137
{:summary "update an integration by id"
3238
:parameters {:path [:map [:id {:title "id"
3339
:description "integration id"} :int]]
3440
:body [:map
3541
[:name :string]
36-
[:content-type-id :int]
42+
[:content-types [:vector
43+
[:map
44+
[:id :int]
45+
[:name :string]]]]
3746
[:categories [:vector
3847
[:map
3948
[:id :int]
@@ -44,18 +53,26 @@
4453

4554
[{:keys [js ds store path-params body] :as _request}]
4655
(let [_ (services/update-bundle! ds {:id (:id path-params)
47-
:data (dissoc body :categories)})
56+
:data (dissoc body :categories :content-types)})
4857

4958
bundle-categories (mapv (fn [{:keys [id]}]
5059
{:bundle-id (:id path-params)
5160
:category-id id}) (:categories body))
61+
bundle-content-types (mapv (fn [{:keys [id]}]
62+
{:bundle-id (:id path-params)
63+
:content-type-id id}) (:content-types body))
64+
5265
job-id (str "bundle_" (:id path-params))
5366

5467
; update bundle categories
5568
bundle-ds (db.util/conn :bundle (:id path-params))
5669
_ (services/delete-bundle-category! bundle-ds {:where [:= :bundle-id (:id path-params)]})
5770
_ (services/insert-bundle-category! bundle-ds {:data bundle-categories})
5871

72+
; update bundle content types
73+
_ (services/delete-bundle-content-types! ds {:where [:= :bundle-id (:id path-params)]})
74+
_ (services/insert-bundle-content-types! ds {:data bundle-content-types})
75+
5976
category-ids (services/category-id-by-bundle bundle-ds {:bundle-id (:id path-params)})
6077
id-vec (mapv (fn [{:keys [category-id]}] category-id) category-ids)
6178
categories-by-bundle (services/categories ds {:where [:in :id id-vec]})]

src/source/routes/integrations.clj

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@
3131
{:summary "add an integration"
3232
:parameters {:body [:map
3333
[:name :string]
34-
[:content-type-id :int]
3534
[:ts-and-cs {:optional true} :int]
35+
[:content-types [:vector
36+
[:map
37+
[:id :int]
38+
[:name :string]]]]
3639
[:categories [:vector
3740
[:map
3841
[:id :int]
@@ -53,17 +56,25 @@
5356

5457
[{:keys [js ds store user body] :as _request}]
5558
(let [new-bundle (services/insert-bundle! ds {:data (merge
56-
(dissoc body :categories)
59+
(dissoc body :categories :content-types)
5760
{:user-id (:id user)
61+
:content-type-id 1 ; temporarily assign garbo id
5862
:uuid (utils/uuid)})
5963
:ret :1})
60-
bundle-categories (reduce (fn [acc {:keys [id]}]
61-
(conj acc {:bundle-id (:id new-bundle)
62-
:category-id id})) [] (:categories body))
64+
bundle-categories (mapv (fn [{:keys [id]}]
65+
{:bundle-id (:id new-bundle)
66+
:category-id id}) (:categories body))
67+
bundle-content-types (mapv (fn [{:keys [id]}]
68+
{:bundle-id (:id new-bundle)
69+
:content-type-id id}) (:content-types body))
70+
6371
_ (migrate/migrate-bundle (:id new-bundle) ["up"])
6472
; insert bundle categories
6573
bundle-ds (db.util/conn :bundle (:id new-bundle))
74+
6675
_ (services/insert-bundle-category! bundle-ds {:data bundle-categories})
76+
_ (services/insert-bundle-content-types! ds {:data bundle-content-types})
77+
6778
category-ids (services/category-id-by-bundle bundle-ds {:bundle-id (:id new-bundle)})
6879
id-vec (mapv (fn [{:keys [category-id]}] category-id) category-ids)
6980
categories-by-bundle (services/categories ds {:where [:in :id id-vec]})]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
(ns source.services.bundle-content-types
2+
(:require [source.db.interface :as db]
3+
[source.db.honey :as hon]))
4+
5+
(defn bundle-content-types
6+
([ds] (bundle-content-types ds {}))
7+
([ds {:keys [where] :as opts}]
8+
(->> {:tname :bundle-content-types
9+
:where where
10+
:ret :*}
11+
(merge opts)
12+
(db/find ds))))
13+
14+
(defn insert-bundle-content-types! [ds {:keys [_data _ret] :as opts}]
15+
(->> {:tname :bundle-content-types}
16+
(merge opts)
17+
(db/insert! ds)))
18+
19+
(defn delete-bundle-content-types! [ds {:keys [id where] :as opts}]
20+
(->> {:tname :bundle-content-types
21+
:where (if (some? id)
22+
[:= :id id]
23+
where)
24+
:ret :1}
25+
(merge opts)
26+
(db/delete! ds)))
27+
28+
(defn content-types-by-bundle [ds {:keys [bundle-id where] :as _opts}]
29+
(hon/execute! ds
30+
{:select [[:bundle-content-types.content-type-id :id] :name]
31+
:from :content-types
32+
:join [:bundle-content-types [:= :bundle-content-types.content-type-id :content-types.id]]
33+
:where (if (some? bundle-id)
34+
[:= :bundle-id bundle-id]
35+
where)}
36+
{:ret :*}))
37+
38+
(defn content-type-id [ds {:keys [bundle-id where] :as opts}]
39+
(->> {:tname :bundle-content-types
40+
:where (if (some? bundle-id)
41+
[:= :bundle-id bundle-id]
42+
where)
43+
:ret :*}
44+
(merge opts)
45+
(db/find ds)))

src/source/services/interface.clj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
[source.services.xml-schemas :as xml]
66
[source.services.bundles :as bundles]
77
[source.services.bundle-categories :as bundle-categories]
8+
[source.services.bundle-content-types :as bundle-content-types]
89
[source.services.post-heuristics :as post-heuristics]
910
[source.services.providers :as providers]
1011
[source.services.feeds :as feeds]
@@ -86,6 +87,23 @@
8687
(defn category-id-by-bundle [ds {:keys [_bundle-id _where] :as opts}]
8788
(bundle-categories/category-id ds opts))
8889

90+
(defn bundle-content-types
91+
([ds] (bundle-content-types ds {}))
92+
([ds {:keys [_where] :as opts}]
93+
(bundle-content-types/bundle-content-types ds opts)))
94+
95+
(defn insert-bundle-content-types! [ds {:keys [_data _ret] :as opts}]
96+
(bundle-content-types/insert-bundle-content-types! ds opts))
97+
98+
(defn delete-bundle-content-types! [ds {:keys [_id _where] :as opts}]
99+
(bundle-content-types/delete-bundle-content-types! ds opts))
100+
101+
(defn content-types-by-bundle [ds {:keys [_bundle-id _where] :as opts}]
102+
(bundle-content-types/content-types-by-bundle ds opts))
103+
104+
(defn content-type-id [ds {:keys [_bundle-id _where] :as opts}]
105+
(bundle-content-types/content-type-id ds opts))
106+
89107
(defn insert-post-heuristics! [ds {:keys [_data] :as opts}]
90108
(post-heuristics/insert-post-heuristics! ds opts))
91109

0 commit comments

Comments
 (0)