Skip to content

Commit

Permalink
Standardise params across create/update
Browse files Browse the repository at this point in the history
  • Loading branch information
dangerousbeans committed Apr 8, 2024
1 parent 4ed2b53 commit 2330125
Showing 1 changed file with 39 additions and 56 deletions.
95 changes: 39 additions & 56 deletions lib/discourse_api/api/categories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,15 @@ module Categories
# :permissions is a hash with the group name and permission_type which is
# an integer 1 = Full 2 = Create Post 3 = Read Only
def create_category(args = {})
args =
API
.params(args)
.required(:name)
.optional(
:color,
:text_color,
:slug,
:permissions,
:auto_close_hours,
:auto_close_based_on_last_post,
:position,
:email_in,
:email_in_allow_strangers,
:logo_url,
:background_url,
:allow_badges,
:topic_template,
:custom_fields,
:description,
:reviewable_by_group_name,
:show_subcategory_list,
:subcategory_list_style,
:allowed_tags,
:allowed_tag_groups,
:required_tag_group_name,
)
.default(parent_category_id: nil)
response = post("/categories", args)
params = common_category_params(args)
response = post("/categories", params.to_h)
response["category"]
end

def update_category(args = {})
category_id = args[:id]
args =
API
.params(args)
.required(:id, :name, :color, :text_color)
.optional(
:slug,
:permissions,
:auto_close_hours,
:auto_close_based_on_last_post,
:position,
:email_in,
:email_in_allow_strangers,
:logo_url,
:background_url,
:allow_badges,
:topic_template,
:custom_fields,
:description,
:reviewable_by_group_name,
:show_subcategory_list,
:subcategory_list_style,
:allowed_tags,
:allowed_tag_groups,
:required_tag_group_name,
)
.default(parent_category_id: nil)
response = put("/categories/#{category_id}", args)
params = common_category_params(args, include_id: true)
response = put("/categories/#{category_id}", params.to_h)
response["body"]["category"] if response["body"]
end

Expand Down Expand Up @@ -146,6 +94,41 @@ def category_set_user_notification_level(category_id, params)
params = API.params(params).required(:notification_level)
post("/category/#{category_id}/notifications", params)
end

private

def common_category_params(args, include_id: false)
params = API.params(args)
params = params.required(:id) if include_id
params.required(:name)
.optional(
:color,
:text_color,
:slug,
:permissions,
:auto_close_hours,
:auto_close_based_on_last_post,
:position,
:email_in,
:email_in_allow_strangers,
:logo_url,
:background_url,
:allow_badges,
:topic_template,
:custom_fields,
:description,
:reviewable_by_group_name,
:show_subcategory_list,
:subcategory_list_style,
:allowed_tags,
:allowed_tag_groups,
:required_tag_group_name,
:topic_featured_links_allowed,
:search_priority,
:form_template_ids
)
.default(parent_category_id: nil)
end
end
end
end

0 comments on commit 2330125

Please sign in to comment.