This repository was 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
Expand file tree
/
Copy pathdatarepo.go
More file actions
151 lines (132 loc) · 3.24 KB
/
datarepo.go
File metadata and controls
151 lines (132 loc) · 3.24 KB
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
package core
import (
"database/sql"
"fmt"
"github.com/datatogether/sql_datastore"
"github.com/datatogether/sqlutil"
"github.com/ipfs/go-datastore"
"github.com/pborman/uuid"
"time"
)
// DataRepo is a place that holds data in a structured format
type DataRepo struct {
// version 4 uuid
Id string
// Created timestamp rounded to seconds in UTC
Created time.Time `json:"created"`
// Updated timestamp rounded to seconds in UTC
Updated time.Time `json:"updated"`
// Title of this data repository
Title string `json:"title"`
// Human-readable description
Description string `json:"description"`
// Main url link to the DataRepository
Url string `json:"url"`
}
func (d *DataRepo) DatastoreType() string {
return "DataRepo"
}
func (d *DataRepo) GetId() string {
return d.Id
}
func (d *DataRepo) Key() datastore.Key {
return datastore.NewKey(fmt.Sprintf("%s:%s", d.DatastoreType(), d.GetId()))
}
// Read dataRepo from db
func (d *DataRepo) Read(store datastore.Datastore) error {
di, err := store.Get(d.Key())
if err != nil {
return err
}
got, ok := di.(*DataRepo)
if !ok {
return ErrInvalidResponse
}
*d = *got
return nil
}
// Save a dataRepo
func (d *DataRepo) Save(store datastore.Datastore) (err error) {
var exists bool
if d.Id != "" {
exists, err = store.Has(d.Key())
if err != nil {
return err
}
}
if !exists {
d.Id = uuid.New()
d.Created = time.Now().Round(time.Second)
d.Updated = d.Created
} else {
d.Updated = time.Now().Round(time.Second)
}
return store.Put(d.Key(), d)
}
// Delete a dataRepo, should only do for erronious additions
func (d *DataRepo) Delete(store datastore.Datastore) error {
return store.Delete(d.Key())
}
func (d *DataRepo) NewSQLModel(key datastore.Key) sql_datastore.Model {
return &DataRepo{
Id: key.Name(),
}
}
func (d DataRepo) SQLQuery(cmd sql_datastore.Cmd) string {
switch cmd {
case sql_datastore.CmdCreateTable:
return qDataRepoCreateTable
case sql_datastore.CmdExistsOne:
return qDataRepoExists
case sql_datastore.CmdSelectOne:
return qDataRepoById
case sql_datastore.CmdInsertOne:
return qDataRepoInsert
case sql_datastore.CmdUpdateOne:
return qDataRepoUpdate
case sql_datastore.CmdDeleteOne:
return qDataRepoDelete
case sql_datastore.CmdList:
return qDataRepos
default:
return ""
}
}
func (d DataRepo) SQLParams(cmd sql_datastore.Cmd) []interface{} {
switch cmd {
case sql_datastore.CmdSelectOne, sql_datastore.CmdExistsOne, sql_datastore.CmdDeleteOne:
return []interface{}{d.Id}
default:
return []interface{}{
d.Id,
d.Created.In(time.UTC),
d.Updated.In(time.UTC),
d.Title,
d.Description,
d.Url,
}
}
}
// UnmarshalSQL reads an sql response into the dataRepo receiver
// it expects the request to have used dataRepoCols() for selection
func (d *DataRepo) UnmarshalSQL(row sqlutil.Scannable) (err error) {
var (
id, title, description, url string
created, updated time.Time
)
if err := row.Scan(&id, &created, &updated, &title, &description, &url); err != nil {
if err == sql.ErrNoRows {
return ErrNotFound
}
return err
}
*d = DataRepo{
Id: id,
Created: created.In(time.UTC),
Updated: updated.In(time.UTC),
Title: title,
Description: description,
Url: url,
}
return nil
}