Пример #1
0
        public async Task <IDisposable> AddSubscription <TEvent>(string sourceBoundedContextName,
                                                                 string publishedLanguageEntity)
            where TEvent : class
        {
            var queue = await _messageBus.QueueDeclareAsync(NamingConventions.QueueNamingConvention(_boundedContextName,
                                                                                                    sourceBoundedContextName, publishedLanguageEntity, _subscriberId));

            await _messageBus.BindAsync(
                new Exchange(
                    NamingConventions.ExchangeNamingConvention(sourceBoundedContextName, publishedLanguageEntity)),
                queue, "");

            return(_messageBus.Consume(queue, async(bytes, properties, info) =>
            {
                var msg = Encoding.UTF8.GetString(bytes);

                Console.WriteLine(msg);
                var validationResult = await _schemaValidator.IsValid <TEvent>(msg);
                if (validationResult.IsValid)
                {
                    var envelope = System.Text.Json.JsonSerializer.Deserialize <Envelope <TEvent> >(msg);
                    var props = new MessageProperties();
                    properties.CopyTo(props);
                    await _eventDispatcher.HandleEvent(envelope, props);
                }
                else
                {
                    throw new Exception($"Schema is invalid, errors: {string.Join(", ", validationResult.Errors)}");
                }
            }));
        }
Пример #2
0
        public async Task <ObjectResult> Create([FromBody] string classJson)
        {
            IList <string> errors;

            try
            {
                if (!_classSchemaValidator.IsValid(classJson, out errors))
                {
                    return(BadRequest(classJson));
                }

                var response = await _classProvider.Create(JsonConvert.DeserializeObject <Class>(classJson));

                if (response.Success)
                {
                    /*
                     * await _firehoseProvider.PublishAsync(Topics.Logs | Topics.SearchIndexes, new Activity<IClass>()
                     *  {
                     *      Id = Guid.NewGuid().ToString(),
                     *      ActivityType = ActivityType.ClassCreate,
                     *      Timestamp = DateTime.UtcNow,
                     *      Payload = new ActivityPayload<IClass>(){
                     *          Data = response.Data
                     *      }
                     *  }
                     * );
                     */
                    await _searchProvider.PublishToElastic(response.Data);

                    return(StatusCode((int)HttpStatusCode.Created, response));
                }
                else
                {
                    /*
                     * await _firehoseProvider.PublishAsync(Topics.Logs, new Activity<string>(){ Id = Guid.NewGuid().ToString(),
                     *                                                                    Timestamp = DateTime.UtcNow,
                     *                                                                    Payload = new ActivityPayload<string>()
                     *                                                                              { Data = string.Format($"ClassesController: Create(), RequestJson: {classJson}, ErrorResponse: {response}")},
                     *                                                                    ActivityType = ActivityType.Log});
                     */
                    return(StatusCode((int)HttpStatusCode.InternalServerError, response));
                }
            }
            catch (Exception ex)
            {
                /*
                 * await _firehoseProvider.PublishAsync(Topics.Logs, new Activity<string>(){ Id = Guid.NewGuid().ToString(),
                 *                                                                        Timestamp = DateTime.UtcNow,
                 *                                                                        Payload = new ActivityPayload<string>()
                 *                                                                                  { Data = string.Format($"ClassesController: Create(), RequestJson: {studentJson}, Exception: {ex}")},
                 *                                                                        ActivityType = ActivityType.Log});
                 */
                return(StatusCode((int)HttpStatusCode.InternalServerError, new ProviderResponse <IClass>()
                {
                    Success = false, ResponseStatus = CustomResponseErrors.ClassCreateFailed.ToString("f"),
                    Exception = ex, Message = ex.Message
                }));
            }
        }