/
tripbasicdb.go
298 lines (266 loc) · 6.56 KB
/
tripbasicdb.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package database
import (
"context"
"strconv"
"time"
"github.com/binhonglee/GlobeTrotte/src/turbine/logger"
"github.com/binhonglee/GlobeTrotte/src/turbine/wings"
"github.com/jackc/pgtype"
"github.com/jackc/pgx/v4"
)
type TripExtra struct {
ID int
UserID int
TimeCreated time.Time
LastUpdated time.Time
}
// AddTripDB - Adding new trip into the database.
func AddTripDB(trip wings.TripBasic, userID int) int {
user, _ := GetUserBasicDBWithID(userID)
if user.ID == -1 {
logger.Print(
logger.Database,
"User adding the new trip is not found.",
)
return -1
}
newTripID := addTripBasic(trip, userID)
if AddTripToUserDB(newTripID, user) {
return newTripID
}
return failAddingTripToUser(newTripID)
}
func GetTripBasicWithID(id int) (wings.TripBasic, TripExtra) {
return fetchTripBasic(id)
}
// UpdateTripDB - Update trip information back into the database.
func UpdateTripDB(trip wings.TripBasic, userID int) bool {
_, extra := fetchTripBasic(trip.ID)
if extra.UserID != userID {
logger.Print(
logger.Database,
"Update request comes from a different user than the original trip owner",
)
return false
}
return updateTripBasic(trip)
}
// DeleteTripDB - Delete trip from the database.
func DeleteTripDB(trip wings.TripBasic) bool {
t, _ := GetTripBasicWithID(trip.GetID())
if t.ID == -1 {
return false
}
return deleteTripWithID(
trip.GetID(),
)
}
// SearchTripsDB - Search for trip from the database
func SearchTripsDB(
cities []wings.City, days int, query string,
) ([]wings.TripBasic, []TripExtra) {
toReturnTripBasic := make([]wings.TripBasic, 0)
toReturnTripExtra := make([]TripExtra, 0)
args := make([]interface{}, 0)
sqlStatement := "SELECT id FROM trips WHERE CARDINALITY(days) > 0"
count := 1
if days > 0 {
sqlStatement += " AND CARDINALITY(days) = $" + strconv.Itoa(count)
args = append(args, days)
count++
}
if len(cities) > 0 {
sqlStatement += " AND ("
for i, city := range cities {
if i > 0 {
sqlStatement += " OR "
}
sqlStatement += "$" + strconv.Itoa(count) + " = ANY(cities)"
args = append(args, int(city))
count++
}
sqlStatement += ")"
}
if len(query) > 0 {
sqlStatement += " AND name ILIKE $" + strconv.Itoa(count) + ""
args = append(args, "%"+query+"%")
count++
}
sqlStatement += " ORDER BY last_updated DESC;"
c := getConn()
rows, err := c.Query(context.Background(), sqlStatement, args...)
defer c.Close()
logger.Err(logger.Database, err, "")
defer rows.Close()
for rows.Next() {
var id int
err = rows.Scan(&id)
logger.Err(logger.Database, err, "")
tripBasic, extra := fetchTripBasic(int(id))
toReturnTripBasic = append(toReturnTripBasic, tripBasic)
toReturnTripExtra = append(toReturnTripExtra, extra)
}
return toReturnTripBasic, toReturnTripExtra
}
// GetRecentTripsDB - Get most recent public trips from database
func GetRecentTripsDB() ([]wings.TripBasic, []TripExtra) {
toReturnTrips := make([]wings.TripBasic, 0)
toReturnExtra := make([]TripExtra, 0)
sqlStatement := `
SELECT id
FROM trips
WHERE
private = FALSE
AND CARDINALITY(days) > 0
ORDER BY last_updated DESC
LIMIT 10;
`
c := getConn()
rows, err := c.Query(context.Background(), sqlStatement)
defer c.Close()
logger.Err(logger.Database, err, "")
defer rows.Close()
for rows.Next() {
var id int
err = rows.Scan(&id)
logger.Err(logger.Database, err, "")
tripBasic, tripExtra := fetchTripBasic(int(id))
toReturnTrips = append(toReturnTrips, tripBasic)
toReturnExtra = append(toReturnExtra, tripExtra)
}
return toReturnTrips, toReturnExtra
}
func fetchTripBasic(id int) (wings.TripBasic, TripExtra) {
var trip wings.TripBasic
var extra TripExtra
var days pgtype.Int4Array
var cities pgtype.Int4Array
sqlStatement := `
SELECT id, userid, name, private, cities, description, days, time_created, last_updated
FROM trips WHERE id=$1;`
c := getConn()
row := c.QueryRow(
context.Background(),
sqlStatement,
id,
)
switch err := row.Scan(
&trip.ID,
&extra.UserID,
&trip.Name,
&trip.Private,
&cities,
&trip.Description,
&days,
&extra.TimeCreated,
&extra.LastUpdated,
); err {
case pgx.ErrNoRows:
logger.Print(logger.Database, "Trip not found.")
trip.ID = -1
default:
logger.Err(logger.Database, err, "")
}
defer c.Close()
extra.ID = trip.ID
trip.Cities = cityIDsToParsedCities(int64V(cities))
trip.Days = fetchDays(int64V(days))
return trip, extra
}
func addTripBasic(newTrip wings.TripBasic, userID int) int {
for index, day := range newTrip.Days {
newTrip.Days[index].ID = addDay(day)
}
sqlStatement := `
INSERT INTO trips (userid, name, private, cities, description, days, time_created, last_updated)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
RETURNING id;`
id := 0
c := getConn()
err := c.QueryRow(
context.Background(),
sqlStatement,
userID,
newTrip.Name,
newTrip.Private,
parsedCitiesToIDs(newTrip.Cities),
newTrip.Description,
daysToIDArray(newTrip.Days),
time.Now(),
time.Now(),
).Scan(&id)
defer c.Close()
if err != nil {
logger.Err(logger.Database, err, "")
return -1
}
logger.Print(
logger.Database,
"New trip ID is: "+strconv.Itoa(id),
)
return id
}
func updateTripBasic(updatedTrip wings.TripBasic) bool {
existingTrip, _ := fetchTripBasic(updatedTrip.GetID())
if existingTrip.GetID() != updatedTrip.GetID() {
logger.Print(
logger.Database,
"Existing Trip is not found. Aborting update.",
)
logger.Print(
logger.Database,
"Given ID is "+strconv.Itoa(updatedTrip.GetID())+
" but found ID is "+strconv.Itoa(existingTrip.GetID())+
" instead.")
return false
}
for index, day := range updatedTrip.Days {
if updateDay(&day, true) {
updatedTrip.Days[index] = day
} else {
logger.Failure(
logger.Database,
"Failed to update a day in trip "+strconv.Itoa(updatedTrip.ID),
)
return false
}
}
for _, dayID := range NotExists(
daysToIDArray(existingTrip.Days),
daysToIDArray(updatedTrip.Days),
) {
deleteDayWithID(dayID)
}
// TODO: Cleanup days and places that are no longer attached to the day
sqlStatement := `
UPDATE trips
SET name = $2,
private = $3,
description = $4,
cities = $5,
days = $6,
last_updated = $7
WHERE id = $1;`
c := getConn()
_, err := c.Exec(
context.Background(),
sqlStatement,
updatedTrip.ID,
updatedTrip.Name,
updatedTrip.Private,
updatedTrip.Description,
parsedCitiesToIDs(updatedTrip.Cities),
daysToIDArray(updatedTrip.Days),
time.Now(),
)
defer c.Close()
if err != nil {
logger.Err(
logger.Database,
err,
"Failed to update trip "+strconv.Itoa(updatedTrip.ID),
)
return false
}
return true
}