Skip to content

Commit

Permalink
CORE-1983: added code to extract the new query parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
slr71 committed Feb 15, 2024
1 parent d3299ee commit dae16db
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
23 changes: 20 additions & 3 deletions internal/controllers/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"strings"
"time"

"gorm.io/gorm"

Expand Down Expand Up @@ -333,21 +334,37 @@ func (s Server) UpdateSubscription(ctx echo.Context) error {
if planName == "" {
return model.Error(ctx, "invalid plan name", http.StatusBadRequest)
}
log.Debugf("plan name from request is %s", planName)

paid, err := query.ValidateBooleanQueryParam(ctx, "paid", nil)
if err != nil {
return model.Error(ctx, err.Error(), http.StatusBadRequest)
}

log.Debugf("plan name from request is %s", planName)
log.Debugf("paid flag from request is %t", paid)

username := strings.TrimSuffix(ctx.Param("username"), s.UsernameSuffix)
if username == "" {
return model.Error(ctx, "invalid username", http.StatusBadRequest)
}

log.Debugf("user name from request is %s", username)

var defaultPeriods int32 = 1
periods, err := query.ValidateIntQueryParam(ctx, "periods", &defaultPeriods, "gte=0")
if err != nil {
return model.Error(ctx, err.Error(), http.StatusBadRequest)
}
log.Debugf("periods from request is %d", periods)

defaultEndDate := time.Now().AddDate(1, 0, 0)
endDate, err := query.ValidateDateQueryParam(ctx, "end_date", &defaultEndDate)
if err != nil {
return model.Error(ctx, err.Error(), http.StatusBadRequest)
}
if !endDate.After(time.Now()) {
return model.Error(ctx, "end date must be in the future", http.StatusBadRequest)
}
log.Debugf("end date from request is %s", endDate)

log = log.WithFields(logrus.Fields{
"user": username,
"plan": planName,
Expand Down
30 changes: 30 additions & 0 deletions internal/query/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strconv"
"strings"
"time"

"github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -50,6 +51,35 @@ func ValidateBooleanQueryParam(ctx echo.Context, name string, defaultValue *bool
return result, nil
}

// ValidateDateQueryParam extracts the value of a date query parameter and validates it.
func ValidateDateQueryParam(ctx echo.Context, name string, defaultValue *time.Time) (time.Time, error) {
errMsg := fmt.Sprintf("invalid query parameter: %s", name)
value := ctx.QueryParam(name)

// Extract the time value.
var timeValue time.Time

// Assume that the parameter is required if there's no default.
if defaultValue == nil {
if err := v.Var(value, "required"); err != nil {
return timeValue, fmt.Errorf("missing required query parameter: %s", name)
}
}

// If no value was provided at this point then the parameter is optional; return the default value.
if value == "" {
return *defaultValue, nil
}

// Parse the parameter value and return the result.
timeValue, err := time.Parse(time.DateOnly, value)
if err != nil {
return timeValue, errors.Wrap(err, errMsg)
}

return timeValue, nil
}

// ValidateIntQueryParam extracts an optional integer query parameter and validates it.
func ValidateIntQueryParam(ctx echo.Context, name string, defaultValue *int32, checks ...string) (int32, error) {
errMsg := fmt.Sprintf("invalid query parameter: %s", name)
Expand Down

0 comments on commit dae16db

Please sign in to comment.