Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
fix #72
Browse files Browse the repository at this point in the history
  • Loading branch information
pinpox committed Feb 15, 2021
1 parent cd12450 commit 07725e5
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 14 deletions.
5 changes: 3 additions & 2 deletions bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand All @@ -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,
Expand Down
24 changes: 16 additions & 8 deletions call.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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"))
Expand All @@ -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)
Expand All @@ -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")
}
Expand All @@ -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
}

Expand Down
14 changes: 13 additions & 1 deletion handler_person.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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")
Expand Down
8 changes: 7 additions & 1 deletion handler_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions person.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 07725e5

Please sign in to comment.