public async Task InvokeAsync(HttpContext context, ITopicsRepository topicsRepository, IMapper mapper, DefaultEventGridEventHandler eventHandler, IOptions <AwesomeEventGridOptions> options)
        {
            try
            {
                ModelState.Reset();

                using (var reader = new StreamReader(context.Request.Body, Encoding.UTF8))
                {
                    var topicToCreate = JsonConvert.DeserializeObject <TopicModel>(await reader.ReadToEndAsync(), options.Value.SerializerSettings);
                    if (topicToCreate == null)
                    {
                        ModelState.AddError("", "Request body is required");
                    }

                    Validate(topicToCreate);

                    if (!ModelState.IsValid)
                    {
                        await BadRequest(context);

                        return;
                    }

                    if (topicsRepository.FindByName(topicToCreate.Name) != null)
                    {
                        ModelState.AddError("name", "Topic does already exists");
                        await BadRequest(context);

                        return;
                    }

                    var topic = mapper.Map <Topic>(topicToCreate);
                    topic = topicsRepository.Add(topic);

                    var topicModel = mapper.Map <TopicModel>(topic);
                    //todo fix url:
                    await CreatedAt(context, "http://foo", topicModel);
                }
            }
            catch (JsonException ex)
            {
                ModelState.AddError("", ex.Message);
                await BadRequest(context);
            }
        }
Beispiel #2
0
        public async Task InvokeAsync(HttpContext context, ITopicsRepository topicsRepository, IMapper mapper)
        {
            ModelState.Reset();

            var name  = (string)context.GetRouteData().Values["name"];
            var topic = topicsRepository.FindByName(name);

            if (topic == null)
            {
                ModelState.AddError("name", "Topic with this name not found");
                await NotFound(context);
            }
            else
            {
                var model = mapper.Map <TopicModel>(topic);
                await Ok(context, model);
            }
        }
        public async Task InvokeAsync(HttpContext context, ITopicsRepository topicsRepository, IMapper mapper, IEventGridEventHandler eventGridEventHandler)
        {
            var options = context.RequestServices.GetService <IOptions <AwesomeEventGridOptions> >();

            ModelState.Reset();
            var topic = (string)context.GetRouteData().Values["name"];

            if (topicsRepository.FindByName(topic) == null)
            {
                ModelState.AddError("name", "Topic does not exists");
                await BadRequest(context);
            }
            else
            {
                using (var reader = new StreamReader(context.Request.Body, Encoding.UTF8))
                {
                    var eventsToPublish = JsonConvert.DeserializeObject <EventModel[]>(await reader.ReadToEndAsync(), options.Value.SerializerSettings);
                    await eventGridEventHandler.HandleAsync(topic, eventsToPublish);

                    await Accepted(context);
                }
            }
        }
Beispiel #4
0
        public async Task InvokeAsync(HttpContext context, IHttpClientFactory httpClientFactory, ISubscriptionsRepository subscriptionsRepository, ITopicsRepository topicsRepository, IMapper mapper, IEventGridEventHandler eventGridEventHandler, IOptions <AwesomeEventGridOptions> options)
        {
            try
            {
                ModelState.Reset();

                using (var reader = new StreamReader(context.Request.Body, Encoding.UTF8))
                {
                    var routeData = context.GetRouteData();
                    var topic     = (string)routeData.Values["topic"];

                    if (topicsRepository.FindByName(topic) == null)
                    {
                        ModelState.AddError("name", "Topic with this name not found");
                        await NotFound(context);

                        return;
                    }

                    var subscriptionModel = JsonConvert.DeserializeObject <SubscriptionModel>(await reader.ReadToEndAsync(), options.Value.SerializerSettings);

                    var subscription = mapper.Map <Subscription>(subscriptionModel);

                    subscription.Topic = topic;
                    var client = httpClientFactory.CreateClient();

                    var validation = new SubscriptionValidationEventDataModel()
                    {
                        ValidationCode = Guid.NewGuid().ToString()
                    };

                    var source = $"{options.Value.TopicsPath}/{topic}#validation";

                    var validationEvent = new EventModel
                    {
                        EventType = "subscriptions.validate",
                        Source    = new Uri(source, UriKind.Relative),
                        Data      = validation,
                    };
                    var response = await client.PostAsJsonAsync(subscription.EndpointUrl, validationEvent);

                    response.EnsureSuccessStatusCode();
                    var content = await response.Content.ReadAsAsync <SubscriptionValidationResponse>();

                    if (content.ValidationResponse != validation.ValidationCode)
                    {
                        ModelState.AddError("", "Endpoint responded with invalid validation code.");
                        await BadRequest(context);

                        return;
                    }


                    var newSubscription = subscriptionsRepository.AddOrUpdate(subscription);
                    var model           = mapper.Map <SubscriptionModel>(newSubscription);

                    var link = "http://link"; // todo
                    await CreatedAt(context, link, model);

                    //    }
                    //    catch (HttpRequestException ex)
                    //    {
                    //        return BadRequest(ex.Message);
                    //    }
                }
            }
            catch (JsonException ex)
            {
                ModelState.AddError("", ex.Message);
                await BadRequest(context);
            }
            catch (HttpRequestException ex)
            {
                ModelState.AddError("", ex.Message);
                await BadRequest(context);
            }
        }