File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments