Exemplo n.º 1
0
        public async Task <IActionResult> Update(int?id, [FromBody] SurveyDto dto)
        {
            _logger.LogInformation("Performing update request...");

            if (Util.IsAnyNullOrEmpty(dto) || id == null)
            {
                _logger.LogWarning("Body from request is empty.");
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            _logger.LogInformation("Updating record...");

            var response = await _surveyService.UpdateSurvey(id.Value, dto);

            if (response.Equals(ServiceResponseType.NotFound))
            {
                _logger.LogWarning("Record does not exist. Unable to update record.");
                return(NotFound());
            }

            _logger.LogInformation("Record updated successfully.");

            return(Ok());
        }
        public void ShouldReturnErrorMessageWhenQuestionGroupNotFound()
        {
            var options = SqliteInMemory.CreateOptions <SurveyDbContext>();

            using (var context = new SurveyDbContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDataBaseWithSurveys();

                var questionGroupId  = -1;
                var currentQuestions = context.Questions.Count();
                var questionDtos     = new List <QuestionDto>
                {
                    new QuestionDto
                    {
                        Text = "How well have you been sleeping?",
                        Type = 1
                    }
                };

                var surveyDto = new SurveyDto
                {
                    Id = 1,
                    QuestionGroupId = questionGroupId,
                    QuestionsDtos   = questionDtos
                };

                var service = new AddQuestionService(context, new MapQuestionDtoToQuestionsService());
                var result  = service.AddQuestion(surveyDto);
                result.ShouldNotBeNull();
                result.First().ErrorMessage.ShouldEqual($"Could not find QuestionGroup with an Id of {questionGroupId}");
                context.SaveChanges();
                context.Questions.Count().ShouldEqual(currentQuestions);
            }
        }
        public void ShouldReturnAnErrorMessageWhenGroupNoNameProvided()
        {
            var options = SqliteInMemory.CreateOptions <SurveyDbContext>();

            using (var context = new SurveyDbContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDataBaseWithSurveys();

                var currentQuestionGroups = context.QuestionGroups.Count();
                var questions             = EfTestData.CreateQuestionsDtos();

                var questionGroupsDto = new List <QuestionGroupDto>
                {
                    new QuestionGroupDto
                    {
                        Questions = questions
                    }
                };

                var surveyDto = new SurveyDto
                {
                    Id = 1,
                    QuestionGroupsDtos = questionGroupsDto
                };

                var service = new AddQuestionGroupService(context, new MapQuestionsToGroupService());
                var result  = service.AddQuestionGroup(surveyDto);
                result.ShouldNotBeNull();
                context.SaveChanges();
                result.First().ErrorMessage.ShouldEqual("A name is needed when creating a new question group.");
                context.QuestionGroups.Count().ShouldEqual(currentQuestionGroups);
            }
        }
        public void ShouldAddAQuestion()
        {
            var options = SqliteInMemory.CreateOptions <SurveyDbContext>();

            using (var context = new SurveyDbContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDataBaseWithSurveys();

                var currentQuestions = context.Questions.Count();
                var questionDtos     = new List <QuestionDto>
                {
                    new QuestionDto
                    {
                        Text = "How well have you been sleeping?",
                        Type = 1
                    }
                };

                var surveyDto = new SurveyDto
                {
                    Id = 1,
                    QuestionGroupId = 1,
                    QuestionsDtos   = questionDtos
                };

                var service = new AddQuestionService(context, new MapQuestionDtoToQuestionsService());
                var result  = service.AddQuestion(surveyDto);
                result.ShouldBeNull();
                context.SaveChanges();
                context.Questions.Count().ShouldEqual(currentQuestions + 1);
            }
        }
        public void ShouldReturnErrorMessageWhenQuestionTextNotSpecified()
        {
            var options = SqliteInMemory.CreateOptions <SurveyDbContext>();

            using (var context = new SurveyDbContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDataBaseWithSurveys();

                var currentQuestions = context.Questions.Count();
                var questionDtos     = new List <QuestionDto>
                {
                    new QuestionDto
                    {
                        Type = 1
                    }
                };

                var surveyDto = new SurveyDto
                {
                    Id = 1,
                    QuestionGroupId = 1,
                    QuestionsDtos   = questionDtos
                };

                var service = new AddQuestionService(context, new MapQuestionDtoToQuestionsService());
                var result  = service.AddQuestion(surveyDto);
                result.ShouldNotBeNull();
                result.First().ErrorMessage.ShouldEqual("A question has been submitted without any text.");
                context.SaveChanges();
                context.Questions.Count().ShouldEqual(currentQuestions);
            }
        }
Exemplo n.º 6
0
 public void TestSetup()
 {
     _survey = new SurveyDto {
         Id         = 2, CreatorName = "John Bleach", CreatedAt = DateTime.UtcNow,
         SurveyName = "Survey2", Description = "Some text 2", Questions = null
     };
 }
Exemplo n.º 7
0
        public async Task <ActionResult <SurveyDto> > AddSurvey(SurveyDto survey, bool activate = false)
        {
            var errorDictionary = ValidateSurveyForm(survey);

            if (errorDictionary.Count > 0)
            {
                return(BadRequest(new { errors = errorDictionary, code = 400 }));
            }

            var dbModel = survey.Adapt <Survey>();

            dbModel.Active           = activate;
            dbModel.CreatorId        = int.Parse(User.FindFirstValue(ClaimTypes.Name));
            dbModel.ModificationDate = DateTime.Now;
            await _context.Surveys.AddAsync(dbModel);

            await _context.SaveChangesAsync();

            var addedSurvey = await GetSurvey(dbModel.Id.Value);

            if (activate)
            {
                await _pushNotificationService.Send("New survey", $"New survey {addedSurvey.Value.Name} linked to your course {addedSurvey.Value.CourseName} has been added!", addedSurvey.Value.CourseSemesterName + addedSurvey.Value.CourseName);
            }

            return(addedSurvey);
        }
        public void ShouldAddQuestionGroup()
        {
            var options = SqliteInMemory.CreateOptions <SurveyDbContext>();

            using (var context = new SurveyDbContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDataBaseWithSurveys();

                var currentQuestionGroups = context.QuestionGroups.Count();
                var questions             = EfTestData.CreateQuestionsDtos();

                var questionGroupsDto = new List <QuestionGroupDto>
                {
                    new QuestionGroupDto
                    {
                        Name      = "Test QuestionGroup Dto",
                        Questions = questions
                    }
                };

                var surveyDto = new SurveyDto
                {
                    Id = 1,
                    QuestionGroupsDtos = questionGroupsDto
                };

                var service = new AddQuestionGroupService(context, new MapQuestionsToGroupService());
                var result  = service.AddQuestionGroup(surveyDto);
                result.ShouldBeNull();
                context.SaveChanges();
                context.QuestionGroups.Count().ShouldEqual(currentQuestionGroups + 1);
            }
        }
        public void ShouldReturnErrorMessageIfSurveyNotFound()
        {
            var options = SqliteInMemory.CreateOptions <SurveyDbContext>();

            using (var context = new SurveyDbContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDataBaseWithSurveys();

                var currentSurveys = context.Surveys.Count();

                var surveyDto = new SurveyDto
                {
                    Id = -1
                };

                var service = new RemoveSurveyService(context);

                var result = service.RemoveSurvey(surveyDto);

                result.ShouldNotBeNull();
                result.First().ErrorMessage.ShouldEqual($"A survey with the ID of {surveyDto.Id} was not found.");
                context.SaveChanges();
                context.Surveys.Count().ShouldEqual(currentSurveys);
            }
        }
Exemplo n.º 10
0
        [HttpPost]      // POST /api/survey
        public IActionResult AddSurvey(SurveyDto dto)
        {
            Survey survey = SurveyMapper.SurveyDtoToSurvey(dto);

            this.surveyService.AddEntity(survey);
            return(Ok());
        }
        public void ShouldReturnAnErrorMessageWhenNoNameProvided()
        {
            var options = SqliteInMemory.CreateOptions <SurveyDbContext>();

            using (var context = new SurveyDbContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDataBaseWithSurveys();

                var currentSurveys = context.Surveys.Count();
                var questionGroups = EfTestData.CreateQuestionGroupDtos();

                var surveyDto = new SurveyDto
                {
                    QuestionGroupsDtos = questionGroups,
                };

                var service = new AddSurveyService(context, new MapQuestionsToGroupService());
                var result  = service.AddSurvey(surveyDto);
                result.ShouldNotBeNull();
                context.SaveChanges();
                result.First().ErrorMessage.ShouldEqual("A name has not been provided for this survey.");
                context.Surveys.Count().ShouldEqual(currentSurveys);
            }
        }
Exemplo n.º 12
0
        public IImmutableList <ValidationResult> AddSurvey(SurveyDto surveyDto)
        {
            if (surveyDto.QuestionGroupsDtos == null)
            {
                Status.AddError("No question groups have been submitted with this survey.");
                return(Status.Errors);
            }

            if (string.IsNullOrWhiteSpace(surveyDto.Name))
            {
                Status.AddError("A name has not been provided for this survey.");
                return(Status.Errors);
            }

            var questionGroups = _questionMapper.Map(surveyDto.QuestionGroupsDtos, _context);
            var survey         = new Survey(surveyDto.Name, questionGroups);

            if (Status.HasErrors)
            {
                return(Status.Errors);
            }

            _context.Add(survey);
            _context.SaveChanges();
            return(null);
        }
        public void ShouldReturnAnErrorMessageIfNewNameNotProvided()
        {
            var options = SqliteInMemory.CreateOptions <SurveyDbContext>();

            using (var context = new SurveyDbContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDataBaseWithSurveys();

                var currentName = context.Surveys.First(m => m.SurveyId == 1).Name;

                var surveyDto = new SurveyDto
                {
                    Id      = 1,
                    NewName = ""
                };

                var service = new RenameSurveyService(context);

                var result = service.RenameSurvey(surveyDto);
                result.ShouldNotBeNull();
                result.First().ErrorMessage.ShouldEqual("No new name has been provided for this survey.");
                context.SaveChanges();
                context.Surveys.First(m => m.SurveyId == surveyDto.Id).Name.ShouldEqual(currentName);
            }
        }
Exemplo n.º 14
0
 public IActionResult Create(SurveyDto surveyDto)
 {
     if (SurveyService.Create(surveyDto) == null)
     {
         return(BadRequest());
     }
     return(Ok());
 }
Exemplo n.º 15
0
        public async Task <SurveyDto> Insert(SurveyDto request)
        {
            var entity = AutoMapper.Mapper.Map <SurveyEntity>(request);

            var result = await entityRepository.InsertAsync(entity);

            return(AutoMapper.Mapper.Map <SurveyDto>(result));
        }
Exemplo n.º 16
0
        public async Task AddSurvey(SurveyDto dto)
        {
            var survey = _mapper.Map <Survey>(dto);

            await _surveyRepository.AddAsync(survey);

            dto.Id = survey.Id;
        }
        public async Task <bool> UpdateEntityByIdAsync(SurveyDto request, int id)
        {
            var entity = _mapper.Map <SurveyDto, Survey>(request);

            entity.Id = id;
            var updated = await _uow.SurveyRepository.UpdateAsync(entity);

            return(await _uow.SaveAsync());
        }
        public async Task <SurveyDto> CreateEntityAsync(SurveyDto request)
        {
            var entity = _mapper.Map <SurveyDto, Survey>(request);

            entity = await _uow.SurveyRepository.CreateAsync(entity);

            var result = await _uow.SaveAsync();

            return(!result || entity == null ? null : _mapper.Map <Survey, SurveyDto>(entity));
        }
 public CreateSurvey(bool isTemplate = false)
 {
     InitializeComponent();
     Survey            = new SurveyDto(null, "", null, 0, new List <QuestionDto>());
     Survey.CourseId   = null;
     Survey.Name       = null;
     Survey.IsTemplate = isTemplate;
     QuestionsList     = new ObservableCollection <QuestionModel>();
     Initialize(true);
 }
Exemplo n.º 20
0
        public async Task <SurveyDto> Update(SurveyDto request)
        {
            var entity = AutoMapper.Mapper.Map <SurveyEntity>(request);

            request.IsDeleted = true;

            var result = await entityRepository.UpdateAsync(entity);

            return(AutoMapper.Mapper.Map <SurveyDto>(result));
        }
Exemplo n.º 21
0
        public static Survey SurveyDtoToSurvey(SurveyDto dto)
        {
            Survey survey = new Survey();

            survey.Title                = dto.Title;
            survey.CommentSurvey        = dto.CommentSurvey;
            survey.PublishingDate       = DateTime.Now;
            survey.Answers              = dto.Answers;
            survey.MedicalExaminationId = dto.MedicalExaminationID;
            return(survey);
        }
Exemplo n.º 22
0
 public HttpResponseMessage Put([FromBody] SurveyDto survey)
 {
     try
     {
         _service.UpdateSurvey(survey);
         return(Request.CreateResponse(HttpStatusCode.OK));
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message));
     }
 }
Exemplo n.º 23
0
 private SurveyTable ConvertSurveyDtoToTable(SurveyDto survey)
 {
     return(new SurveyTable
     {
         IdSurvey = survey.IdSurvey,
         IdUser = survey.IdUser,
         Question = survey.Question,
         Yes = survey.Yes,
         No = survey.No,
         Avoid = survey.Avoid,
         TypeMember = survey.TypeMember,
         EndTime = survey.EndTime,
         StartTime = survey.StartTime
     });
 }
Exemplo n.º 24
0
        private Dictionary <string, List <string> > ValidateSurveyForm(SurveyDto survey)
        {
            var errors = new Dictionary <string, List <string> >();

            if (_context.Surveys.Any(x => x.Name == survey.Name && x.IsTemplate == survey.IsTemplate && survey.Id != x.Id))
            {
                errors["Title"] = new List <string> {
                    "Survey form with that title already exists"
                }
            }
            ;

            return(errors);
        }
    }
Exemplo n.º 25
0
        public static SurveyDto SurveyToSurveyDto(Survey survey)
        {
            SurveyDto dto = new SurveyDto();

            dto.Title          = survey.Title;
            dto.CommentSurvey  = survey.CommentSurvey;
            dto.PublishingDate = survey.PublishingDate;
            dto.Answers        = new List <Answer>();
            foreach (Answer answer in survey.Answers)
            {
                dto.Answers.Add(new Answer(answer.QuestionId, answer.Grade));
            }

            return(dto);
        }
Exemplo n.º 26
0
        [HttpPost] // Since we are creating a resource we use HttpPost
        public IHttpActionResult CreateSurvey(SurveyDto surveyDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var survey = Mapper.Map <SurveyDto, Survey>(surveyDto);

            _context.Surveys.Add(survey);
            _context.SaveChanges();

            surveyDto.Id = survey.Id;

            return(Created(new Uri(Request.RequestUri + "/" + survey.Id), surveyDto));
        }
Exemplo n.º 27
0
        public ActionResult PutSurvey(int id, SurveyDto surveyDto)
        {
            var surveyInDb = _context.Surveys.SingleOrDefault(s => s.Id == id);

            if (surveyInDb == null)
            {
                return(NotFound());
            }

            surveyDto.Id = surveyInDb.Id;
            _mapper.Map(surveyDto, surveyInDb);

            _context.SaveChanges();

            return(Ok());
        }
Exemplo n.º 28
0
        public void Should_ReturnSurvey_When_BuildFromADto()
        {
            var dto = new SurveyDto
            {
                Elevator = "A",
                Shift    = "V",
                Floor    = "11"
            };

            var builder = new SurveyBuilder();
            var survey  = builder.Build(dto);

            Assert.IsTrue(survey.Floor == 11);
            Assert.IsTrue(survey.Shift == Shift.V);
            Assert.IsTrue(survey.Elevator == Elevator.A);
        }
Exemplo n.º 29
0
        public async Task <ServiceResponseType> UpdateSurvey(int id, SurveyDto dto)
        {
            var survey = await _surveyRepository.GetAsync(id);

            if (survey == null)
            {
                return(ServiceResponseType.NotFound);
            }

            survey.Name        = dto.Name ?? survey.Name;
            survey.Description = dto.Description ?? survey.Description;

            await _surveyRepository.Edit(survey);

            return(ServiceResponseType.Ok);
        }
Exemplo n.º 30
0
        public IImmutableList <ValidationResult> RemoveSurvey(SurveyDto dto)
        {
            var survey = _context.Find <Survey>(dto.Id);

            if (survey == null)
            {
                Status.AddError($"A survey with the ID of {dto.Id} was not found.");
                return(Status.Errors);
            }

            Status = survey.RemoveSelf(_context);
            if (Status.HasErrors)
            {
                return(Status.Errors);
            }
            _context.SaveChanges();
            return(null);
        }