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,