@@ -3,7 +3,6 @@ package inventory
33import (
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+
1619func 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}
0 commit comments