/
post.go
80 lines (70 loc) · 1.92 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
r "gopkg.in/dancannon/gorethink.v1"
"log"
"time"
"errors"
)
const postingsTable = "POSTINGS"
var ErrPostNotFound = errors.New("Post Not Found")
type PostingFetcher interface {
Get(id string) (Posting, error)
}
type PostingCreator interface {
Create(post Posting) (id string, err error)
}
type PostingStore interface {
PostingFetcher
PostingCreator
}
func NewPostingStore(session *r.Session) PostingStore {
return &RethinkPosting{session: session}
}
type RethinkPosting struct {
session *r.Session
}
// Get fetches a posting. Make sure to check for err.
// returns ErrPostNotFound for empty result.
func (self *RethinkPosting) Get(id string) (Posting, error) {
post := Posting{}
res, err := r.Table(postingsTable).Get(id).Run(self.session)
if err != nil {
return post, err
}
if res.IsNil() {
return post, ErrPostNotFound
}
err = res.One(&post)
if err != nil {
return post, err
}
return post, nil
}
func (self *RethinkPosting) Create(post Posting) (string, error) {
wr, err := r.Table(postingsTable).Insert(&post).RunWrite(self.session)
if err != nil {
return "", err
}
log.Printf("%v", wr.GeneratedKeys)
return wr.GeneratedKeys[0], nil
}
type Posting struct {
Id string `gorethink:"id,omitempty"`
Title string `gorethink:"title"`
Description string `gorethink:"description,omitempty"`
Images Images `gorethink:"images,omitempty":json:images"` // B64 encoded images
Created time.Time `gorethink:"created,omitempty"`
Updated time.Time `gorethink:"updated,omitempty"`
}
type Content struct {
Created string `gorethink:"created,omitempty"`
Origin string `gorethink:"origin,omitempty"` // Creating system
Data string `gorethink:"id,omitempty"` //
}
type Image struct {
Content Contents `gorethink:"contents"`
Data string `gorethink:"data"`
Footprint string `gorethink:"footprint,omitempty"`
}
type Contents []Content
type Images []Image