Пример #1
0
        public async Task <ResponseViewModel> CreateResponse(NewResponseViewModel response, ObjectId formId)
        {
            FormObjectViewModel form = await GetForm(formId);

            List <FieldViewModel> fields = form.fields;

            if (fields.Count != response.responseValues.Count)
            {
                throw new Exception("Responses do not match fields");
            }

            bool fieldsValid = ResponseUtils.ResponseValidator(new List <FieldViewModel>(fields),
                                                               new List <NewResponseValuesViewModel>(response.responseValues));

            if (!fieldsValid)
            {
                throw new Exception("Responses do not match fields");
            }

            List <ResponseValueViewModel> responseValues = new List <ResponseValueViewModel>();

            foreach (var responseValue in response.responseValues)
            {
                if (!TypeConstants.IsValidFieldType(responseValue.responseType))
                {
                    throw new Exception("Invalid Response Type");
                }

                ResponseValueViewModel responseValueViewModel = new ResponseValueViewModel
                {
                    Id           = ObjectId.GenerateNewId(),
                    responseType = responseValue.responseType,
                    value        = responseValue.value,
                    index        = responseValue.index,
                    fieldId      = ObjectId.Parse(responseValue.fieldId)
                };

                string responseType = responseValue.responseType;
                if (responseType == TypeConstants.CHECKBOX_INPUT)
                {
                    var values = Constants.ConvertJsonObject(responseValue.value);
                    responseValueViewModel.value = values;
                }

                responseValues.Add(responseValueViewModel);
            }

            ResponseViewModel responseViewModel = new ResponseViewModel
            {
                Id             = ObjectId.GenerateNewId(),
                createdBy      = response.createdBy,
                createdAt      = DateTime.UtcNow,
                formId         = formId,
                responseValues = responseValues
            };

            await responseCollection.InsertOneAsync(responseViewModel);

            return(responseViewModel);
        }
Пример #2
0
        public async Task <FormObjectViewModel> CreateForm(NewFormViewModel form)
        {
            ObjectId formId = ObjectId.GenerateNewId();
            List <FieldViewModel> fields = new List <FieldViewModel>();

            foreach (var field in form.fields)
            {
                string fieldType = field.fieldType;
                if (!TypeConstants.IsValidFieldType(fieldType))
                {
                    throw new Exception("Invalid Field Type");
                }

                FieldViewModel fieldViewModel = new FieldViewModel
                {
                    Id        = ObjectId.GenerateNewId(),
                    formId    = formId,
                    fieldType = field.fieldType,
                    index     = field.index,
                    title     = field.title,
                    createdAt = DateTime.UtcNow
                };

                if (fieldType == TypeConstants.CHECKBOX_INPUT || fieldType == TypeConstants.DROP_DOWN_INPUT ||
                    fieldType == TypeConstants.RADIO_INPUT)
                {
                    var values = Constants.ConvertJsonObject(field.value);
                    fieldViewModel.value = values;
                }
                else
                {
                    fieldViewModel.value = field.value;
                }

                fields.Add(fieldViewModel);
            }

            if (fields.Count != 0)
            {
                await fieldCollection.InsertManyAsync(fields);
            }

            IEnumerable <ObjectId> fieldIds = from _ in fields
                                              select _.Id;

            FormViewModel formViewModel = new FormViewModel
            {
                Id        = formId,
                createdAt = DateTime.UtcNow,
                createdBy = form.createdBy,
                formTitle = form.title,
                fields    = fieldIds.ToList()
            };

            await formCollection.InsertOneAsync(formViewModel);

            return(FormUtils.CombineFormAndFields(formViewModel, fields));
        }
Пример #3
0
        public async Task <FieldViewModel> AddNewFieldToForm(NewFieldViewModel field, ObjectId formId)
        {
            ObjectId fieldObjectId = ObjectId.GenerateNewId();
            string   fieldType     = field.fieldType;

            if (!TypeConstants.IsValidFieldType(fieldType))
            {
                throw new Exception("Invalid Field Type");
            }

            FieldViewModel fieldViewModel = new FieldViewModel
            {
                Id        = fieldObjectId,
                formId    = formId,
                createdAt = DateTime.UtcNow,
                index     = field.index,
                fieldType = field.fieldType,
                title     = field.title
            };

            if (fieldType == TypeConstants.CHECKBOX_INPUT || fieldType == TypeConstants.DROP_DOWN_INPUT ||
                fieldType == TypeConstants.RADIO_INPUT)
            {
                var values = Constants.ConvertJsonObject(field.value);
                fieldViewModel.value = values;
            }
            else
            {
                fieldViewModel.value = field.value;
            }

            await formCollection.UpdateOneAsync(_ => _.Id == formId,
                                                Builders <FormViewModel> .Update.Push <ObjectId>(_ => _.fields, fieldObjectId));

            await fieldCollection.InsertOneAsync(fieldViewModel);

            return(fieldViewModel);
        }
Пример #4
0
        public async Task <FieldViewModel> UpdateField(NewFieldViewModel field, ObjectId formId, ObjectId fieldId)
        {
            if (!TypeConstants.IsValidFieldType(field.fieldType))
            {
                throw new Exception("Invalid Field Type");
            }

            string fieldType  = field.fieldType;
            object fieldValue = field.value;

            if (fieldType == TypeConstants.CHECKBOX_INPUT || fieldType == TypeConstants.DROP_DOWN_INPUT ||
                fieldType == TypeConstants.RADIO_INPUT)
            {
                var values = Constants.ConvertJsonObject(field.value);
                fieldValue = values;
            }

            UpdateResult fieldUpdateResult = await fieldCollection.UpdateOneAsync(
                _ => _.Id == fieldId && _.formId == formId,
                Builders <FieldViewModel> .Update
                .Set(_ => _.title, field.title)
                .Set(_ => _.fieldType, field.fieldType)
                .Set(_ => _.index, field.index)
                .Set(_ => _.value, fieldValue));

            if (fieldUpdateResult.IsAcknowledged)
            {
                var fieldTask = await fieldCollection.FindAsync(_ => _.Id == fieldId);

                return(await fieldTask.SingleOrDefaultAsync());
            }
            else
            {
                throw new Exception("Unable to update field content");
            }
        }