1
0
mirror of https://github.com/mrusme/neonmodem.git synced 2024-06-16 06:25:23 +00:00

Refactored response

This commit is contained in:
マリウス 2022-12-29 23:52:39 -05:00
parent d6436021f1
commit b11bfe46fd
No known key found for this signature in database
GPG Key ID: 272ED814BF63261F
2 changed files with 11 additions and 8 deletions

View File

@ -13,8 +13,7 @@ import (
)
type Response struct {
Post PostModel `json:"post,omitempty"`
Posts []PostModel `json:"latest_posts,omitempty"`
Post PostModel `json:"post,omitempty"`
}
type RequestError struct {

View File

@ -7,6 +7,10 @@ import (
const PostsBaseURL = "/posts"
type ListPostsResponse struct {
LatestPosts []PostModel `json:"latest_posts,omitempty"`
}
type PostModel struct {
ID int `json:"id"`
Name string `json:"name"`
@ -68,7 +72,7 @@ type PostsService interface {
) (PostModel, error)
List(
ctx context.Context,
) ([]PostModel, error)
) (*ListPostsResponse, error)
}
type PostServiceHandler struct {
@ -98,18 +102,18 @@ func (a *PostServiceHandler) Show(
// List
func (a *PostServiceHandler) List(
ctx context.Context,
) ([]PostModel, error) {
) (*ListPostsResponse, error) {
uri := PostsBaseURL + ".json"
req, err := a.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return []PostModel{}, err
return nil, err
}
response := new(Response)
response := new(ListPostsResponse)
if err = a.client.Do(ctx, req, response); err != nil {
return []PostModel{}, err
return nil, err
}
return response.Posts, nil
return response, nil
}