static void Main(string[] args) { var database = new Database(); using (var priceValidator = new SubscriberSocket(">tcp://localhost:10000")) using (var productPush = new PushSocket("@tcp://*:12000")) { priceValidator.Subscribe("product"); while (true) { var topic = priceValidator.ReceiveFrameString(); var productTohavePriceValidated = priceValidator.ReceiveFrameString(); var product = Newtonsoft.Json.JsonConvert.DeserializeObject <Product>(productTohavePriceValidated); Console.WriteLine(productTohavePriceValidated); var databaseProduct = database.Products.SingleOrDefault(x => x.Id == product.Id); ValidatedProductMessage message = null; if (databaseProduct != null) { if (string.IsNullOrEmpty(product.Name)) { message = new ValidatedProductMessage(product, "Product name is required, can not be displayed on the site."); } else if (string.IsNullOrEmpty(product.Description)) { message = new ValidatedProductMessage(product, "Product description is required, can not be displayed on the site."); } else if (product.Price > 1000 && product.RelatedProducts.Count == 0) { message = new ValidatedProductMessage(product, "Expensive products must to have at least 1 related product, can not be displayed on the site."); } else { message = new ValidatedProductMessage(product); } } else { message = new ValidatedProductMessage(null, "Product not found, need to be created by the Product Team before be published on site"); } productPush.SendFrame(Newtonsoft.Json.JsonConvert.SerializeObject(message)); } } }
static void Main(string[] args) { var database = new Database(); using (var priceValidator = new SubscriberSocket(">tcp://localhost:10000")) using (var pricePush = new PushSocket("@tcp://*:11000")) { priceValidator.Subscribe("product"); while (true) { var topic = priceValidator.ReceiveFrameString(); var productTohavePriceValidated = priceValidator.ReceiveFrameString(); Console.WriteLine(productTohavePriceValidated); var product = Newtonsoft.Json.JsonConvert.DeserializeObject <Product>(productTohavePriceValidated); var databaseProduct = database.Products.SingleOrDefault(x => x.Id == product.Id); ValidatedProductMessage message = null; if (databaseProduct != null) { if (databaseProduct.Price != product.Price) { message = new ValidatedProductMessage(product, "Wrong price, can not be displayed on the site."); } else { message = new ValidatedProductMessage(product); } } else { message = new ValidatedProductMessage(null, "Product not found, need to be created by the Product Team before be published on site"); } pricePush.SendFrame(Newtonsoft.Json.JsonConvert.SerializeObject(message)); } } }
static void Main(string[] args) { var identity = Guid.NewGuid().ToString(); using (var receiver = new PullSocket(">tcp://localhost:10000")) using (var validationPush = new PushSocket(">tcp://localhost:11000")) { while (true) { var productTohavePriceValidated = receiver.ReceiveFrameString(); Console.WriteLine(productTohavePriceValidated); var product = Newtonsoft.Json.JsonConvert.DeserializeObject <Product>(productTohavePriceValidated); var validationMessages = ValidateProduct(product); validationMessages.AddRange(ValidatePrice(product)); var message = new ValidatedProductMessage(product, identity, validationMessages.ToArray()); validationPush.SendFrame(Newtonsoft.Json.JsonConvert.SerializeObject(message)); } } }