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
|
package main
import (
"database/sql"
"flag"
"fmt"
"log"
"os"
"time"
_ "github.com/lib/pq"
)
// Database connection details
const (
host = "postgres.banded-neon.ts.net"
user = "postgresql"
dbname = "journal"
)
var db *sql.DB
func init() {
var err error
// Connection string
psqlInfo := fmt.Sprintf("host=%s user=%s dbname=%s sslmode=disable", host, user, dbname)
db, err = sql.Open("postgres", psqlInfo)
if err != nil {
log.Fatal(err)
}
if err = db.Ping(); err != nil {
log.Fatal(err)
}
}
func addMODContact(firstName, lastName string) {
var contactId int
query := `INSERT INTO contacts (first_name, last_name, contact_type) VALUES ($1, $2, 3) RETURNING id`
err := db.QueryRow(query, firstName, lastName).Scan(&contactId)
if err != nil {
log.Fatal(err)
}
fmt.Printf("MOD contact added with ID: %d\n", contactId)
}
func addSleepEntry(entry string) {
_, err := db.Exec("INSERT INTO journal_entries (entry, type) VALUES ($1, 4)", entry)
if err != nil {
log.Fatal(err)
}
}
func addPersonalEntry(entry string) {
_, err := db.Exec("INSERT INTO journal_entries (entry, type) VALUES ($1, 2)", entry)
if err != nil {
log.Fatal(err)
}
}
func addMODEntry(entry string) {
_, err := db.Exec("INSERT INTO journal_entries (entry, type) VALUES ($1, 1)", entry)
if err != nil {
log.Fatal(err)
}
}
func listPersonalEntries(orientation string) {
var rows *sql.Rows
var id sql.NullInt64
var dateAdded sql.NullTime
var entry sql.NullString
var comment sql.NullString
rows, err := db.Query("SELECT id, date_added, entry, comment FROM journal_entries WHERE type = 2 ORDER BY date_added ASC, id ASC")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
fmt.Println("Personal Entries:")
var previousDateAdded sql.NullTime // Track the previous date only
previousDateAdded.Time = time.Time{} // Initialize to zero value
// Scan for each row
for rows.Next() {
if err := rows.Scan(&id, &dateAdded, &entry, &comment); err != nil {
log.Fatal(err)
}
// Check if previousDateAdded is valid to avoid printing dash before first entry
if previousDateAdded.Valid && dateAdded.Valid && !sameDay(previousDateAdded.Time, dateAdded.Time) {
fmt.Println("----") // Print a dashed line between different day entries
}
if orientation == "vertical" {
formatVertically(id, dateAdded, entry, comment)
} else if orientation == "horizontal" {
formatHorizontally(dateAdded, entry, comment)
}
previousDateAdded = dateAdded // Update previousDateAdded to current date
}
}
// Function to determine if two times are on the same day
func sameDay(t1, t2 time.Time) bool {
return t1.Year() == t2.Year() && t1.YearDay() == t2.YearDay()
}
func formatVertically(id sql.NullInt64, dateAdded sql.NullTime, entry sql.NullString, comment sql.NullString) {
fmt.Printf("ID: %s\n", formatInt64(id))
fmt.Printf("Date Added: %s\n", formatTime(dateAdded))
fmt.Printf("Entry: %s\n", formatString(entry))
fmt.Printf("Comment: %s\n", formatString(comment))
}
func formatHorizontally(dateAdded sql.NullTime, entry sql.NullString, comment sql.NullString) {
fmt.Printf("%s %s | %s | %s\n", formatShortTime(dateAdded), getWeekdayFromDate(dateAdded), formatString(entry), formatString(comment))
}
func getWeekdayFromDate(date sql.NullTime) string {
return date.Time.Format("Monday")
}
func formatShortTime(t sql.NullTime) string {
if t.Valid {
return t.Time.Format("2006-01-02 15:04")
}
return "NULL"
}
// Helper functions to handle sql.Null* types
func formatInt64(v sql.NullInt64) string {
if v.Valid {
return fmt.Sprintf("%d", v.Int64)
}
return "NULL"
}
func formatTime(v sql.NullTime) string {
if v.Valid {
return v.Time.Format(time.RFC3339)
}
return "NULL"
}
func formatString(v sql.NullString) string {
if v.Valid {
return v.String
}
return "NULL"
}
func main() {
// Define flags
addMODFlag := flag.Bool("addMOD", false, "Add a MOD contact with first name and last name")
firstName := flag.String("firstName", "", "First name of the contact")
lastName := flag.String("lastName", "", "Last name of the contact")
orientationFlag := flag.String("orientation", "horizontal", "Orientation of the journal entries (vertical or horizontal)")
addSleepEntryFlag := flag.Bool("addSleepEntry", false, "Add a sleep entry")
sleepEntry := flag.String("sleepEntry", "", "Content of the sleep entry")
addPersonalEntryFlag := flag.Bool("p", false, "Add a personal entry")
entry := flag.String("entry", "", "Content of the journal entry")
addMODEntryFlag := flag.Bool("addMODEntry", false, "Add a MOD entry")
listPersonalEntriesFlag := flag.Bool("l", false, "List all personal entries")
// Parse flags
flag.Parse()
// Call functions based on flags
if *addMODFlag {
if *firstName == "" || *lastName == "" {
log.Fatal("Both firstName and lastName must be provided for adding a MOD contact")
}
addMODContact(*firstName, *lastName)
} else if *addSleepEntryFlag {
if *sleepEntry == "" {
log.Fatal("Sleep entry content must be provided")
}
addSleepEntry(*sleepEntry)
} else if *addPersonalEntryFlag {
if *entry == "" {
log.Fatal("Personal entry content must be provided - use the -entry flag")
}
addPersonalEntry(*entry)
} else if *addMODEntryFlag {
if *entry == "" {
log.Fatal("MOD entry content must be provided - use the -entry flag")
}
addMODEntry(*entry)
} else if *listPersonalEntriesFlag {
listPersonalEntries(*orientationFlag)
} else {
flag.Usage() // Print usage if no valid flags are provided
os.Exit(1)
}
}
|