Exemple #1
0
        public async Task SaveChangesAsync_GivenNewSurvery_ShouldSetCreatedProperties()
        {
            var survey = new SurveyShrike_API.Domain.Entities.Survey()
            {
                Id          = Guid.NewGuid(),
                Title       = "Test",
                CreatedBy   = "Test",
                isDeleted   = false,
                ModifiedBy  = "Test",
                SurveyForms = new List <SurveyFormField>()
                {
                    new SurveyFormField()
                    {
                        FormConfiguration = "{order : 1 }",
                        FormTypes         = SurveyShrike_API.Common.SurveyFieldType.SingleLineInput,
                        Id    = 2,
                        Title = "Test title"
                    }
                }
            };

            _context.Surveys.Add(survey);

            await _context.SaveChangesAsync();

            survey.CreatedOn.ShouldNotBeNull();
            survey.CreatedOn.ShouldBeLessThan(DateTime.Now);
        }
            /// <summary>
            /// Handle is called by mediaR to perform the operation and notify the event subscriber.
            /// </summary>
            /// <param name="request">request object</param>
            /// <param name="cancellationToken">cancellation token</param>
            /// <returns>Unit</returns>
            public async Task <Unit> Handle(CreateSurveyCommand request, CancellationToken cancellationToken)
            {
                var email = await _getUserInformation.GetUser();

                var entity = new SurveyEntity.Survey
                {
                    Title      = request.Title,
                    CreatedBy  = email,
                    CreatedOn  = DateTime.UtcNow,
                    ModifiedBy = email,
                    ModifiedOn = DateTime.UtcNow,
                    isDeleted  = false
                };

                entity.SurveyForms = new List <SurveyEntity.SurveyFormField>();
                if (request.FormFields != null && request.FormFields.Count > 0)
                {
                    request.FormFields.ToList().ForEach(x =>
                    {
                        entity.SurveyForms.Add(new SurveyEntity.SurveyFormField()
                        {
                            FormConfiguration = x.FormConfiguration,
                            FormTypes         = x.FormTypes,
                            Title             = x.Title
                        });
                    });
                }

                _context.Surveys.Add(entity);

                await _context.SaveChangesAsync(cancellationToken);

                await _mediator.Publish(new SurveyCreated(), cancellationToken);

                return(Unit.Value);
            }