-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiary.go
More file actions
56 lines (48 loc) · 1.19 KB
/
diary.go
File metadata and controls
56 lines (48 loc) · 1.19 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
package main
import (
"database/sql"
"fmt"
"log"
"strings"
)
type DiaryEntry struct {
text string
date string
}
type diarySlice []*DiaryEntry
func (text diarySlice) DiaryString() string {
var s []string
for _, u := range text {
if u != nil {
s = append(s, fmt.Sprintf("Eintrag: %s Datum: %s", u.text, u.date))
}
}
return strings.Join(s, "\n")
}
func GetAllDiaryEntries(db *sql.DB) string {
sqlStatement := `SELECT * FROM diary`
entries := make([]*DiaryEntry, 0)
rows, err := db.Query(sqlStatement)
if err != nil {
log.Println("Error getting all diary entries: ", err)
}
for rows.Next() {
entry := new(DiaryEntry)
_ = rows.Scan(&entry.text, &entry.date)
entries = append(entries, entry)
}
fmt.Println(entries)
return (diarySlice(entries).DiaryString())
}
func InsertNewDiaryEntry(db *sql.DB, message string) {
log.Println("Message text: ", message)
unfiltered_message := strings.Replace(message, "/neuerEintrag ", "", -1)
entry := strings.Split(unfiltered_message, ",")
entry_text := entry[0]
entry_date := entry[1]
sqlStatement := `INSERT INTO diary (text, date) VALUES (?, ?)`
_, err := db.Exec(sqlStatement, entry_text, entry_date)
if err != nil {
panic(err)
}
}