Add notion of promo

This commit is contained in:
nemunaire 2021-03-01 17:47:00 +01:00
commit 000ac334cc
5 changed files with 40 additions and 20 deletions

View file

@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"time"
@ -14,11 +15,11 @@ func init() {
router.GET("/api/surveys", apiAuthHandler(
func(u *User, _ httprouter.Params, _ []byte) HTTPResponse {
if u == nil {
return formatApiResponse(getSurveys("WHERE shown = TRUE AND NOW() > start_availability"))
return formatApiResponse(getSurveys(fmt.Sprintf("WHERE shown = TRUE AND NOW() > start_availability AND promo = %d", currentPromo)))
} else if u.IsAdmin {
return formatApiResponse(getSurveys(""))
} else {
return formatApiResponse(getSurveys("WHERE shown = TRUE"))
return formatApiResponse(getSurveys(fmt.Sprintf("WHERE shown = TRUE AND promo = %d", u.Promo)))
}
}))
router.POST("/api/surveys", apiHandler(func(_ httprouter.Params, body []byte) HTTPResponse {
@ -27,11 +28,15 @@ func init() {
return APIErrorResponse{err: err}
}
return formatApiResponse(NewSurvey(new.Title, new.Shown, new.StartAvailability, new.EndAvailability))
if new.Promo == 0 {
new.Promo = currentPromo
}
return formatApiResponse(NewSurvey(new.Title, new.Promo, new.Shown, new.StartAvailability, new.EndAvailability))
}, adminRestricted))
router.GET("/api/surveys/:sid", apiAuthHandler(surveyAuthHandler(
func(s Survey, u *User, _ []byte) HTTPResponse {
if s.Shown || (u != nil && u.IsAdmin) {
if (s.Promo == u.Promo && s.Shown) || (u != nil && u.IsAdmin) {
return APIResponse{s}
} else {
return APIErrorResponse{
@ -55,7 +60,7 @@ func init() {
}), adminRestricted))
router.GET("/api/surveys/:sid/score", apiAuthHandler(surveyAuthHandler(
func(s Survey, u *User, _ []byte) HTTPResponse {
if s.Shown || (u != nil && u.IsAdmin) {
if (s.Promo == u.Promo && s.Shown) || (u != nil && u.IsAdmin) {
if score, err := s.GetScore(u); err != nil {
return APIErrorResponse{err: err}
} else if score == nil {
@ -73,7 +78,7 @@ func init() {
router.GET("/api/users/:uid/surveys/:sid/score", apiAuthHandler(func(uauth *User, ps httprouter.Params, body []byte) HTTPResponse {
return surveyAuthHandler(func(s Survey, uauth *User, _ []byte) HTTPResponse {
return userHandler(func(u User, _ []byte) HTTPResponse {
if uauth != nil && ((s.Shown && u.Id == uauth.Id) || uauth.IsAdmin) {
if uauth != nil && ((s.Promo == u.Promo && s.Shown && u.Id == uauth.Id) || uauth.IsAdmin) {
if score, err := s.GetScore(&u); err != nil {
return APIErrorResponse{err: err}
} else if score == nil {
@ -119,6 +124,7 @@ func surveyAuthHandler(f func(Survey, *User, []byte) HTTPResponse) func(*User, h
type Survey struct {
Id int64 `json:"id"`
Title string `json:"title"`
Promo uint `json:"promo"`
Shown bool `json:"shown"`
Corrected bool `json:"corrected"`
StartAvailability time.Time `json:"start_availability"`
@ -126,14 +132,14 @@ type Survey struct {
}
func getSurveys(cnd string, param ...interface{}) (surveys []Survey, err error) {
if rows, errr := DBQuery("SELECT id_survey, title, shown, corrected, start_availability, end_availability FROM surveys "+cnd, param...); errr != nil {
if rows, errr := DBQuery("SELECT id_survey, title, promo, 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.Shown, &s.Corrected, &s.StartAvailability, &s.EndAvailability); err != nil {
if err = rows.Scan(&s.Id, &s.Title, &s.Promo, &s.Shown, &s.Corrected, &s.StartAvailability, &s.EndAvailability); err != nil {
return
}
surveys = append(surveys, s)
@ -147,17 +153,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, shown, corrected, start_availability, end_availability FROM surveys WHERE id_survey=?", id).Scan(&s.Id, &s.Title, &s.Shown, &s.Corrected, &s.StartAvailability, &s.EndAvailability)
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)
return
}
func NewSurvey(title string, shown bool, startAvailability time.Time, endAvailability time.Time) (Survey, error) {
if res, err := DBExec("INSERT INTO surveys (title, shown, start_availability, end_availability) VALUES (?, ?, ?, ?)", title, shown, startAvailability, endAvailability); err != nil {
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 {
return Survey{}, err
} else if sid, err := res.LastInsertId(); err != nil {
return Survey{}, err
} else {
return Survey{sid, title, shown, false, startAvailability, endAvailability}, nil
return Survey{sid, title, promo, shown, false, startAvailability, endAvailability}, nil
}
}
@ -195,7 +201,7 @@ func (s Survey) GetScores() (scores map[int64]*float64, err error) {
}
func (s Survey) Update() (int64, error) {
if res, err := DBExec("UPDATE surveys SET title = ?, shown = ?, corrected = ?, start_availability = ?, end_availability = ? WHERE id_survey = ?", s.Title, s.Shown, s.Corrected, s.StartAvailability, s.EndAvailability, s.Id); err != nil {
if res, 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 {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
return 0, err