Esempio n. 1
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);
        }
Esempio n. 2
0
        public async Task <IActionResult> PutSurvey(int id, SurveyDto survey, bool activate = false)
        {
            var errorDictionary = ValidateSurveyForm(survey);

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

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

            model.Id = id;
            model.ModificationDate      = DateTime.Now;
            _context.Entry(model).State = EntityState.Modified;
            model.CreatorId             = int.Parse(User.FindFirstValue(ClaimTypes.Name));
            model.Active = activate;

            var oldQuestions = await _context.Questions.Where(x => x.SurveyId == id).ToListAsync();

            _context.Questions.RemoveRange(oldQuestions);
            await _context.SaveChangesAsync();

            foreach (var question in model.Questions)
            {
                question.Id = null;
            }

            await _context.Questions.AddRangeAsync(model.Questions);

            await _context.SaveChangesAsync();

            var addedSurvey = await GetSurvey(model.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(Ok());
        }