Add start_date, end_date validation controller (#216)

This commit is contained in:
walker-16 2023-04-04 10:49:27 -03:00 committed by GitHub
parent 2da5719d49
commit a5df48c967
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 5 deletions

View File

@ -298,3 +298,32 @@ func ExtractIsNotional(ctx *fiber.Ctx) (bool, error) {
}
return false, response.NewInvalidQueryParamError(ctx, "INVALID <by> QUERY PARAMETER", nil)
}
func ExtractTimeRange(ctx *fiber.Ctx) (*time.Time, *time.Time, error) {
startTime, err := ExtractTime(ctx, "start_time")
if err != nil {
return nil, nil, err
}
// check if start_time is in the future
if startTime != nil && startTime.After(time.Now()) {
return nil, nil, response.NewInvalidQueryParamError(ctx, "INVALID <start_time> QUERY PARAMETER, CANNOT BE GREATER THAN TODAYS DATE", nil)
}
endTime, err := ExtractTime(ctx, "end_time")
if err != nil {
return nil, nil, err
}
if startTime != nil && endTime != nil {
// check if start_time and end_time are equal
if startTime.Equal(*endTime) {
return nil, nil, response.NewInvalidQueryParamError(ctx, "INVALID <start_time>, <end_time> QUERY PARAMETER, <start_time> CANNOT BE EQUAL TO <end_time>", nil)
}
// check if start_time is greater than end_time
if startTime.After(*endTime) {
return nil, nil, response.NewInvalidQueryParamError(ctx, "INVALID <start_time>, <end_time> QUERY PARAMETER, <start_time> CANNOT BE GREATER THAN <end_time>", nil)
}
}
return startTime, endTime, nil
}

View File

@ -71,11 +71,7 @@ func (c *Controller) GetLastTransactions(ctx *fiber.Ctx) error {
// @Failure 500
// @Router /api/v1/x-chain-activity [get]
func (c *Controller) GetChainActivity(ctx *fiber.Ctx) error {
startTime, err := middleware.ExtractTime(ctx, "start_time")
if err != nil {
return err
}
endTime, err := middleware.ExtractTime(ctx, "end_time")
startTime, endTime, err := middleware.ExtractTimeRange(ctx)
if err != nil {
return err
}