This repository has been archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
post.go
60 lines (55 loc) · 2.29 KB
/
post.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
package models
import (
"encoding/json"
"time"
"github.com/jackc/pgtype"
)
// Post stores the data of a post
type Post struct {
ID int `json:"id"`
Title string `json:"title"`
Slug string `json:"slug"`
Body string `json:"body"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt pgtype.Timestamptz `json:"updatedAt"`
Tags []string `json:"tags"`
Hidden bool `json:"hidden"`
AuthorID string `json:"authorid"`
FeatureImgURL string `json:"featureImgUrl"`
Subtitle string `json:"subtitle"`
Views int `json:"views"`
}
// MarshalJSON marshals post data
func (p *Post) MarshalJSON() ([]byte, error) {
value, _ := p.UpdatedAt.Value()
if value == nil {
return json.Marshal(struct {
ID int `json:"id"`
Title string `json:"title"`
Slug string `json:"slug"`
Body string `json:"body"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt *pgtype.Timestamptz `json:"updatedAt"`
Tags []string `json:"tags"`
Hidden bool `json:"hidden"`
AuthorID string `json:"authorid"`
FeatureImgURL string `json:"featureImgUrl"`
Subtitle string `json:"subtitle"`
Views int `json:"views"`
}{p.ID, p.Title, p.Slug, p.Body, p.CreatedAt, nil, p.Tags, p.Hidden, p.AuthorID, p.FeatureImgURL, p.Subtitle, p.Views})
}
return json.Marshal(struct {
ID int `json:"id"`
Title string `json:"title"`
Slug string `json:"slug"`
Body string `json:"body"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
Tags []string `json:"tags"`
Hidden bool `json:"hidden"`
AuthorID string `json:"authorid"`
FeatureImgURL string `json:"featureImgUrl"`
Subtitle string `json:"subtitle"`
Views int `json:"views"`
}{p.ID, p.Title, p.Slug, p.Body, p.CreatedAt, p.UpdatedAt.Time, p.Tags, p.Hidden, p.AuthorID, p.FeatureImgURL, p.Subtitle, p.Views})
}