From 749a69a62e4a42e7ad16143260f5d011b995c681 Mon Sep 17 00:00:00 2001 From: Joeyyy09 Date: Thu, 3 Aug 2023 11:56:37 +0530 Subject: [PATCH] Update models.go --- models.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/models.go b/models.go index 700e9e5..e4e3bd3 100644 --- a/models.go +++ b/models.go @@ -78,4 +78,51 @@ func databaseFeedFollowsToFeedFollows(feedFollows []database.FeedFollow) []FeedF result[i] = databaseFeedFollowToFeedFollow(feedFollow) } return result +} + + +type Post struct { + ID uuid.UUID `json:"id"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` + Title string `json:"title"` + Url string `json:"url"` + Description *string `json:"description"` + PublishedAt *time.Time `json:"published_at"` + FeedID uuid.UUID `json:"feed_id"` +} + +func databasePostToPost(post database.Post) Post { + return Post{ + ID: post.ID, + CreatedAt: post.CreatedAt, + UpdatedAt: post.UpdatedAt, + Title: post.Title, + Url: post.Url, + Description: nullStringToStringPtr(post.Description), + PublishedAt: nullTimeToTimePtr(post.PublishedAt), + FeedID: post.FeedID, + } +} + +func databasePostsToPosts(posts []database.Post) []Post { + result := make([]Post, len(posts)) + for i, post := range posts { + result[i] = databasePostToPost(post) + } + return result +} + +func nullTimeToTimePtr(t sql.NullTime) *time.Time { + if t.Valid { + return &t.Time + } + return nil +} + +func nullStringToStringPtr(s sql.NullString) *string { + if s.Valid { + return &s.String + } + return nil } \ No newline at end of file