This repository has been archived by the owner on Nov 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection.go
168 lines (148 loc) · 3.92 KB
/
collection.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
package core
import (
"database/sql"
"fmt"
"github.com/datatogether/sql_datastore"
"github.com/datatogether/sqlutil"
datastore "github.com/ipfs/go-datastore"
"github.com/pborman/uuid"
"time"
)
// Collections are generic groupings of content
// collections can be thought of as a csv file listing content hashes
// as the first column, and whatever other information is necessary in
// subsequent columns
type Collection struct {
// version 4 uuid
Id string `json:"id"`
// Created timestamp rounded to seconds in UTC
Created time.Time `json:"created"`
// Updated timestamp rounded to seconds in UTC
Updated time.Time `json:"updated"`
// sha256 multihash of the public key that created this collection
Creator string `json:"creator"`
// human-readable title of the collection
Title string `json:"title"`
// description of the collection
Description string `json:"description"`
// url this collection originates from
Url string `json:"url,omitempty"`
}
func (c Collection) DatastoreType() string {
return "Collection"
}
func (c Collection) GetId() string {
return c.Id
}
func (c Collection) Key() datastore.Key {
return datastore.NewKey(fmt.Sprintf("%s:%s", c.DatastoreType(), c.GetId()))
}
// Read collection from db
func (c *Collection) Read(store datastore.Datastore) error {
if c.Id == "" && c.Url != "" {
// TODO - figure out a way to query stores by url...
if sqlstore, ok := store.(*sql_datastore.Datastore); ok {
row := sqlstore.DB.QueryRow(qCollectionByUrl, c.Url)
return c.UnmarshalSQL(row)
}
}
ci, err := store.Get(c.Key())
if err != nil {
return err
}
got, ok := ci.(*Collection)
if !ok {
return ErrInvalidResponse
}
*c = *got
return nil
}
// Save a collection
func (c *Collection) Save(store datastore.Datastore) (err error) {
var exists bool
if c.Id != "" {
exists, err = store.Has(c.Key())
if err != nil {
return err
}
}
if !exists {
c.Id = uuid.New()
c.Created = time.Now().Round(time.Second)
c.Updated = c.Created
} else {
c.Updated = time.Now().Round(time.Second)
}
return store.Put(c.Key(), c)
}
// Delete a collection, should only do for erronious additions
func (c *Collection) Delete(store datastore.Datastore) error {
return store.Delete(c.Key())
}
func (c *Collection) NewSQLModel(key datastore.Key) sql_datastore.Model {
return &Collection{
Id: key.Name(),
}
}
func (c Collection) SQLQuery(cmd sql_datastore.Cmd) string {
switch cmd {
case sql_datastore.CmdCreateTable:
return qCollectionCreateTable
case sql_datastore.CmdExistsOne:
return qCollectionExists
case sql_datastore.CmdSelectOne:
return qCollectionById
case sql_datastore.CmdInsertOne:
return qCollectionInsert
case sql_datastore.CmdUpdateOne:
return qCollectionUpdate
case sql_datastore.CmdDeleteOne:
return qCollectionDelete
case sql_datastore.CmdList:
return qCollections
default:
return ""
}
}
func (c *Collection) SQLParams(cmd sql_datastore.Cmd) []interface{} {
switch cmd {
case sql_datastore.CmdSelectOne, sql_datastore.CmdExistsOne, sql_datastore.CmdDeleteOne:
return []interface{}{c.Id}
case sql_datastore.CmdList:
return nil
default:
return []interface{}{
c.Id,
c.Created.In(time.UTC),
c.Updated.In(time.UTC),
c.Creator,
c.Title,
c.Description,
c.Url,
}
}
}
// UnmarshalSQL reads an sql response into the collection receiver
// it expects the request to have used collectionCols() for selection
func (c *Collection) UnmarshalSQL(row sqlutil.Scannable) (err error) {
var (
id, creator, title, description, url string
created, updated time.Time
)
if err := row.Scan(&id, &created, &updated, &creator, &title, &description, &url); err != nil {
if err == sql.ErrNoRows {
return ErrNotFound
}
return err
}
*c = Collection{
Id: id,
Created: created.In(time.UTC),
Updated: updated.In(time.UTC),
Creator: creator,
Title: title,
Description: description,
Url: url,
}
return nil
}