-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_hoop.go
149 lines (123 loc) · 3.09 KB
/
data_hoop.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
package main
import (
"database/sql"
"log"
"time"
)
type Hoop struct {
ID int64 `json:"id"`
UserID int64 `json:"user_id"`
User User `json:"user"`
Name string `json:"name"`
Description string `json:"description"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Data map[string]interface{} `json:"data,omitempty"`
}
func hoopExists(hoop *Hoop, fetch bool) (bool, *Hoop) {
if fetch {
if newHoop, err := getHoop(hoop.ID); err != nil {
log.Println(err)
return false, nil
} else {
*hoop = newHoop
}
return true, hoop
} else {
count := 0
if err := db.QueryRow(COUNT_HOOP_SQL, hoop.ID).Scan(&count); err != nil || count == 0 {
log.Println(err)
return false, nil
}
return true, nil
}
}
func getHoop(hoopID int64) (hoop Hoop, err error) {
if err = db.QueryRow(GET_HOOP_SQL, hoopID).Scan(
&hoop.ID,
&hoop.UserID,
&hoop.Name,
&hoop.Description,
&hoop.Latitude,
&hoop.Longitude,
&hoop.CreatedAt,
&hoop.UpdatedAt,
); err != nil {
return
}
if hoop.User, err = getUserByID(hoop.UserID); err != nil {
return
}
var featuredStory Story
if featuredStory, err = getFeaturedStory(hoop.ID); err != nil {
return
} else {
hoop.Data = map[string]interface{}{}
hoop.Data["featured_story"] = featuredStory
}
return
}
func getHoops(query string, args ...interface{}) (hoops []Hoop, err error) {
var rows *sql.Rows
if rows, err = db.Query(query, args...); err != nil {
return
}
for rows.Next() {
var hoop Hoop
if err = rows.Scan(
&hoop.ID,
&hoop.UserID,
&hoop.Name,
&hoop.Description,
&hoop.Latitude,
&hoop.Longitude,
&hoop.CreatedAt,
&hoop.UpdatedAt,
); err != nil {
return
}
if hoop.User, err = getUserByID(hoop.UserID); err != nil {
return
}
var featuredStory Story
if featuredStory, err = getFeaturedStory(hoop.ID); err != nil {
return
} else {
hoop.Data = map[string]interface{}{}
hoop.Data["featured_story"] = featuredStory
}
hoops = append(hoops, hoop)
}
return
}
func insertHoop(userID int64, name, description, imageURL string, latitude, longitude float64) error {
// Start Transaction
tx, err := db.Begin()
if err != nil {
return err
}
var hoopID, storyID int64
// Insert Hoop
if err := tx.QueryRow(INSERT_HOOP_SQL, userID, name, description, latitude, longitude).Scan(&hoopID); err != nil {
return err
}
// Insert Story
if err := tx.QueryRow(INSERT_STORY_SQL, hoopID, userID, name, description, imageURL).Scan(&storyID); err != nil {
return err
}
// Insert HoopFeaturedStory
if _, err := tx.Exec(INSERT_HOOP_FEATURED_STORY_SQL, hoopID, storyID); err != nil {
return err
}
// Insert Activity
if _, err := tx.Exec(INSERT_POST_HOOP_ACTIVITY_SQL, userID, ACTIVITY_POST_HOOP, hoopID); err != nil {
return err
}
// End Transaction
if err := tx.Commit(); err != nil {
return err
}
return nil
}