Exemple #1
0
        public async Task BindModelAsync(ModelBindingContext bindingContext)
        {
            if (bindingContext == null)
            {
                throw new ArgumentNullException(nameof(bindingContext));
            }

            if (bindingContext.ModelMetadata.ModelType != typeof(SaveSurveyViewModel))
            {
                return;
            }

            var sr = new StreamReader(bindingContext.HttpContext.Request.Body);

            var body = await sr.ReadToEndAsync();

            Console.WriteLine(await sr.ReadToEndAsync());

            JToken token      = JObject.Parse(body);
            var    serializer = new Newtonsoft.Json.JsonSerializer()
            {
                Converters        = { new QuestionJsonConverter() },
                NullValueHandling = NullValueHandling.Include,
                TypeNameHandling  = TypeNameHandling.None,
                Formatting        = Formatting.Indented
            };
            SaveSurveyViewModel survey = token.ToObject <SaveSurveyViewModel>(serializer);

            bindingContext.Result = ModelBindingResult.Success(survey);
        }
Exemple #2
0
        public async Task <ActionResult> SaveSurvey([FromBody] SaveSurveyViewModel survey)
        {
            if (survey == null)
            {
                return(BadRequest("survey not specified"));
            }

            var command = new UpdateSurveyCommand()
            {
                Survey = new Survey()
                {
                    Id                    = survey.SurveyId,
                    Title                 = survey.SurveyName,
                    Description           = survey.Description,
                    Questions             = survey.QuestionDefinitions,
                    SurveyPassAccessLevel = (SurveyPassAccessLevel)survey.Access
                }
            };

            CommandResult result = await _commandDispatcher.ExecuteAsync(command);

            if (result.IsSuccess)
            {
                return(Ok(new OkServiceResponse <string>("saved")));
            }

            if (result.Error != null)
            {
                return(BadRequest(new ErrorServiceRespose(result.Error)));
            }

            return(BadRequest(new ErrorServiceRespose("Unexpected error")));
        }