Skip to content

Commit

Permalink
api: endpoint /elections can now filter startDate and endDate (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
altergui committed Aug 8, 2024
1 parent 2491a75 commit 303a022
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 7 deletions.
16 changes: 10 additions & 6 deletions api/api_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ type PaginationParams struct {
// ElectionParams allows the client to filter elections
type ElectionParams struct {
PaginationParams
OrganizationID string `json:"organizationId,omitempty"`
ElectionID string `json:"electionId,omitempty"`
Status string `json:"status,omitempty"`
WithResults *bool `json:"withResults,omitempty"`
FinalResults *bool `json:"finalResults,omitempty"`
ManuallyEnded *bool `json:"manuallyEnded,omitempty"`
OrganizationID string `json:"organizationId,omitempty"`
ElectionID string `json:"electionId,omitempty"`
Status string `json:"status,omitempty"`
WithResults *bool `json:"withResults,omitempty"`
FinalResults *bool `json:"finalResults,omitempty"`
ManuallyEnded *bool `json:"manuallyEnded,omitempty"`
StartDateAfter *time.Time `json:"startDateAfter,omitempty"`
StartDateBefore *time.Time `json:"startDateBefore,omitempty"`
EndDateAfter *time.Time `json:"endDateAfter,omitempty"`
EndDateBefore *time.Time `json:"endDateBefore,omitempty"`
}

// OrganizationParams allows the client to filter organizations
Expand Down
10 changes: 9 additions & 1 deletion api/elections.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,9 @@ func (a *API) buildElectionIDHandler(msg *apirest.APIdata, ctx *httprouter.HTTPC
// parseElectionParams returns an ElectionParams filled with the passed params
func parseElectionParams(paramPage, paramLimit, paramStatus,
paramOrganizationID, paramElectionID,
paramWithResults, paramFinalResults, paramManuallyEnded string,
paramWithResults, paramFinalResults, paramManuallyEnded,
paramStartDateAfter, paramStartDateBefore,
paramEndDateAfter, paramEndDateBefore string,
) (*ElectionParams, error) {
pagination, err := parsePaginationParams(paramPage, paramLimit)
if err != nil {
Expand All @@ -782,6 +784,11 @@ func parseElectionParams(paramPage, paramLimit, paramStatus,
return nil, err
}

startDateAfter, err := parseDate(paramStartDateAfter)
if err != nil {
return nil, err
}

return &ElectionParams{
PaginationParams: pagination,
OrganizationID: util.TrimHex(paramOrganizationID),
Expand All @@ -790,5 +797,6 @@ func parseElectionParams(paramPage, paramLimit, paramStatus,
WithResults: withResults,
FinalResults: finalResults,
ManuallyEnded: manuallyEnded,
StartDateAfter: startDateAfter,
}, nil
}
1 change: 1 addition & 0 deletions api/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ var (
ErrCantParseBoolean = apirest.APIerror{Code: 4055, HTTPstatus: apirest.HTTPstatusBadRequest, Err: fmt.Errorf("cannot parse string into boolean")}
ErrCantParseHexString = apirest.APIerror{Code: 4056, HTTPstatus: apirest.HTTPstatusBadRequest, Err: fmt.Errorf("cannot parse string into hex bytes")}
ErrPageNotFound = apirest.APIerror{Code: 4057, HTTPstatus: apirest.HTTPstatusNotFound, Err: fmt.Errorf("page not found")}
ErrCantParseDate = apirest.APIerror{Code: 4058, HTTPstatus: apirest.HTTPstatusBadRequest, Err: fmt.Errorf("cannot parse date")}
ErrVochainEmptyReply = apirest.APIerror{Code: 5000, HTTPstatus: apirest.HTTPstatusInternalErr, Err: fmt.Errorf("vochain returned an empty reply")}
ErrVochainSendTxFailed = apirest.APIerror{Code: 5001, HTTPstatus: apirest.HTTPstatusInternalErr, Err: fmt.Errorf("vochain SendTx failed")}
ErrVochainGetTxFailed = apirest.APIerror{Code: 5002, HTTPstatus: apirest.HTTPstatusInternalErr, Err: fmt.Errorf("vochain GetTx failed")}
Expand Down
15 changes: 15 additions & 0 deletions api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"math/big"
"strconv"
"strings"
"time"

cometpool "github.com/cometbft/cometbft/mempool"
cometcoretypes "github.com/cometbft/cometbft/rpc/core/types"
Expand Down Expand Up @@ -275,6 +276,20 @@ func parseBool(s string) (*bool, error) {
return &b, nil
}

// parseDate parses an RFC3339 string into a time.Time value.
//
// The empty string "" is treated specially, returns a nil pointer with no error.
func parseDate(s string) (*time.Time, error) {
if s == "" {
return nil, nil
}
b, err := time.Parse(time.RFC3339, s)
if err != nil {
return nil, ErrCantParseDate.With(s)
}
return &b, nil
}

// parsePaginationParams returns a PaginationParams filled with the passed params
func parsePaginationParams(paramPage, paramLimit string) (PaginationParams, error) {
page, err := parsePage(paramPage)
Expand Down
14 changes: 14 additions & 0 deletions vochain/indexer/db/processes.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions vochain/indexer/queries/processes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ WITH results AS (
OR (sqlc.arg(manually_ended) = 1 AND manually_ended = TRUE)
OR (sqlc.arg(manually_ended) = 0 AND manually_ended = FALSE)
)
AND (
(sqlc.arg(start_date_after) IS NULL OR start_date >= sqlc.arg(start_date_after))
AND (sqlc.arg(start_date_before) IS NULL OR start_date <= sqlc.arg(start_date_before))
AND (sqlc.arg(end_date_after) IS NULL OR end_date >= sqlc.arg(end_date_after))
AND (sqlc.arg(end_date_before) IS NULL OR end_date <= sqlc.arg(end_date_before))
)
)
)
SELECT id, total_count
Expand Down

0 comments on commit 303a022

Please sign in to comment.