github.com/k0marov/go-socnet@v0.0.0-20220715154813-90d07867c782/features/posts/delivery/http/responses/responses.go (about)

     1  package responses
     2  
     3  import (
     4  	"github.com/k0marov/go-socnet/features/posts/domain/entities"
     5  	"github.com/k0marov/go-socnet/features/posts/domain/values"
     6  	profile_responses "github.com/k0marov/go-socnet/features/profiles/delivery/http/responses"
     7  )
     8  
     9  type PostImageResponse struct {
    10  	Index int    `json:"index"`
    11  	Url   string `json:"url"`
    12  }
    13  
    14  func newPostImageListResponse(images []values.PostImage) []PostImageResponse {
    15  	respList := make([]PostImageResponse, 0)
    16  	for _, img := range images {
    17  		resp := PostImageResponse{
    18  			Index: img.Index,
    19  			Url:   img.URL,
    20  		}
    21  		respList = append(respList, resp)
    22  	}
    23  	return respList
    24  }
    25  
    26  type PostResponse struct {
    27  	Id        string                            `json:"id"`
    28  	Author    profile_responses.ProfileResponse `json:"author"`
    29  	Text      string                            `json:"text"`
    30  	CreatedAt int64                             `json:"created_at"`
    31  	Images    []PostImageResponse               `json:"images"`
    32  	Likes     int                               `json:"likes"`
    33  	IsLiked   bool                              `json:"is_liked"`
    34  	IsMine    bool                              `json:"is_mine"`
    35  }
    36  type PostsResponse struct {
    37  	Posts []PostResponse `json:"posts"`
    38  }
    39  
    40  func NewPostListResponse(posts []entities.ContextedPost) PostsResponse {
    41  	postResponses := make([]PostResponse, 0)
    42  	for _, post := range posts {
    43  		resp := PostResponse{
    44  			Id:        post.Id,
    45  			Author:    profile_responses.NewProfileResponse(post.Author),
    46  			Text:      post.Text,
    47  			CreatedAt: post.CreatedAt,
    48  			Images:    newPostImageListResponse(post.Images),
    49  			Likes:     post.Likes,
    50  			IsLiked:   post.IsLiked,
    51  			IsMine:    post.IsMine,
    52  		}
    53  		postResponses = append(postResponses, resp)
    54  	}
    55  	return PostsResponse{
    56  		Posts: postResponses,
    57  	}
    58  }