0
0
mirror of https://github.com/mrusme/neonmodem.git synced 2025-06-30 22:18:39 -04: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

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

View File

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