|
| 1 | +package inventory |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/mongodb/mongo-go-driver/mongo/findopt" |
| 8 | + |
| 9 | + "github.com/TerrexTech/go-eventstore-models/model" |
| 10 | + "github.com/TerrexTech/go-mongoutils/mongo" |
| 11 | + "github.com/pkg/errors" |
| 12 | +) |
| 13 | + |
| 14 | +// TimeConstraints is the parameter for querying inventory by stat-time and end-time. |
| 15 | +type FlashOrSale struct { |
| 16 | + Field string `json:"field,omitempty"` |
| 17 | + Weight float64 `json:"weight,omitempty"` |
| 18 | + Count int `json:"count,omitempty"` |
| 19 | +} |
| 20 | + |
| 21 | +func queryFlashOrSale(collection *mongo.Collection, event *model.Event) *model.KafkaResponse { |
| 22 | + flashOrSale := &FlashOrSale{} |
| 23 | + err := json.Unmarshal(event.Data, &flashOrSale) |
| 24 | + if err != nil { |
| 25 | + err = errors.Wrap(err, "Query: Error while unmarshalling Event-data") |
| 26 | + log.Println(err) |
| 27 | + return &model.KafkaResponse{ |
| 28 | + AggregateID: event.AggregateID, |
| 29 | + CorrelationID: event.CorrelationID, |
| 30 | + Error: err.Error(), |
| 31 | + ErrorCode: InternalError, |
| 32 | + EventAction: event.EventAction, |
| 33 | + ServiceAction: event.ServiceAction, |
| 34 | + UUID: event.UUID, |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + if flashOrSale.Weight < 1 { |
| 39 | + err = errors.New("blank weight provided") |
| 40 | + err = errors.Wrap(err, "QueryFlashOrSale") |
| 41 | + log.Println(err) |
| 42 | + return &model.KafkaResponse{ |
| 43 | + AggregateID: event.AggregateID, |
| 44 | + CorrelationID: event.CorrelationID, |
| 45 | + Error: err.Error(), |
| 46 | + ErrorCode: InternalError, |
| 47 | + EventAction: event.EventAction, |
| 48 | + ServiceAction: event.ServiceAction, |
| 49 | + UUID: event.UUID, |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + filter := map[string]interface{}{ |
| 54 | + flashOrSale.Field: map[string]interface{}{ |
| 55 | + "$gt": flashOrSale.Weight, |
| 56 | + }, |
| 57 | + } |
| 58 | + eventData, err := json.Marshal(filter) |
| 59 | + if err != nil { |
| 60 | + err = errors.Wrap(err, "Error marshalling TimeConstraint-filter") |
| 61 | + log.Println(err) |
| 62 | + return &model.KafkaResponse{ |
| 63 | + AggregateID: event.AggregateID, |
| 64 | + CorrelationID: event.CorrelationID, |
| 65 | + Error: err.Error(), |
| 66 | + ErrorCode: InternalError, |
| 67 | + EventAction: event.EventAction, |
| 68 | + ServiceAction: event.ServiceAction, |
| 69 | + UUID: event.UUID, |
| 70 | + } |
| 71 | + } |
| 72 | + event.Data = eventData |
| 73 | + |
| 74 | + count := flashOrSale.Count |
| 75 | + // if count < 1 { |
| 76 | + // count = 50 |
| 77 | + // } else if count == 50 { |
| 78 | + // count = 50 |
| 79 | + // } else if count == 100 { |
| 80 | + // count = 100 |
| 81 | + // } |
| 82 | + findopts := findopt.Limit(int64(count)) |
| 83 | + return queryInventory(collection, event, findopts) |
| 84 | +} |
0 commit comments