Skip to content

Commit 18eae9e

Browse files
Merge #6
6: Fix CountQuery paramter-parsing r=Jaskaranbir a=Jaskaranbir Co-authored-by: Jaskaranbir <[email protected]>
2 parents 8b0e274 + 993c487 commit 18eae9e

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

inventory/query_count.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package inventory
33
import (
44
"encoding/json"
55
"log"
6-
"strconv"
76

87
"github.com/mongodb/mongo-go-driver/mongo/findopt"
98

@@ -13,8 +12,27 @@ import (
1312
"github.com/TerrexTech/go-mongoutils/mongo"
1413
)
1514

15+
type CountQuery struct {
16+
Count int `json:"count,omitempty"`
17+
}
18+
1619
func queryCount(collection *mongo.Collection, event *model.Event) *model.KafkaResponse {
17-
count, err := strconv.Atoi(string(event.Data))
20+
countQuery := &CountQuery{}
21+
err := json.Unmarshal(event.Data, countQuery)
22+
if err != nil {
23+
err = errors.Wrap(err, "QueryCount: Error unmarshalling paramters")
24+
log.Println(err)
25+
return &model.KafkaResponse{
26+
AggregateID: event.AggregateID,
27+
CorrelationID: event.CorrelationID,
28+
Error: err.Error(),
29+
ErrorCode: InternalError,
30+
EventAction: event.EventAction,
31+
ServiceAction: event.ServiceAction,
32+
UUID: event.UUID,
33+
}
34+
}
35+
1836
if err != nil {
1937
err = errors.Wrap(err, "QueryCount: Error converting parameter to number")
2038
log.Println(err)
@@ -29,7 +47,7 @@ func queryCount(collection *mongo.Collection, event *model.Event) *model.KafkaRe
2947
}
3048
}
3149

32-
if count == 0 {
50+
if countQuery.Count == 0 {
3351
err = errors.New("count must be greater than 0")
3452
log.Println(err)
3553
return &model.KafkaResponse{
@@ -42,7 +60,7 @@ func queryCount(collection *mongo.Collection, event *model.Event) *model.KafkaRe
4260
UUID: event.UUID,
4361
}
4462
}
45-
if count > 100 {
63+
if countQuery.Count > 100 {
4664
err = errors.New("count must be less than 100")
4765
log.Println(err)
4866
return &model.KafkaResponse{
@@ -77,6 +95,6 @@ func queryCount(collection *mongo.Collection, event *model.Event) *model.KafkaRe
7795
}
7896
event.Data = eventData
7997

80-
findopts := findopt.Limit(int64(count))
98+
findopts := findopt.Limit(int64(countQuery.Count))
8199
return queryInventory(collection, event, findopts)
82100
}

test/inventory_suite_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,10 @@ var _ = Describe("InventoryAggregate", func() {
290290

291291
Byf("Creating query args")
292292
// Get 3 records
293-
marshalQuery, err := json.Marshal(3)
293+
query := &inventory.CountQuery{
294+
Count: 2,
295+
}
296+
marshalQuery, err := json.Marshal(query)
294297
Expect(err).ToNot(HaveOccurred())
295298

296299
Byf("Creating query MockEvent")
@@ -335,7 +338,6 @@ var _ = Describe("InventoryAggregate", func() {
335338
err = json.Unmarshal(kr.Result, &result)
336339
Expect(err).ToNot(HaveOccurred())
337340
Expect(result).ToNot(BeEmpty())
338-
log.Println(string(kr.Result))
339341
return true
340342
}
341343
return false

0 commit comments

Comments
 (0)