示例#1
0
        public bool OnLogReplication(AppendEntriesEvent appendEntriesEvent)
        {
            if (appendEntriesEvent.Entries != null && appendEntriesEvent.Entries.Count > 0)
            {
                foreach (var item in appendEntriesEvent.Entries)
                {
                    if (item.ObjectType == typeof(ServiceHealth).FullName)
                    {
                        var serviceHealth = JsonConvert.DeserializeObject <ServiceHealth>(item.Value.ToString());
                        _serviceHealthService.Create(serviceHealth.ServiceId, serviceHealth);
                    }
                    else if (item.ObjectType == typeof(Service).FullName)
                    {
                        var serviceData = JsonConvert.DeserializeObject <Service>(item.Value.ToString());
                        switch (item.Method)
                        {
                        case MethodType.Create:
                            _servicesService.Create(serviceData);
                            break;

                        case MethodType.Update:
                            _servicesService.Update(serviceData);
                            break;

                        case MethodType.Delete:
                            _servicesService.Delete(serviceData.Id);
                            break;
                        }
                    }
                    else if (item.ObjectType == typeof(KvProperty).FullName)
                    {
                        var kvProperty = JsonConvert.DeserializeObject <KvProperty>(item.Value.ToString());
                        switch (item.Method)
                        {
                        case MethodType.Create:
                            _kvPropertyService.Create(kvProperty);
                            break;

                        case MethodType.Update:
                            _kvPropertyService.Update(kvProperty);
                            break;

                        case MethodType.Delete:
                            _kvPropertyService.Delete(kvProperty.Key);
                            break;
                        }
                    }
                }
            }

            return(true);
        }
        public async Task <ActionResult> Post([FromBody] KvProperty kvProperty)
        {
            if (!_consensusContext.IsLeader())
            {
                return(Redirect($"{_consensusContext.NodeVote.LeaderNode.Address}/api/key-values"));
            }

            var actionConfirmation = await _kvPropertyService.Create(kvProperty);

            if (actionConfirmation.WasSuccessful)
            {
                return(Created($"api/key-values/{kvProperty.Key}", kvProperty));
            }
            else
            {
                return(BadRequest(actionConfirmation));
            }
        }