-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.go
111 lines (99 loc) · 3.07 KB
/
models.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import "github.com/abrl91/golang-rss-scraper/internal/database"
type User struct {
ID string `json:"id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Name string `json:"name"`
ApiKey string `json:"api_key"`
}
type Feed struct {
ID string `json:"id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Title string `json:"title"`
Url string `json:"url"`
UserID string `json:"user_id"`
}
type FeedFollow struct {
ID string `json:"id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
UserID string `json:"user_id"`
FeedID string `json:"feed_id"`
}
type Post struct {
ID string `json:"id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Title string `json:"title"`
Body *string `json:"body"` // because it can be null, we use a pointer so that if it returns null, it will return null in the json response
PublishedAt string `json:"published_at"`
Url string `json:"url"`
FeedID string `json:"feed_id"`
}
func databaseUserToUser(dbUser database.User) User {
return User{
ID: dbUser.ID.String(),
CreatedAt: dbUser.CreatedAt.String(),
UpdatedAt: dbUser.UpdatedAt.String(),
Name: dbUser.Name,
ApiKey: dbUser.ApiKey,
}
}
func databaseFeedToFeed(dbFeed database.Feed) Feed {
return Feed{
ID: dbFeed.ID.String(),
CreatedAt: dbFeed.CreatedAt.String(),
UpdatedAt: dbFeed.UpdatedAt.String(),
Title: dbFeed.Title,
Url: dbFeed.Url,
UserID: dbFeed.UserID.String(),
}
}
func databaseFeedsToFeeds(dbFeeds []database.Feed) []Feed {
feeds := make([]Feed, 0, len(dbFeeds))
for _, dbFeed := range dbFeeds {
feeds = append(feeds, databaseFeedToFeed(dbFeed))
}
return feeds
}
func databaseFeedFollowToFeedFollow(dbFeedFollow database.FeedFollow) FeedFollow {
return FeedFollow{
ID: dbFeedFollow.ID.String(),
CreatedAt: dbFeedFollow.CreatedAt.String(),
UpdatedAt: dbFeedFollow.UpdatedAt.String(),
UserID: dbFeedFollow.UserID.String(),
FeedID: dbFeedFollow.FeedID.String(),
}
}
func databaseFeedFollowsToFeedFollows(dbFeedFollows []database.FeedFollow) []FeedFollow {
feedFollows := make([]FeedFollow, 0, len(dbFeedFollows))
for _, dbFeedFollow := range dbFeedFollows {
feedFollows = append(feedFollows, databaseFeedFollowToFeedFollow(dbFeedFollow))
}
return feedFollows
}
func databasePostToPost(dbPost database.Post) Post {
var description *string
if dbPost.Body.Valid {
description = &dbPost.Body.String
}
return Post{
ID: dbPost.ID.String(),
CreatedAt: dbPost.CreatedAt.String(),
UpdatedAt: dbPost.UpdatedAt.String(),
Title: dbPost.Title,
Body: description,
PublishedAt: dbPost.PublishedAt.String(),
Url: dbPost.Url,
FeedID: dbPost.FeedID.String(),
}
}
func databasePostsToPosts(dbPosts []database.Post) []Post {
posts := make([]Post, 0, len(dbPosts))
for _, dbPost := range dbPosts {
posts = append(posts, databasePostToPost(dbPost))
}
return posts
}