Skip to content

Commit d2bca55

Browse files
committed
Add an endpoint for fetching articles
1 parent b88d8d0 commit d2bca55

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

backend/web/api/article.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package api
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/theandrew168/bloggulus/backend/query"
7+
"github.com/theandrew168/bloggulus/backend/web/api/jsonutil"
8+
)
9+
10+
func HandleArticleList(qry *query.Query) http.Handler {
11+
type response struct {
12+
Articles []query.Article `json:"articles"`
13+
}
14+
15+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
16+
articles, err := qry.ListRecentArticles(2, 0)
17+
if err != nil {
18+
http.Error(w, "Error fetching articles", http.StatusInternalServerError)
19+
return
20+
}
21+
22+
jsonutil.Write(w, http.StatusOK, response{
23+
Articles: articles,
24+
})
25+
})
26+
}

backend/web/api/handler.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ func Handler(
1414
qry *query.Query,
1515
) http.Handler {
1616
mux := http.NewServeMux()
17-
mux.Handle("GET /articles", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
18-
w.WriteHeader(200)
19-
w.Write([]byte("Articles API Endpoint"))
20-
}))
17+
mux.Handle("GET /articles", HandleArticleList(qry))
2118
return mux
2219
}

0 commit comments

Comments
 (0)