コード例 #1
0
        public async Task <ActionConfirmation> Create(Service service)
        {
            var validatorConfirmation = _serviceValidator.Validate(service, ActionType.Create);

            if (!validatorConfirmation.WasSuccessful)
            {
                return(validatorConfirmation);
            }

            if (_consensusContext.IsLeader())
            {
                var consensusResult = await _logReplication.DistributeEntry(service, MethodType.Create);

                if (consensusResult.WasSuccessful)
                {
                    _serviceRepository.Create(service);
                    if (ShouldExecuteHealthChecking(service))
                    {
                        _serviceHealthWorker.RunHealthChecker(service);
                    }
                }
                else
                {
                    return(ActionConfirmation.CreateError($"Distribute item to other services fails: '{consensusResult.Message}'."));
                }
            }
            else
            {
                _serviceRepository.Create(service);
            }

            return(ActionConfirmation.CreateSuccessful());
        }
コード例 #2
0
        public ActionConfirmation Validate(KvProperty kvProperty, ActionType actionType)
        {
            var errors = new List <ValidationError>();

            if (string.IsNullOrWhiteSpace(kvProperty.Key))
            {
                errors.Add(new ValidationError
                {
                    FieldName = nameof(kvProperty.Id),
                    Message   = "K/V property key wasn't specified"
                });
            }

            if (actionType == ActionType.Create && _kvPropertyRepository.Get(kvProperty.Key) != null)
            {
                errors.Add(new ValidationError
                {
                    FieldName = nameof(kvProperty.Key),
                    Message   = $"K/V property with id '{kvProperty.Key}' already exists."
                });
            }

            if (errors.Any())
            {
                return(ActionConfirmation.CreateError("There was an validation errors.", errors.ToArray()));
            }

            return(ActionConfirmation.CreateSuccessful());
        }
コード例 #3
0
        public async Task <ActionConfirmation> Delete(string id)
        {
            var service = _serviceRepository.Get(id);

            if (_consensusContext.IsLeader())
            {
                var consensusResult = await _logReplication.DistributeEntry(service, MethodType.Delete);

                if (consensusResult.WasSuccessful)
                {
                    _serviceRepository.Remove(id);
                    _serviceHealthWorker.StopHealthChecker(id);
                }
                else
                {
                    return(ActionConfirmation.CreateError($"Distribute item to other services fails: '{consensusResult.Message}'."));
                }
            }
            else
            {
                _serviceRepository.Remove(id);
            }

            return(ActionConfirmation.CreateSuccessful());
        }
コード例 #4
0
        public async Task <ActionConfirmation> Update(KvProperty kvProperty)
        {
            var validatorConfirmation = _kvPropertyValidator.Validate(kvProperty, ActionType.Update);

            if (!validatorConfirmation.WasSuccessful)
            {
                return(validatorConfirmation);
            }

            if (_consensusContext.IsLeader())
            {
                var consensusResult = await _logReplication.DistributeEntry(kvProperty, MethodType.Update);

                if (consensusResult.WasSuccessful)
                {
                    _kvPropertyRepository.Update(kvProperty);
                }
                else
                {
                    return(ActionConfirmation.CreateError($"Distribute item to other services fails: '{consensusResult.Message}'."));
                }
            }
            else
            {
                _kvPropertyRepository.Update(kvProperty);
            }

            return(ActionConfirmation.CreateSuccessful());
        }
コード例 #5
0
        public ActionConfirmation Validate(Service service, ActionType actionType)
        {
            var errors = new List <ValidationError>();

            var idPattern = new Regex("^[a-zA-Z0-9-]*$");

            if (!idPattern.IsMatch(service.Id))
            {
                errors.Add(new ValidationError
                {
                    FieldName = nameof(service.Id),
                    Message   = $"Service id contains unacceptable characters (only alphanumeric letters and dash is acceptable)."
                });
            }

            if (actionType == ActionType.Create && _serviceRepository.Get(service.Id) != null)
            {
                errors.Add(new ValidationError
                {
                    FieldName = nameof(service.Id),
                    Message   = $"Service with id '{service.Id}' already exists."
                });
            }

            if (string.IsNullOrWhiteSpace(service.Id))
            {
                errors.Add(new ValidationError
                {
                    FieldName = nameof(service.Id),
                    Message   = "Service id wasn't specified"
                });
            }

            if (string.IsNullOrWhiteSpace(service.ServiceType))
            {
                errors.Add(new ValidationError
                {
                    FieldName = nameof(service.ServiceType),
                    Message   = "Service type wasn't specified"
                });
            }

            if (string.IsNullOrWhiteSpace(service.Address))
            {
                errors.Add(new ValidationError
                {
                    FieldName = nameof(service.Address),
                    Message   = "Service address wasn't specified"
                });
            }

            if (errors.Any())
            {
                return(ActionConfirmation.CreateError("There was an validation errors.", errors.ToArray()));
            }

            return(ActionConfirmation.CreateSuccessful());
        }
コード例 #6
0
        public ActionResult Create(SpeechViewModel model)
        {
            var     currentUserId = this.CurrentUserSessionContext().UserId;
            Message message       = _messageService.CreateSpeech(model.Mind, model.Speech, currentUserId);

            if (message != null)
            {
                _unitOfWork.Commit();
                TempData[ActionConfirmation.TempDataKey] = ActionConfirmation.CreateSuccess("Your speech has been created.");
                return(RedirectToActionPermanent("Edit", new { @id = message.Id }));
            }

            TempData[ActionConfirmation.TempDataKey] = ActionConfirmation.CreateError("Your speech has not been created. Try again.");
            return(View(model));
        }
コード例 #7
0
        public async Task <ActionConfirmation <T> > SendRequest <T>(HttpMethod httpMethod, string path, string id, string query, T objectData) where T : class
        {
            var neutrinoAddress = GetNeutrinoLeaderAddress();
            var url             = neutrinoAddress.AppendPathSegment(path);

            if (!string.IsNullOrWhiteSpace(id))
            {
                url = url.AppendPathSegment(id);
            }

            if (!string.IsNullOrWhiteSpace(query))
            {
                if (query.StartsWith("?"))
                {
                    url.Query = query;
                }
                else
                {
                    url = url.AppendPathSegment(query);
                }
            }

            var responseMessage = await SendRequest(() => CreateHttpRequestMessage(httpMethod, url, objectData));

            if (responseMessage.IsSuccessStatusCode)
            {
                var objectStringFromResponse = await responseMessage.Content.ReadAsStringAsync();

                var objectFromResponse = default(T);

                if (!string.IsNullOrWhiteSpace(objectStringFromResponse))
                {
                    objectFromResponse = JsonConvert.DeserializeObject <T>(objectStringFromResponse);
                }

                return(ActionConfirmation <T> .CreateSuccessful(objectFromResponse));
            }

            var errorFromResponse = await responseMessage.Content.ReadAsStringAsync();

            var errorMessage = $"Request finished with status code: '{responseMessage.StatusCode}. Message from server: {errorFromResponse}.";

            return(ActionConfirmation.CreateError <T>(errorMessage));
        }
コード例 #8
0
        public ActionResult Edit(SpeechViewModel model)
        {
            var currentUserId = this.CurrentUserSessionContext().UserId;

            if (!_messageService.HasRightToMessage(currentUserId, model.MessageId))
            {
                return(new HttpAccessDeniedResult());
            }

            Message message = _messageService.UpdateSpeech(model.MessageId, model.Mind, model.Speech);

            if (message != null)
            {
                _unitOfWork.Commit();
                TempData[ActionConfirmation.TempDataKey] = ActionConfirmation.CreateSuccess("Your speech has been saved.");
                return(RedirectToActionPermanent("Edit", new { @id = message.Id }));
            }

            TempData[ActionConfirmation.TempDataKey] = ActionConfirmation.CreateError("Your speech has not been saved. Try again.");
            return(View(model));
        }
コード例 #9
0
        public async Task <ActionConfirmation> SendRequest(HttpMethod httpMethod, string path, string id = null)
        {
            var neutrinoAddress = GetNeutrinoLeaderAddress();
            var url             = neutrinoAddress.AppendPathSegment(path);

            if (!string.IsNullOrWhiteSpace(id))
            {
                url = url.AppendPathSegment(id);
            }

            var responseMessage = await SendRequest(() => CreateHttpRequestMessage(httpMethod, url, null));

            if (responseMessage.IsSuccessStatusCode)
            {
                return(ActionConfirmation.CreateSuccessful());
            }

            var errorFromResponse = await responseMessage.Content.ReadAsStringAsync();

            var errorMessage = $"Request finished with status code: '{responseMessage.StatusCode}. Message from server: {errorFromResponse}.";

            return(ActionConfirmation.CreateError(errorMessage));
        }