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 pathcall.go
129 lines (105 loc) · 3.46 KB
/
call.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
package main
import (
"errors"
"net/url"
"strconv"
"time"
log "github.com/sirupsen/logrus"
)
// Call represents a call issued by a user. It contains data on the
// availability, times and location. Invitations will then be send out for that
// call
type Call struct {
ID int `db:"id"`
Title string `db:"title"`
CenterID int `db:"center_id"`
Capacity int `db:"capacity"`
TimeStart time.Time `db:"time_start"`
TimeEnd time.Time `db:"time_end"`
YoungOnly bool `db:"young_only"`
LocName string `db:"loc_name"`
LocStreet string `db:"loc_street"`
LocHouseNr string `db:"loc_housenr"`
LocPLZ string `db:"loc_plz"`
LocCity string `db:"loc_city"`
LocOpt string `db:"loc_opt"`
}
func todayAt(input string) (time.Time, error) {
now := time.Now()
year, month, day := now.Date()
tmp, err := time.Parse("15:4", input)
if err != nil {
return now, err
}
hour, min, _ := tmp.Clock()
return time.Date(year, month, day, hour, min, 0, 0, now.Location()), nil
}
// NewCall creates a new call
func NewCall(data url.Values) (Call, []string, error) {
var errorStrings []string
var retError error
// Validate capacity > 0
capacity, err := strconv.Atoi(data.Get("capacity"))
if err != nil || capacity < 1 {
errorStrings = append(errorStrings, "Ungültige Kapazität")
retError = err
}
// Validate start and end times make sense
log.Debug("start-time: ", data.Get("start-time"))
log.Debug("end-time: ", data.Get("end-time"))
timeStart, err := todayAt(data.Get("start-time"))
if err != nil {
errorStrings = append(errorStrings, "Ungültige Startzeit")
retError = err
}
timeEnd, err := todayAt(data.Get("end-time"))
if err != nil {
errorStrings = append(errorStrings, "Ungültige Endzezeit")
retError = err
}
if !timeStart.Before(timeEnd) {
errorStrings = append(errorStrings, "Endzezeit ist nicht nach Startzeit")
retError = err
}
// Get text fields and check that they are not empty strings
var locName, locStreet, locHouseNr, locPlz, locCity, locOpt, title string
var youngOnly bool
locName, errorStrings = getFormFieldWithErrors(data, "loc_name", errorStrings)
locStreet, errorStrings = getFormFieldWithErrors(data, "loc_street", errorStrings)
locHouseNr, errorStrings = getFormFieldWithErrors(data, "loc_housenr", errorStrings)
locPlz, errorStrings = getFormFieldWithErrors(data, "loc_plz", errorStrings)
locCity, errorStrings = getFormFieldWithErrors(data, "loc_city", errorStrings)
locOpt = data.Get("loc_opt")
title, errorStrings = getFormFieldWithErrors(data, "title", errorStrings)
if youngOnly, err = strconv.ParseBool(data.Get("young_only")); err != nil {
errorStrings = append(errorStrings, "Ungültige Angabe für Impfstoff")
retError = err
}
if len(errorStrings) != 0 {
retError = errors.New("Missing input data")
}
if err != nil {
retError = err
}
return Call{
Title: title,
CenterID: 0, //TODO set centerID from contextString
Capacity: capacity,
TimeStart: timeStart,
TimeEnd: timeEnd,
LocName: locName,
LocStreet: locStreet,
LocHouseNr: locHouseNr,
LocPLZ: locPlz,
LocCity: locCity,
LocOpt: locOpt,
YoungOnly: youngOnly,
}, errorStrings, retError
}
func getFormFieldWithErrors(data url.Values, formID string, errorStrings []string) (string, []string) {
value := data.Get(formID)
if value == "" {
errorStrings = append(errorStrings, "Ungültige Eingabe für: "+formID)
}
return value, errorStrings
}