/
articles.go
173 lines (151 loc) · 4.83 KB
/
articles.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2013 Justin Wilson. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package articles
import (
"fmt"
"log"
"strings"
"time"
"code.minty.io/wombat/backends"
)
/*type Img struct {
Src string `src` //`json:"src"` // data:"src"`
Alt string `alt` //`json:"alt"` // data:"alt"`
W int `w` //`json:"w"` // data:"w"`
H int `h` //`json:"h"` // data:"h"`
}
type Article struct {
Printer `-` //`json:"-"`
TitlePath string `titlePath` //`json:"titlePath"` // data:"titlepath"`
Title string `title` //`json:"title"` // data:"title"`
Synopsis string `synopsis` //`json:"synopsis" // data:"synopsis"`
Content string `content` //`json:"content"` // data:"content"`
IsPublished bool `isPublished` //`json:"isActive"` // data:"isActive"`
Created time.Time `created` //`json:"created"` // data:"created"`
Modified time.Time `modified` //`json:"modified"` // data:"modified"`
Img Img `img` //`json:"img"` // data:"img"`
Imgs []Img `imgs` //`json:"imgs"` // data:"imgs"`
}*/
// TODO I'd like to use something like the above (data tags), or leave it up to the backend
// implementation, instead of having implementation specific tags (bson tags).
type Img struct {
Src string `json:"src" bson:"src"`
Alt string `json:"alt" bson:"alt"`
W int `json:"w" bson:"w"`
H int `json:"h" bson:"h"`
}
type Article struct {
Printer `json:"-" bson:"-"`
TitlePath string `json:"titlePath" bson:"titlePath"`
Title string `json:"title" bson:"title"`
Synopsis string `json:"synopsis" bson:"synopsis"`
Content string `json:"content" bson:"content"`
IsPublished bool `json:"isPublished" bson:"isPublished"`
Created time.Time `json:"created" bson:"created"`
Modified time.Time `json:"modified" bson:"modified"`
Img Img `json:"img" bson:"img"`
Imgs []Img `json:"imgs" bson:"imgs"`
}
type Articles struct {
Reader
}
type Reader interface {
ByTitlePath(titlePath string, unPublished bool) (interface{}, error)
Recent(limit, page int, unPublished bool) (interface{}, error)
}
type Printer interface {
Print(article interface{}) error
UpdateSynopsis(titlePath, synopsis string, modified time.Time) error
UpdateContent(titlePath, content string, modified time.Time) error
Delete(titlePath string) error
Publish(titlePath string, publish bool) error
WriteImg(titlePath string, img interface{}) error
WriteImgs(titlePath string, imgs interface{}) error
}
const VERSION string = "0.0.2"
func New() Articles {
var r Reader
if p, err := backends.Open("wombat:apps:article-reader"); err != nil {
log.Fatal("No 'article' reader available")
} else {
if o, ok := p.(Reader); !ok {
log.Fatal("Invalid 'article' reader")
} else {
r = o
}
}
return Articles{r}
}
func NewArticle(title string) *Article {
if p, err := backends.Open("wombat:apps:article-printer"); err != nil {
log.Println("No 'article' printer available")
} else {
if printer, ok := p.(Printer); !ok {
log.Println("Invalid 'article' printer")
} else {
tp, t := titlePathTime(title)
return &Article{Printer: printer,
Title: title,
TitlePath: tp,
Created: t}
}
}
return nil
}
func titlePathTime(title string) (string, time.Time) {
// create a new article, based on the current time
t := time.Now()
titlePath := fmt.Sprintf("%d/%02d/%02d/%s/",
t.Year(),
t.Month(),
t.Day(),
strings.Replace(title, " ", "-", -1))
return titlePath, t
}
func (a *Article) Print() error {
return a.Printer.Print(a)
}
func (a *Article) UpdateContent(content string) error {
return a.Printer.UpdateContent(a.TitlePath, content, time.Now())
}
func (a *Article) SetSynopsis(synopsis string) (err error) {
modified := time.Now()
if err = a.Printer.UpdateSynopsis(a.TitlePath, synopsis, modified); err == nil {
a.Synopsis = synopsis
a.Modified = modified
}
return
}
func (a *Article) SetContent(content string) (err error) {
modified := time.Now()
if err = a.Printer.UpdateContent(a.TitlePath, content, modified); err == nil {
a.Content = content
a.Modified = modified
}
return
}
func (a *Article) Delete() error {
return a.Printer.Delete(a.TitlePath)
}
func (a *Article) Publish(publish bool) (err error) {
if err = a.Printer.Publish(a.TitlePath, publish); err == nil {
a.IsPublished = publish
}
return
}
func (a *Article) SetImg(img Img) (err error) {
if err = a.Printer.WriteImg(a.TitlePath, img); err == nil {
a.Img = img
}
return
}
func (a *Article) SetImgs(imgs []Img) (err error) {
if err = a.Printer.WriteImgs(a.TitlePath, imgs); err == nil {
a.Imgs = imgs
}
return
}
/*func (a *Article) MarshalJSON() ([]byte, error) {
return []byte(`{ "titlePath": "bob" }`), nil
}*/