Skip to content

Commit

Permalink
Update policy API to create files
Browse files Browse the repository at this point in the history
[Re #1543]
  • Loading branch information
lucassousaf committed Aug 10, 2023
1 parent b905b74 commit 25a45dd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
12 changes: 7 additions & 5 deletions backend/src/gpml/db/policy.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ insert into policy(
remarks,
review_status,
url,
image,
language
--~ (when (contains? params :id) ", id")
--~ (when (contains? params :created_by) ", created_by")
Expand All @@ -27,6 +26,8 @@ insert into policy(
--~ (when (contains? params :subnational_city) ", subnational_city")
--~ (when (contains? params :document_preview) ", document_preview")
--~ (when (contains? params :source) ", source")
--~ (when (contains? params :image_id) ", image_id")
--~ (when (contains? params :thumbnail_id) ", thumbnail_id")
)
values(
:title,
Expand All @@ -45,7 +46,6 @@ values(
:remarks,
:v:review_status::review_status,
:url,
:image,
:language
--~ (when (contains? params :id) ", :id")
--~ (when (contains? params :created_by) ", :created_by")
Expand All @@ -55,6 +55,8 @@ values(
--~ (when (contains? params :subnational_city) ", :subnational_city")
--~ (when (contains? params :document_preview) ", :document_preview")
--~ (when (contains? params :source) ", :source")
--~ (when (contains? params :image_id) ", :image_id")
--~ (when (contains? params :thumbnail_id) ", :thumbnail_id")
)
returning id;

Expand Down Expand Up @@ -88,7 +90,7 @@ select
language,
(select json_agg(tag) from policy_tag where policy = :id) as tags,
(select json_agg(coalesce(country, country_group))
from policy_geo_coverage where policy = :id) as geo_coverage_value
from policy_geo_coverage where policy = :id) as geo_coverage_value
from policy
where id = :id

Expand Down Expand Up @@ -116,9 +118,9 @@ select
document_preview,
language
(select json_agg(tag)
from policy_tag where policy = :id) as tags,
from policy_tag where policy = :id) as tags,
(select created_by
from policy where id = :id) as created_by
from policy where id = :id) as created_by
from v_policy_data
where id = :id

Expand Down
18 changes: 13 additions & 5 deletions backend/src/gpml/handler/policy.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[duct.logger :refer [log]]
[gpml.db.policy :as db.policy]
[gpml.domain.policy :as dom.policy]
[gpml.handler.image :as handler.image]
[gpml.handler.file :as handler.file]
[gpml.handler.resource.geo-coverage :as handler.geo]
[gpml.handler.resource.permission :as h.r.permission]
[gpml.handler.resource.related-content :as handler.resource.related-content]
Expand Down Expand Up @@ -39,7 +39,11 @@
document_preview related_content topics
attachments remarks entity_connections individual_connections
capacity_building source]}]
(let [data (cond-> {:title title
(let [image-id (when (seq image)
(handler.file/create-file config conn image :policy :images :public))
thumbnail-id (when (seq thumbnail)
(handler.file/create-file config conn thumbnail :policy :images :public))
data (cond-> {:title title
:original_title original_title
:abstract abstract
:url url
Expand All @@ -55,8 +59,6 @@
:sub_content_type sub_content_type
:document_preview document_preview
:topics (pg-util/->JDBCArray topics "text")
:image (handler.image/assoc-image config conn image "policy")
:thumbnail (handler.image/assoc-image config conn thumbnail "policy")
:geo_coverage_type geo_coverage_type
:geo_coverage_value geo_coverage_value
:geo_coverage_countries geo_coverage_countries
Expand All @@ -72,7 +74,13 @@
(assoc :capacity_building capacity_building)

(not (nil? source))
(assoc :source source))
(assoc :source source)

image-id
(assoc :image_id image-id)

thumbnail-id
(assoc :thumbnail_id thumbnail-id))
policy-id (->>
(util/update-if-not-nil data :source #(sql-util/keyword->pg-enum % "resource_source"))
(db.policy/new-policy conn) :id)
Expand Down

0 comments on commit 25a45dd

Please sign in to comment.