Skip to content

Commit 05713be

Browse files
committed
api: endpoint /elections now filters by title and description (WIP)
1 parent 50c3f98 commit 05713be

9 files changed

Lines changed: 85 additions & 41 deletions

File tree

api/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ const (
6767
ParamVoteId = "voteId"
6868
ParamPage = "page"
6969
ParamLimit = "limit"
70+
ParamTitle = "title"
71+
ParamDescription = "description"
7072
ParamStatus = "status"
7173
ParamWithResults = "withResults"
7274
ParamFinalResults = "finalResults"

api/api_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ type ElectionParams struct {
3333
StartDateBefore *time.Time `json:"startDateBefore,omitempty"`
3434
EndDateAfter *time.Time `json:"endDateAfter,omitempty"`
3535
EndDateBefore *time.Time `json:"endDateBefore,omitempty"`
36+
Title string `json:"title,omitempty"`
37+
Description string `json:"description,omitempty"`
3638
}
3739

3840
// OrganizationParams allows the client to filter organizations
@@ -122,6 +124,8 @@ type ElectionSummary struct {
122124
Results [][]*types.BigInt `json:"result,omitempty"`
123125
ManuallyEnded bool `json:"manuallyEnded"`
124126
ChainID string `json:"chainId"`
127+
Title string `json:"title"`
128+
Description string `json:"description"`
125129
}
126130

127131
// ElectionsList is used to return a paginated list to the client

api/elections.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ func (a *API) electionList(params *ElectionParams) (*ElectionsList, error) {
316316
params.StartDateBefore,
317317
params.EndDateAfter,
318318
params.EndDateBefore,
319+
params.Title,
320+
params.Description,
319321
)
320322
if err != nil {
321323
return nil, ErrIndexerQueryFailed.WithErr(err)
@@ -831,6 +833,8 @@ func electionParams(f func(key string) string, keys ...string) (*ElectionParams,
831833
PaginationParams: pagination,
832834
OrganizationID: util.TrimHex(strings[ParamOrganizationId]),
833835
ElectionID: util.TrimHex(strings[ParamElectionId]),
836+
Title: strings[ParamTitle],
837+
Description: strings[ParamDescription],
834838
Status: strings[ParamStatus],
835839
WithResults: bools[ParamWithResults],
836840
FinalResults: bools[ParamFinalResults],

vochain/indexer/db/models.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vochain/indexer/db/processes.sql.go

Lines changed: 39 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vochain/indexer/indexer_test.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ func testProcessList(t *testing.T, procsCount int) {
332332
for len(procs) < procsCount {
333333
fmt.Printf("%x\n", eidProcsCount)
334334
fmt.Printf("%s\n", hex.EncodeToString(eidProcsCount))
335-
list, total, err := idx.ProcessList(10, last, hex.EncodeToString(eidProcsCount), "", 0, 0, 0, nil, nil, nil, nil, nil, nil, nil)
335+
list, total, err := idx.ProcessList(10, last, hex.EncodeToString(eidProcsCount), "", 0, 0, 0, nil, nil, nil, nil, nil, nil, nil, "", "")
336336
if err != nil {
337337
t.Fatal(err)
338338
}
@@ -351,7 +351,7 @@ func testProcessList(t *testing.T, procsCount int) {
351351
}
352352
qt.Assert(t, procs, qt.HasLen, procsCount)
353353

354-
_, total, err := idx.ProcessList(64, 0, "", "", 0, 0, 0, nil, nil, nil, nil, nil, nil, nil)
354+
_, total, err := idx.ProcessList(64, 0, "", "", 0, 0, 0, nil, nil, nil, nil, nil, nil, nil, "", "")
355355
qt.Assert(t, err, qt.IsNil)
356356
qt.Assert(t, total, qt.Equals, uint64(10+procsCount))
357357

@@ -369,7 +369,7 @@ func testProcessList(t *testing.T, procsCount int) {
369369
qt.Assert(t, countEntityProcs([]byte("not an entity id that exists")), qt.Equals, int64(-1))
370370

371371
// Past the end (from=10000) should return an empty list
372-
emptyList, _, err := idx.ProcessList(64, 10000, "", "", 0, 0, 0, nil, nil, nil, nil, nil, nil, nil)
372+
emptyList, _, err := idx.ProcessList(64, 10000, "", "", 0, 0, 0, nil, nil, nil, nil, nil, nil, nil, "", "")
373373
qt.Assert(t, err, qt.IsNil)
374374
qt.Assert(t, emptyList, qt.DeepEquals, [][]byte{})
375375
}
@@ -459,7 +459,7 @@ func TestProcessSearch(t *testing.T) {
459459
app.AdvanceTestBlock()
460460

461461
// Exact process search
462-
list, _, err := idx.ProcessList(10, 0, hex.EncodeToString(eidTest), pidExact, 0, 0, 0, nil, nil, nil, nil, nil, nil, nil)
462+
list, _, err := idx.ProcessList(10, 0, hex.EncodeToString(eidTest), pidExact, 0, 0, 0, nil, nil, nil, nil, nil, nil, nil, "", "")
463463
if err != nil {
464464
t.Fatal(err)
465465
}
@@ -468,7 +468,7 @@ func TestProcessSearch(t *testing.T) {
468468
}
469469
// Exact process search, with it being encrypted.
470470
// This once caused a sqlite bug due to a mistake in the SQL query.
471-
list, _, err = idx.ProcessList(10, 0, hex.EncodeToString(eidTest), pidExactEncrypted, 0, 0, 0, nil, nil, nil, nil, nil, nil, nil)
471+
list, _, err = idx.ProcessList(10, 0, hex.EncodeToString(eidTest), pidExactEncrypted, 0, 0, 0, nil, nil, nil, nil, nil, nil, nil, "", "")
472472
if err != nil {
473473
t.Fatal(err)
474474
}
@@ -477,7 +477,7 @@ func TestProcessSearch(t *testing.T) {
477477
}
478478
// Search for nonexistent process
479479
list, _, err = idx.ProcessList(10, 0, hex.EncodeToString(eidTest),
480-
"4011d50537fa164b6fef261141797bbe4014526f", 0, 0, 0, nil, nil, nil, nil, nil, nil, nil)
480+
"4011d50537fa164b6fef261141797bbe4014526f", 0, 0, 0, nil, nil, nil, nil, nil, nil, nil, "", "")
481481
if err != nil {
482482
t.Fatal(err)
483483
}
@@ -486,7 +486,7 @@ func TestProcessSearch(t *testing.T) {
486486
}
487487
// Search containing part of all manually-defined processes
488488
list, _, err = idx.ProcessList(10, 0, hex.EncodeToString(eidTest),
489-
"011d50537fa164b6fef261141797bbe4014526e", 0, 0, 0, nil, nil, nil, nil, nil, nil, nil)
489+
"011d50537fa164b6fef261141797bbe4014526e", 0, 0, 0, nil, nil, nil, nil, nil, nil, nil, "", "")
490490
if err != nil {
491491
t.Fatal(err)
492492
}
@@ -495,7 +495,7 @@ func TestProcessSearch(t *testing.T) {
495495
}
496496

497497
list, _, err = idx.ProcessList(100, 0, hex.EncodeToString(eidTest),
498-
"0c6ca22d2c175a1fbdd15d7595ae532bb1094b5", 0, 0, models.ProcessStatus_ENDED, nil, nil, nil, nil, nil, nil, nil)
498+
"0c6ca22d2c175a1fbdd15d7595ae532bb1094b5", 0, 0, models.ProcessStatus_ENDED, nil, nil, nil, nil, nil, nil, nil, "", "")
499499
if err != nil {
500500
t.Fatal(err)
501501
}
@@ -504,17 +504,17 @@ func TestProcessSearch(t *testing.T) {
504504
}
505505

506506
// Partial process search as uppercase hex
507-
list, _, err = idx.ProcessList(10, 0, hex.EncodeToString(eidTest), "011D50537FA164B6FEF261141797BBE4014526E", 0, 0, 0, nil, nil, nil, nil, nil, nil, nil)
507+
list, _, err = idx.ProcessList(10, 0, hex.EncodeToString(eidTest), "011D50537FA164B6FEF261141797BBE4014526E", 0, 0, 0, nil, nil, nil, nil, nil, nil, nil, "", "")
508508
qt.Assert(t, err, qt.IsNil)
509509
qt.Assert(t, list, qt.HasLen, len(processIds))
510510
// Partial process search as mixed case hex
511-
list, _, err = idx.ProcessList(10, 0, hex.EncodeToString(eidTest), "011D50537fA164B6FeF261141797BbE4014526E", 0, 0, 0, nil, nil, nil, nil, nil, nil, nil)
511+
list, _, err = idx.ProcessList(10, 0, hex.EncodeToString(eidTest), "011D50537fA164B6FeF261141797BbE4014526E", 0, 0, 0, nil, nil, nil, nil, nil, nil, nil, "", "")
512512
qt.Assert(t, err, qt.IsNil)
513513
qt.Assert(t, list, qt.HasLen, len(processIds))
514514

515515
// Search with an exact Entity ID, but starting with a null byte.
516516
// This can trip up sqlite, as it assumes TEXT strings are NUL-terminated.
517-
list, _, err = idx.ProcessList(100, 0, "\x00foobar", "", 0, 0, 0, nil, nil, nil, nil, nil, nil, nil)
517+
list, _, err = idx.ProcessList(100, 0, "\x00foobar", "", 0, 0, 0, nil, nil, nil, nil, nil, nil, nil, "", "")
518518
if err != nil {
519519
t.Fatal(err)
520520
}
@@ -523,12 +523,12 @@ func TestProcessSearch(t *testing.T) {
523523
}
524524

525525
// list all processes, with a max of 10
526-
list, _, err = idx.ProcessList(10, 0, "", "", 0, 0, 0, nil, nil, nil, nil, nil, nil, nil)
526+
list, _, err = idx.ProcessList(10, 0, "", "", 0, 0, 0, nil, nil, nil, nil, nil, nil, nil, "", "")
527527
qt.Assert(t, err, qt.IsNil)
528528
qt.Assert(t, list, qt.HasLen, 10)
529529

530530
// list all processes, with a max of 1000
531-
list, _, err = idx.ProcessList(1000, 0, "", "", 0, 0, 0, nil, nil, nil, nil, nil, nil, nil)
531+
list, _, err = idx.ProcessList(1000, 0, "", "", 0, 0, 0, nil, nil, nil, nil, nil, nil, nil, "", "")
532532
qt.Assert(t, err, qt.IsNil)
533533
qt.Assert(t, list, qt.HasLen, 21)
534534
}
@@ -577,25 +577,25 @@ func TestProcessListWithNamespaceAndStatus(t *testing.T) {
577577
app.AdvanceTestBlock()
578578

579579
// Get the process list for namespace 123
580-
list, _, err := idx.ProcessList(100, 0, hex.EncodeToString(eid20), "", 123, 0, 0, nil, nil, nil, nil, nil, nil, nil)
580+
list, _, err := idx.ProcessList(100, 0, hex.EncodeToString(eid20), "", 123, 0, 0, nil, nil, nil, nil, nil, nil, nil, "", "")
581581
qt.Assert(t, err, qt.IsNil)
582582
// Check there are exactly 10
583583
qt.Assert(t, len(list), qt.CmpEquals(), 10)
584584

585585
// Get the process list for all namespaces
586-
list, _, err = idx.ProcessList(100, 0, "", "", 0, 0, 0, nil, nil, nil, nil, nil, nil, nil)
586+
list, _, err = idx.ProcessList(100, 0, "", "", 0, 0, 0, nil, nil, nil, nil, nil, nil, nil, "", "")
587587
qt.Assert(t, err, qt.IsNil)
588588
// Check there are exactly 10 + 10
589589
qt.Assert(t, len(list), qt.CmpEquals(), 20)
590590

591591
// Get the process list for namespace 10
592-
list, _, err = idx.ProcessList(100, 0, "", "", 10, 0, 0, nil, nil, nil, nil, nil, nil, nil)
592+
list, _, err = idx.ProcessList(100, 0, "", "", 10, 0, 0, nil, nil, nil, nil, nil, nil, nil, "", "")
593593
qt.Assert(t, err, qt.IsNil)
594594
// Check there is exactly 1
595595
qt.Assert(t, len(list), qt.CmpEquals(), 1)
596596

597597
// Get the process list for namespace 10
598-
list, _, err = idx.ProcessList(100, 0, "", "", 0, 0, models.ProcessStatus_READY, nil, nil, nil, nil, nil, nil, nil)
598+
list, _, err = idx.ProcessList(100, 0, "", "", 0, 0, models.ProcessStatus_READY, nil, nil, nil, nil, nil, nil, nil, "", "")
599599
qt.Assert(t, err, qt.IsNil)
600600
// Check there is exactly 1
601601
qt.Assert(t, len(list), qt.CmpEquals(), 10)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- +goose Up
2+
ALTER TABLE processes ADD COLUMN title TEXT NOT NULL DEFAULT '';
3+
ALTER TABLE processes ADD COLUMN description TEXT NOT NULL DEFAULT '';
4+
5+
-- +goose Down
6+
ALTER TABLE processes DROP COLUMN description;
7+
ALTER TABLE processes DROP COLUMN title;

vochain/indexer/process.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func (idx *Indexer) ProcessList(limit, offset int, entityID string, processID st
5252
namespace uint32, srcNetworkID int32, status models.ProcessStatus,
5353
withResults, finalResults, manuallyEnded *bool,
5454
startDateAfter, startDateBefore, endDateAfter, endDateBefore *time.Time,
55+
title, description string,
5556
) ([][]byte, uint64, error) {
5657
if offset < 0 {
5758
return nil, 0, fmt.Errorf("invalid value: offset cannot be %d", offset)
@@ -78,6 +79,8 @@ func (idx *Indexer) ProcessList(limit, offset int, entityID string, processID st
7879
StartDateBefore: startDateBefore,
7980
EndDateAfter: endDateAfter,
8081
EndDateBefore: endDateBefore,
82+
Title: title,
83+
Description: description,
8184
})
8285
if err != nil {
8386
return nil, 0, err
@@ -98,7 +101,7 @@ func (idx *Indexer) ProcessExists(processID string) bool {
98101
if len(processID) != 64 {
99102
return false
100103
}
101-
_, count, err := idx.ProcessList(1, 0, "", processID, 0, 0, 0, nil, nil, nil, nil, nil, nil, nil)
104+
_, count, err := idx.ProcessList(1, 0, "", processID, 0, 0, 0, nil, nil, nil, nil, nil, nil, nil, "", "")
102105
if err != nil {
103106
log.Errorw(err, "indexer query failed")
104107
}

0 commit comments

Comments
 (0)