This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_person.go
107 lines (88 loc) · 2.58 KB
/
handler_person.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
package main
import (
"io"
"net/http"
"strconv"
log "github.com/sirupsen/logrus"
)
func handlerAddPerson(w http.ResponseWriter, r *http.Request) {
tData := TmplData{
CurrentUser: contextString(contextKeyCurrentUser, r),
}
// GET requests show import page
if r.Method == http.MethodGet {
persons, err := bridge.GetPersons()
if err != nil {
log.Warn(err)
if _, err := io.WriteString(w, "Failed to retrieve persons"); err != nil {
log.Error(err)
}
return
}
tData.Persons = persons
if err := templates.ExecuteTemplate(w, "importPersons.html", tData); err != nil {
log.Error(err)
}
} else if r.Method == http.MethodPost {
if err := r.ParseForm(); err != nil {
tData.AppMessages = append(tData.AppMessages, "Ungültige Eingaben")
if err := templates.ExecuteTemplate(w, "importPersons.html", tData); err != nil {
log.Error(err)
}
return
}
data := r.Form
phone := data.Get("phone")
group := data.Get("group")
// Try to create new call from input data
groupNum, err := strconv.Atoi(group)
if err != nil {
log.Debug(err)
tData.AppMessages = append(tData.AppMessages, "Ungültige Gruppe")
if err := templates.ExecuteTemplate(w, "importPersons.html", tData); err != nil {
log.Error(err)
}
return
}
if phone == "" {
tData.AppMessages = append(tData.AppMessages, "Fehlende Rufnummer")
if err := templates.ExecuteTemplate(w, "importPersons.html", tData); err != nil {
log.Error(err)
}
return
}
person, err := NewPerson(0, groupNum, phone, false)
if err != nil {
log.Debug(err)
tData.AppMessages = append(tData.AppMessages, "Eingaben ungültig")
if err := templates.ExecuteTemplate(w, "importPersons.html", tData); err != nil {
log.Error(err)
}
return
}
// Add call to bridge
if err := bridge.AddPerson(person); err != nil {
log.Warn(err)
log.Warn(person)
tData.AppMessages = append(tData.AppMessages, "Personen konnten nicht gespeichert werden. Rufnummer schon vorhanden?")
if err := templates.ExecuteTemplate(w, "importPersons.html", tData); err != nil {
log.Error(err)
}
return
}
// Send onboarding notificatino
if err := bridge.sender.SendMessageOnboarding(person.Phone); err != nil {
log.Error(err)
}
log.Debug("Person was added")
tData.AppMessageSuccess = "Import Erfolgreich!"
tData.CurrentUser = contextString(contextKeyCurrentUser, r)
if err := templates.ExecuteTemplate(w, "importPersons.html", tData); err != nil {
log.Error(err)
}
} else {
if _, err := io.WriteString(w, "Invalid request"); err != nil {
log.Error(err)
}
}
}