1
0
mirror of https://github.com/mrusme/neonmodem.git synced 2024-12-04 14:46:37 -05:00

Implemented Discourse Create, moved all into api

This commit is contained in:
マリウス 2023-01-01 22:20:34 -05:00
parent 5acfb29c3a
commit 73862a7c8f
No known key found for this signature in database
GPG Key ID: 272ED814BF63261F
5 changed files with 45 additions and 8 deletions

View File

@ -1,4 +1,4 @@
package discourse
package api
import (
"context"

View File

@ -1,4 +1,4 @@
package discourse
package api
import (
"bytes"

View File

@ -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,

View File

@ -1,4 +1,4 @@
package discourse
package api
import (
"context"

View File

@ -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,