From 73862a7c8f09dc03e4f540ba2ae644239faada73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=9E=E3=83=AA=E3=82=A6=E3=82=B9?= Date: Sun, 1 Jan 2023 22:20:34 -0500 Subject: [PATCH] Implemented Discourse Create, moved all into api --- system/discourse/{ => api}/categories.go | 2 +- system/discourse/{ => api}/client.go | 2 +- system/discourse/{ => api}/posts.go | 42 ++++++++++++++++++++++-- system/discourse/{ => api}/topics.go | 2 +- system/discourse/discourse.go | 5 +-- 5 files changed, 45 insertions(+), 8 deletions(-) rename system/discourse/{ => api}/categories.go (99%) rename system/discourse/{ => api}/client.go (99%) rename system/discourse/{ => api}/posts.go (78%) rename system/discourse/{ => api}/topics.go (99%) diff --git a/system/discourse/categories.go b/system/discourse/api/categories.go similarity index 99% rename from system/discourse/categories.go rename to system/discourse/api/categories.go index 21060a6..c627f9c 100644 --- a/system/discourse/categories.go +++ b/system/discourse/api/categories.go @@ -1,4 +1,4 @@ -package discourse +package api import ( "context" diff --git a/system/discourse/client.go b/system/discourse/api/client.go similarity index 99% rename from system/discourse/client.go rename to system/discourse/api/client.go index 135c95d..a7d06d9 100644 --- a/system/discourse/client.go +++ b/system/discourse/api/client.go @@ -1,4 +1,4 @@ -package discourse +package api import ( "bytes" diff --git a/system/discourse/posts.go b/system/discourse/api/posts.go similarity index 78% rename from system/discourse/posts.go rename to system/discourse/api/posts.go index cf6f5e8..2751bad 100644 --- a/system/discourse/posts.go +++ b/system/discourse/api/posts.go @@ -1,4 +1,4 @@ -package discourse +package api import ( "context" @@ -7,8 +7,16 @@ import ( const PostsBaseURL = "/posts" -type ListPostsResponse struct { - LatestPosts []PostModel `json:"latest_posts,omitempty"` +type CreatePostModel struct { + Title string `json:"title,omitempty"` + Raw string `json:"raw"` + TopicID int `json:"topic_id,omitempty"` + Category int `json:"category,omitempty"` + TargetRecipients string `json:"targe_recipients,omitempty"` + Archetype string `json:"archetype,omitempty"` + CreatedAt string `json:"created_at"` + EmbedURL string `json:"embed_url,omitempty"` + ExternalID string `json:"external_id,omitempty"` } type PostModel struct { @@ -65,7 +73,15 @@ type PostModel struct { ReviewableScorePendingCount int `json:"reviewable_score_pending_count"` } +type ListPostsResponse struct { + LatestPosts []PostModel `json:"latest_posts,omitempty"` +} + type PostsService interface { + Create( + ctx context.Context, + w *CreatePostModel, + ) (PostModel, error) Show( ctx context.Context, id string, @@ -79,6 +95,26 @@ type PostServiceHandler struct { client *Client } +// Create +func (a *PostServiceHandler) Create( + ctx context.Context, + w *CreatePostModel, +) (PostModel, error) { + uri := PostsBaseURL + ".json" + + req, err := a.client.NewRequest(ctx, http.MethodPost, uri, w) + if err != nil { + return PostModel{}, err + } + + response := new(Response) + if err = a.client.Do(ctx, req, response); err != nil { + return PostModel{}, err + } + + return response.Post, nil +} + // Show func (a *PostServiceHandler) Show( ctx context.Context, diff --git a/system/discourse/topics.go b/system/discourse/api/topics.go similarity index 99% rename from system/discourse/topics.go rename to system/discourse/api/topics.go index 7cf93f4..202ad01 100644 --- a/system/discourse/topics.go +++ b/system/discourse/api/topics.go @@ -1,4 +1,4 @@ -package discourse +package api import ( "context" diff --git a/system/discourse/discourse.go b/system/discourse/discourse.go index cfce8bb..aaf9edf 100644 --- a/system/discourse/discourse.go +++ b/system/discourse/discourse.go @@ -13,13 +13,14 @@ import ( "github.com/mrusme/gobbs/models/post" "github.com/mrusme/gobbs/models/reply" "github.com/mrusme/gobbs/system/adapter" + "github.com/mrusme/gobbs/system/discourse/api" "go.uber.org/zap" ) type System struct { config map[string]interface{} logger *zap.SugaredLogger - client *Client + client *api.Client } func (sys *System) GetConfig() map[string]interface{} { @@ -64,7 +65,7 @@ func (sys *System) Load() error { credentials[k] = v.(string) } - sys.client = NewClient(&ClientConfig{ + sys.client = api.NewClient(&api.ClientConfig{ Endpoint: url.(string), Credentials: credentials, HTTPClient: http.DefaultClient,