diff --git a/atsebayt/src/components/SurveyAdmin.svelte b/atsebayt/src/components/SurveyAdmin.svelte index b12c8f3..dc28e7d 100644 --- a/atsebayt/src/components/SurveyAdmin.svelte +++ b/atsebayt/src/components/SurveyAdmin.svelte @@ -51,6 +51,15 @@ +
+
+ +
+
+ +
+
+
diff --git a/atsebayt/src/lib/surveys.js b/atsebayt/src/lib/surveys.js index d1b6702..88d1669 100644 --- a/atsebayt/src/lib/surveys.js +++ b/atsebayt/src/lib/surveys.js @@ -8,10 +8,11 @@ class Survey { } } - update({ id, title, promo, shown, corrected, start_availability, end_availability }) { + update({ id, title, promo, group, shown, corrected, start_availability, end_availability }) { this.id = id; this.title = title; this.promo = promo; + this.group = group; this.shown = shown; this.corrected = corrected; if (this.start_availability != start_availability) { diff --git a/db.go b/db.go index 1c52d66..de129e2 100644 --- a/db.go +++ b/db.go @@ -82,6 +82,7 @@ CREATE TABLE IF NOT EXISTS surveys( id_survey INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, title VARCHAR(255), promo MEDIUMINT NOT NULL, + grp VARCHAR(255) NOT NULL, shown BOOLEAN NOT NULL DEFAULT FALSE, corrected BOOLEAN NOT NULL DEFAULT FALSE, start_availability TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, diff --git a/surveys.go b/surveys.go index bd6581a..83f135a 100644 --- a/surveys.go +++ b/surveys.go @@ -6,6 +6,7 @@ import ( "fmt" "net/http" "strconv" + "strings" "time" "github.com/julienschmidt/httprouter" @@ -19,7 +20,19 @@ func init() { } else if u.IsAdmin { return formatApiResponse(getSurveys("ORDER BY promo DESC, start_availability ASC")) } else { - return formatApiResponse(getSurveys(fmt.Sprintf("WHERE shown = TRUE AND promo = %d ORDER BY start_availability ASC", u.Promo))) + surveys, err := getSurveys(fmt.Sprintf("WHERE shown = TRUE AND promo = %d ORDER BY start_availability ASC", u.Promo)) + if err != nil { + return APIErrorResponse{err: err} + } + + var response []Survey + for _, s := range surveys { + if s.Group == "" || strings.Contains(u.Groups, ","+s.Group+",") { + response = append(response, s) + } + } + + return formatApiResponse(response, nil) } })) router.POST("/api/surveys", apiHandler(func(_ httprouter.Params, body []byte) HTTPResponse { @@ -32,11 +45,11 @@ func init() { new.Promo = currentPromo } - return formatApiResponse(NewSurvey(new.Title, new.Promo, new.Shown, new.StartAvailability, new.EndAvailability)) + return formatApiResponse(NewSurvey(new.Title, new.Promo, new.Group, new.Shown, new.StartAvailability, new.EndAvailability)) }, adminRestricted)) router.GET("/api/surveys/:sid", apiAuthHandler(surveyAuthHandler( func(s Survey, u *User, _ []byte) HTTPResponse { - if (s.Promo == u.Promo && s.Shown) || (u != nil && u.IsAdmin) { + if (s.Promo == u.Promo && (s.Group == "" || (u != nil && strings.Contains(u.Groups, ","+s.Group+",")) && s.Shown)) || (u != nil && u.IsAdmin) { return APIResponse{s} } else { return APIErrorResponse{ @@ -125,6 +138,7 @@ type Survey struct { Id int64 `json:"id"` Title string `json:"title"` Promo uint `json:"promo"` + Group string `json:"group"` Shown bool `json:"shown"` Corrected bool `json:"corrected"` StartAvailability time.Time `json:"start_availability"` @@ -132,14 +146,14 @@ type Survey struct { } func getSurveys(cnd string, param ...interface{}) (surveys []Survey, err error) { - if rows, errr := DBQuery("SELECT id_survey, title, promo, shown, corrected, start_availability, end_availability FROM surveys "+cnd, param...); errr != nil { + if rows, errr := DBQuery("SELECT id_survey, title, promo, grp, shown, corrected, start_availability, end_availability FROM surveys "+cnd, param...); errr != nil { return nil, errr } else { defer rows.Close() for rows.Next() { var s Survey - if err = rows.Scan(&s.Id, &s.Title, &s.Promo, &s.Shown, &s.Corrected, &s.StartAvailability, &s.EndAvailability); err != nil { + if err = rows.Scan(&s.Id, &s.Title, &s.Promo, &s.Group, &s.Shown, &s.Corrected, &s.StartAvailability, &s.EndAvailability); err != nil { return } surveys = append(surveys, s) @@ -153,17 +167,17 @@ func getSurveys(cnd string, param ...interface{}) (surveys []Survey, err error) } func getSurvey(id int) (s Survey, err error) { - err = DBQueryRow("SELECT id_survey, title, promo, shown, corrected, start_availability, end_availability FROM surveys WHERE id_survey=?", id).Scan(&s.Id, &s.Title, &s.Promo, &s.Shown, &s.Corrected, &s.StartAvailability, &s.EndAvailability) + err = DBQueryRow("SELECT id_survey, title, promo, grp, shown, corrected, start_availability, end_availability FROM surveys WHERE id_survey=?", id).Scan(&s.Id, &s.Title, &s.Promo, &s.Group, &s.Shown, &s.Corrected, &s.StartAvailability, &s.EndAvailability) return } -func NewSurvey(title string, promo uint, shown bool, startAvailability time.Time, endAvailability time.Time) (*Survey, error) { - if res, err := DBExec("INSERT INTO surveys (title, promo, shown, start_availability, end_availability) VALUES (?, ?, ?, ?, ?)", title, promo, shown, startAvailability, endAvailability); err != nil { +func NewSurvey(title string, promo uint, group string, shown bool, startAvailability time.Time, endAvailability time.Time) (*Survey, error) { + if res, err := DBExec("INSERT INTO surveys (title, promo, grp, shown, start_availability, end_availability) VALUES (?, ?, ?, ?, ?, ?)", title, promo, group, shown, startAvailability, endAvailability); err != nil { return nil, err } else if sid, err := res.LastInsertId(); err != nil { return nil, err } else { - return &Survey{sid, title, promo, shown, false, startAvailability, endAvailability}, nil + return &Survey{sid, title, promo, group, shown, false, startAvailability, endAvailability}, nil } } @@ -201,7 +215,7 @@ func (s Survey) GetScores() (scores map[int64]*float64, err error) { } func (s *Survey) Update() (*Survey, error) { - if _, err := DBExec("UPDATE surveys SET title = ?, promo = ?, shown = ?, corrected = ?, start_availability = ?, end_availability = ? WHERE id_survey = ?", s.Title, s.Promo, s.Shown, s.Corrected, s.StartAvailability, s.EndAvailability, s.Id); err != nil { + if _, err := DBExec("UPDATE surveys SET title = ?, promo = ?, grp = ?, shown = ?, corrected = ?, start_availability = ?, end_availability = ? WHERE id_survey = ?", s.Title, s.Promo, s.Group, s.Shown, s.Corrected, s.StartAvailability, s.EndAvailability, s.Id); err != nil { return nil, err } else { return s, err