diff --git a/bridge.go b/bridge.go index 2e7625d..e3fe24c 100644 --- a/bridge.go +++ b/bridge.go @@ -26,7 +26,7 @@ CREATE TABLE IF NOT EXISTS persons ( center_id INTEGER NOT NULL, group_num INTEGER NOT NULL, status INTEGER NOT NULL, - young INTEGER NOT NULL + age INTEGER NOT NULL ); ` var schemaCalls = ` @@ -37,7 +37,8 @@ CREATE TABLE IF NOT EXISTS calls ( capacity INTEGER NOT NULL, time_start DATETIME NOT NULL, time_end DATETIME NOT NULL, - young_only INTEGER NOT NULL, + age_min INTEGER NOT NULL, + age_max INTEGER NOT NULL, loc_name TEXT NOT NULL, loc_street TEXT NOT NULL, loc_housenr TEXT NOT NULL, diff --git a/call.go b/call.go index 72977e8..2f6ea26 100644 --- a/call.go +++ b/call.go @@ -19,7 +19,8 @@ type Call struct { Capacity int `db:"capacity"` TimeStart time.Time `db:"time_start"` TimeEnd time.Time `db:"time_end"` - YoungOnly bool `db:"young_only"` + AgeMin int `db:"age_min"` + AgeMax int `db:"age_max"` LocName string `db:"loc_name"` LocStreet string `db:"loc_street"` LocHouseNr string `db:"loc_housenr"` @@ -56,6 +57,18 @@ func NewCall(data url.Values) (Call, []string, error) { retError = err } + ageMin, err := strconv.Atoi(data.Get("age_min")) + if err != nil || ageMin < 0 { + errorStrings = append(errorStrings, "Ungültiges Mindestalter") + retError = err + } + + ageMax, err := strconv.Atoi(data.Get("age_max")) + if err != nil || ageMax > 200 { + errorStrings = append(errorStrings, "Ungültiges Höchstalter") + 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")) @@ -79,7 +92,6 @@ func NewCall(data url.Values) (Call, []string, error) { // 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) @@ -89,11 +101,6 @@ func NewCall(data url.Values) (Call, []string, error) { locOpt, errorStrings = getFormFieldWithErrors(data, "loc_opt", errorStrings) 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") } @@ -114,7 +121,8 @@ func NewCall(data url.Values) (Call, []string, error) { LocPLZ: locPlz, LocCity: locCity, LocOpt: locOpt, - YoungOnly: youngOnly, + AgeMin: ageMin, + AgeMax: ageMax, }, errorStrings, retError } diff --git a/handler_person.go b/handler_person.go index b8754ac..cb88182 100644 --- a/handler_person.go +++ b/handler_person.go @@ -44,6 +44,7 @@ func handlerAddPerson(w http.ResponseWriter, r *http.Request) { data := r.Form phone := data.Get("phone") group := data.Get("group") + age := data.Get("age") // Try to create new call from input data groupNum, err := strconv.Atoi(group) @@ -57,6 +58,17 @@ func handlerAddPerson(w http.ResponseWriter, r *http.Request) { return } + ageNum, err := strconv.Atoi(age) + + if err != nil { + log.Debug(err) + tData.AppMessages = append(tData.AppMessages, "Ungültiges Alter") + 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 { @@ -65,7 +77,7 @@ func handlerAddPerson(w http.ResponseWriter, r *http.Request) { return } - person, err := NewPerson(0, groupNum, phone, false) + person, err := NewPerson(0, groupNum, phone, false, ageNum) if err != nil { log.Debug(err) tData.AppMessages = append(tData.AppMessages, "Eingaben ungültig") diff --git a/handler_upload.go b/handler_upload.go index d14c1f7..1e15b29 100644 --- a/handler_upload.go +++ b/handler_upload.go @@ -61,9 +61,15 @@ func handlerUpload(w http.ResponseWriter, r *http.Request) { return } + ageNum, err := strconv.Atoi(record[2]) + if err != nil { + log.Warn(err) + return + } + // Try to create a new persion object from the data and return on // errors - p, err := NewPerson(0, groupNum, record[1], false) + p, err := NewPerson(0, groupNum, record[1], false, ageNum) if err != nil { log.Warn(err) return diff --git a/person.go b/person.go index 1a4f2ef..0713a93 100644 --- a/person.go +++ b/person.go @@ -16,17 +16,18 @@ type Person struct { CenterID int `db:"center_id"` // ID of center that added this person Group int `db:"group_num"` // Vaccination group Status bool `db:"status"` // Vaccination status - Young bool `db:"young"` // Can be vacciated with any serum + Age int `db:"age"` // Age of the person, to determine compatible vaccines } // NewPerson receives the input data and returns a slice of person objects. For // single import this will just be an array with a single entry, for CSV upload // it may be longer. -func NewPerson(centerID, group int, phone string, status bool) (Person, error) { +func NewPerson(centerID, group int, phone string, status bool, age int) (Person, error) { person := Person{ CenterID: centerID, Status: status, + Age: age, } num, err := libphonenumber.Parse(phone, "DE")