feat: add CreateRelatedArticle

This commit is contained in:
Buravit Yenjit 2025-04-01 14:52:47 +07:00
parent dd86f38b5a
commit 8af97c0150
3 changed files with 62 additions and 0 deletions

View File

@ -58,6 +58,13 @@ func (a *api) registerKnowledgeHubRoutes(_ chi.Router, api huma.API) {
Path: prefix + "/{uuid}/related", Path: prefix + "/{uuid}/related",
Tags: tags, Tags: tags,
}, a.getArticleRelatedArticlesHandler) }, a.getArticleRelatedArticlesHandler)
huma.Register(api, huma.Operation{
OperationID: "createRelatedArticle",
Method: http.MethodPost,
Path: prefix + "/{uuid}/related",
Tags: tags,
}, a.createRelatedArticleHandler)
} }
type GetKnowledgeArticlesOutput struct { type GetKnowledgeArticlesOutput struct {
@ -102,6 +109,14 @@ type GetRelatedArticlesOutput struct {
} `json:"body"` } `json:"body"`
} }
type CreateRelatedArticleInput struct {
UUID string `path:"uuid"`
Body struct {
RelatedTitle string `json:"related_title"`
RelatedTag string `json:"related_tag"`
} `json:"body"`
}
func (a *api) getAllKnowledgeArticlesHandler(ctx context.Context, input *struct{}) (*GetKnowledgeArticlesOutput, error) { func (a *api) getAllKnowledgeArticlesHandler(ctx context.Context, input *struct{}) (*GetKnowledgeArticlesOutput, error) {
resp := &GetKnowledgeArticlesOutput{} resp := &GetKnowledgeArticlesOutput{}
@ -251,3 +266,25 @@ func (a *api) getArticleRelatedArticlesHandler(ctx context.Context, input *struc
resp.Body.RelatedArticles = related resp.Body.RelatedArticles = related
return resp, nil return resp, nil
} }
func (a *api) createRelatedArticleHandler(
ctx context.Context,
input *CreateRelatedArticleInput,
) (*struct{}, error) {
// Validate main article exists
if _, err := a.knowledgeHubRepo.GetArticleByID(ctx, input.UUID); err != nil {
return nil, huma.Error404NotFound("main article not found")
}
// Create related article
related := &domain.RelatedArticle{
RelatedTitle: input.Body.RelatedTitle,
RelatedTag: input.Body.RelatedTag,
}
if err := a.knowledgeHubRepo.CreateRelatedArticle(ctx, input.UUID, related); err != nil {
return nil, huma.Error500InternalServerError("failed to create related article")
}
return nil, nil // HTTP 204 No Content
}

View File

@ -55,4 +55,5 @@ type KnowledgeHubRepository interface {
GetTableOfContents(ctx context.Context, articleID string) ([]TableOfContent, error) GetTableOfContents(ctx context.Context, articleID string) ([]TableOfContent, error)
GetRelatedArticles(ctx context.Context, articleID string) ([]RelatedArticle, error) GetRelatedArticles(ctx context.Context, articleID string) ([]RelatedArticle, error)
CreateRelatedArticle(ctx context.Context, articleID string, related *RelatedArticle) error
} }

View File

@ -182,3 +182,27 @@ func (p *postgresKnowledgeHubRepository) GetRelatedArticles(ctx context.Context,
return p.fetchRelatedArticles(ctx, query, articleID) return p.fetchRelatedArticles(ctx, query, articleID)
} }
func (p *postgresKnowledgeHubRepository) CreateRelatedArticle(
ctx context.Context,
articleID string,
related *domain.RelatedArticle,
) error {
related.UUID = uuid.New().String() // Generate UUID
related.ArticleID = articleID // Link to main article
query := `
INSERT INTO related_articles
(uuid, article_id, related_title, related_tag, created_at, updated_at)
VALUES ($1, $2, $3, $4, NOW(), NOW())`
_, err := p.conn.Exec(
ctx,
query,
related.UUID,
related.ArticleID,
related.RelatedTitle,
related.RelatedTag,
)
return err
}