-
Notifications
You must be signed in to change notification settings - Fork 0
/
reading-lists.go
329 lines (258 loc) · 9.51 KB
/
reading-lists.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
)
type ReadingList struct {
Title string `json:"title"`
Members []string `json:"members"`
Books []string `json:"books"`
}
func readReadingLists() []ReadingList {
data, err := ioutil.ReadFile(fmt.Sprintf("%s/reading-lists.json", getCallDirectory()))
if err != nil {
panic(err)
}
var readinglists []ReadingList
err = json.Unmarshal(data, &readinglists)
if err != nil {
panic(err)
}
return readinglists
}
func getReadingListIndex(reading_list_title string) int {
var reading_lists []ReadingList = readReadingLists()
for i := 0; i < len(reading_lists); i++ {
if compareStringsCaseInsensitive(reading_lists[i].Title, reading_list_title) {
return i
}
}
return -1
}
func printReadingList(reading_list *ReadingList, verbose bool, vverbose bool) {
colorPrintField("Title", (*reading_list).Title)
colorPrintField("Members", strings.Join((*reading_list).Members[:], ", "))
var books []Book = readBooks()
colorPrintField("Books", "")
if len((*reading_list).Books) == 0 {
fmt.Println("NO BOOKS IN READING LIST.")
return
}
fmt.Println("-------------------------------------------------------------")
fmt.Println("-------------------------------------------------------------")
for i := 0; i < len((*reading_list).Books); i++ {
for j := 0; j < len(books); j++ {
if compareStringsCaseInsensitive(books[j].Title, (*reading_list).Books[i]) {
printBook(&books[j], true, verbose, vverbose)
break
}
}
}
fmt.Println("-------------------------------------------------------------")
}
func listReadingLists(verbose bool, vverbose bool, limit int) {
var reading_lists []ReadingList = readReadingLists()
var until = len(reading_lists)
if limit != 0 {
until = limit
}
for i := 0; i < until; i++ {
printReadingList(&reading_lists[i], verbose, vverbose)
}
}
func writeReadingLists(reading_lists *[]ReadingList) {
gitPullOrigin(true)
dataBytes, err := json.Marshal((*reading_lists))
if err != nil {
panic(err)
}
err = ioutil.WriteFile(fmt.Sprintf("%s/reading-lists.json", getCallDirectory()), dataBytes, 0644)
if err != nil {
panic(err)
}
gitCommit("reading-lists.json", "Edit reading-lists.json")
gitPush()
}
func appendReadingList(readinglist *ReadingList) {
var readinglists []ReadingList = readReadingLists()
readinglists = append(readinglists, *readinglist)
writeReadingLists(&readinglists)
}
func addReadingList() {
title := getInput("Title: ")
var reading_list_index = getReadingListIndex(title)
if reading_list_index != -1 {
fmt.Printf("Reading list already exists with title: \"%s\".\n", title)
return
}
username := getInput("Username: ")
var user_index int = getUserIndex(username)
if user_index == -1 {
fmt.Printf("No user found with username: %s\n", username)
}
var users []User = readUsers()
members := []string{users[user_index].Username}
var new_reading_list *ReadingList = &ReadingList{
Title: title,
Members: members,
Books: nil,
}
appendReadingList(new_reading_list)
}
func updateReadingListTitle(reading_list_title, new_title string) {
var readinglists []ReadingList = readReadingLists()
var updated_reading_list_index = getReadingListIndex(new_title)
if updated_reading_list_index != -1 {
fmt.Printf("Reading list already exists with title: \"%s\".\n", new_title)
return
}
var reading_list_index = getReadingListIndex(reading_list_title)
if reading_list_index != -1 {
readinglists[reading_list_index].Title = new_title
writeReadingLists(&readinglists)
} else {
fmt.Printf("Can't find reading list with title: \"%s\".\n", reading_list_title)
}
}
func runUpdateReadingList() {
reading_list_title := getInput("Reading List Title: ")
field := getInput("Field: ")
value := getInput("New Value: ")
switch strings.ToLower(field) {
case "title":
updateReadingListTitle(reading_list_title, value)
default:
fmt.Println("Options are title.")
}
}
func addBookToReadingList() {
title := getInput("Reading List Title: ")
book_title := getInput("Book Title: ")
var readinglists []ReadingList = readReadingLists()
var readinglist_index int = getReadingListIndex(title)
if readinglist_index == -1 {
fmt.Printf("No reading list with title: %s/n", title)
return
}
var book_index int = getBookIndex(book_title)
if book_index == -1 {
fmt.Printf("No book found with title: %s\n", book_title)
return
}
var book_already_in_list bool
for i := 0; i < len(readinglists[readinglist_index].Books); i++ {
if readinglists[readinglist_index].Books[i] == book_title {
book_already_in_list = true
}
}
if book_already_in_list != false {
fmt.Printf("Book with title: \"%s\" already exists in reading list \"%s\".\n", book_title, title)
return
}
readinglists[readinglist_index].Books = append(readinglists[readinglist_index].Books, book_title)
writeReadingLists(&readinglists)
}
func addMemberToReadingList() {
title := getInput("Reading List Title: ")
new_member := getInput("New Member Username: ")
var readinglists []ReadingList = readReadingLists()
var users []User = readUsers()
var readinglist_index int = getReadingListIndex(title)
if readinglist_index == -1 {
fmt.Printf("No reading list with title: \"%s\".\n", title)
return
}
var user_index int = getUserIndex(new_member)
if user_index == -1 {
fmt.Printf("No user found with username: \"%s\".\n", new_member)
return
}
var user_already_in_list bool
for i := 0; i < len(readinglists[readinglist_index].Members); i++ {
if readinglists[readinglist_index].Members[i] == new_member {
user_already_in_list = true
}
}
if user_already_in_list != false {
fmt.Printf("User with title: \"%s\" already exists in reading list \"%s\".\n", new_member, title)
return
}
readinglists[readinglist_index].Members = append(readinglists[readinglist_index].Members, users[user_index].Username)
users[user_index].ReadingLists = append(users[user_index].ReadingLists, title)
writeReadingLists(&readinglists)
writeUsers(&users)
}
func deleteBookFromReadingList() {
title := getInput("Reading List Title: ")
book_title := getInput("Book Title: ")
var readinglists []ReadingList = readReadingLists()
var reading_list_index int = getReadingListIndex(title)
if reading_list_index == -1 {
fmt.Printf("No reading list with title: \"%s\".\n", title)
return
}
var book_exists int = getBookIndex(book_title)
if book_exists == -1 {
fmt.Printf("No book found with title: \"%s\".\n", book_title)
return
}
for i := 0; i < len(readinglists[reading_list_index].Books); i++ {
if compareStringsCaseInsensitive(readinglists[reading_list_index].Books[i], book_title) {
readinglists[reading_list_index].Books = append(readinglists[reading_list_index].Books[:i], readinglists[reading_list_index].Books[i+1:]...)
break
}
}
writeReadingLists(&readinglists)
}
func deleteMemberFromReadingList() {
title := getInput("Reading List Title: ")
member := getInput("Member Username: ")
var readinglists []ReadingList = readReadingLists()
var users []User = readUsers()
var i int = getReadingListIndex(title)
if i == -1 {
fmt.Printf("No reading list with title: \"%s\".\n", title)
return
}
var user_index int = getUserIndex(member)
if user_index == -1 {
fmt.Printf("No user found with username: \"%s\".\n", member)
return
}
for j := 0; j < len(readinglists[i].Members); j++ {
if users[user_index].Username == readinglists[i].Members[j] {
readinglists[i].Members = append(readinglists[i].Members[:j], readinglists[i].Members[j+1:]...)
break
}
}
for j := 0; j < len(users[user_index].ReadingLists); j++ {
if users[user_index].ReadingLists[j] == title {
users[user_index].ReadingLists = append(users[user_index].ReadingLists[:j], users[user_index].ReadingLists[j+1:]...)
break
}
}
writeReadingLists(&readinglists)
writeUsers(&users)
}
func deleteReadingList(reading_list_title string) {
var reading_lists []ReadingList = readReadingLists()
var users []User = readUsers()
var reading_list_index int = getReadingListIndex(reading_list_title)
if reading_list_index == -1 {
fmt.Printf("No reading list with title: \"%s\".\n", reading_list_title)
return
}
for i := 0; i < len(users); i++ {
for j := 0; j < len(users[i].ReadingLists); j++ {
if users[i].ReadingLists[j] == reading_list_title {
users[i].ReadingLists = append(users[i].ReadingLists[:j], users[i].ReadingLists[j+1:]...)
break
}
}
}
reading_lists = append(reading_lists[:reading_list_index], reading_lists[reading_list_index+1:]...)
writeReadingLists(&reading_lists)
writeUsers(&users)
}