Example #1
0
        public FoodItem SaveFood(FoodItem item)
        {
            verfiyScope();
            if (item.ID == 0)
            {
                ctx.Food.Add(item);
            }
            else
            {
                ctx.Food.Attach(item);
                ctx.Entry(item).State = EntityState.Modified;
            }

            ctx.SaveChanges();

            if (cfg.FeatureManagement.Reactive)
            {
                publisher.PublishEvent(item, FoodEventType.Update);
            }

            return(item);
        }
        public async void PublishEvent(FoodItem item, FoodEventType type)
        {
            var evtItem = new CloudEvent <FoodItem>(item);
            var client  = new HttpClient {
                BaseAddress = new Uri(cfg.Azure.EventGridEP)
            };

            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Add("aeg-sas-key", cfg.Azure.EventGridKey);
            var json     = JsonConvert.SerializeObject(evtItem);
            var content  = new StringContent(json, Encoding.UTF8, "application/json");
            var response = await client.PostAsync(string.Empty, content);

            //TODO: Update Event to use CloudEventV10 schema
            // EventGridEvent evt = new EventGridEvent()
            // {
            //     Id = Guid.NewGuid().ToString(),
            //     EventType = $"FoodApp.{type.ToString()}",
            //     Data = new FoodEventData()
            //     {
            //         id = item.ID,
            //         type = FoodEventType.Create.ToString(),
            //         quantity = 1
            //     },
            //     EventTime = DateTime.Now,
            //     Subject = "FoodEvent",
            //     DataVersion = "2.0"
            // };
            // List<CloudEvent<FoodItem>> events = new List<CloudEvent<FoodItem>> (){cloudEvent};

            // string topicHostname = new Uri(cfg.Azure.EventGridEP).Host;
            // TopicCredentials topicCredentials = new TopicCredentials(cfg.Azure.EventGridKey);
            // EventGridClient client = new EventGridClient(topicCredentials);
            // client.PublishEventsAsync (topicHostname, events).GetAwaiter ().GetResult ();
        }
Example #3
0
 public FoodItem SaveFood(FoodItem item)
 {
     ctx.Food.Add(item);
     ctx.SaveChanges();
     return(item);
 }
Example #4
0
 public ActionResult SendMail(FoodItem item)
 {
     FoodApp.GraphHelper.SendMail("Take a look at this great food", JsonSerializer.Serialize(item), new[] { config.App.mailSender }, config);
     return(Ok());
 }