Beispiel #1
0
        public async Task <ActionResult <apple.Survey> > PostSurvey(apple.Survey survey)
        {
            survey.UserId = GetUserId();
            if (SurveyExists(survey.Id))
            {
                await PutSurvey(survey.Id, survey);

                return(Ok(survey));
            }
            else
            {
                _context.Surveys.Add(survey);
                try
                {
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateException)
                {
                    if (SurveyExists(survey.Id))
                    {
                        // This shouldn't really be hit, given the previous check.
                        return(Conflict());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(CreatedAtAction("GetSurvey", new { id = survey.Id }, survey));
            }
        }
Beispiel #2
0
        public async Task <ActionResult <apple.Survey> > PutSurvey(int id, apple.Survey survey)
        {
            if (id != survey.Id)
            {
                return(BadRequest());
            }
            _context.Entry(survey).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!SurveyExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtAction("GetSurvey", new { id = survey.Id }, survey));
        }
Beispiel #3
0
        public async Task <shared.Survey> UpdateSurveyAsync(shared.Survey existingSurvey)
        {
            HttpResponseMessage response = await _client.PutAsJsonAsync(API_PREFIX + "/" + existingSurvey.Id, existingSurvey);

            response.EnsureSuccessStatusCode();

            return(await response.Content.ReadFromJsonAsync <shared.Survey>());;
        }
Beispiel #4
0
        public async Task <shared.Survey> CreateSurveyAsync(shared.Survey NewSurvey)
        {
            NewSurvey.Id          = 0;
            NewSurvey.DateCreated = DateTime.Now;
            HttpResponseMessage response = await _client.PostAsJsonAsync(API_PREFIX, NewSurvey);

            response.EnsureSuccessStatusCode();

            return(await response.Content.ReadFromJsonAsync <shared.Survey>());
        }
Beispiel #5
0
        public async Task <bool> DeleteSurveyAsync(shared.Survey existingSurvey)
        {
            try
            {
                HttpResponseMessage response = await _client.DeleteAsync(API_PREFIX + "/" + existingSurvey.Id);

                response.EnsureSuccessStatusCode();
            }
            catch (Exception e)
            {
                return(false);
            }
            return(true);
        }
Beispiel #6
0
        public async Task <ActionResult <apple.Survey> > GetSurvey(int id)
        {
            apple.Survey survey = await _context.Surveys
                                  .Include(surv => surv.SurveyItems)
                                  .ThenInclude(item => item.SurveyItemOptions)
                                  .Include(surv => surv.SurveyItems)
                                  .ThenInclude(item => item.SurveyAnswers)
                                  .FirstOrDefaultAsync(surv => surv.Id == id);

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

            return(survey);
        }
Beispiel #7
0
        public DTOSurvey ConvertSurveyToDTO(shared.Survey objSurvey)
        {
            DTOSurvey objDTOSurvey = new DTOSurvey();

            objDTOSurvey.Id         = objSurvey.Id;
            objDTOSurvey.SurveyName = objSurvey.SurveyName;

            objDTOSurvey.SurveyItems = new List <DTOSurveyItem>();

            foreach (var SurveyItem in objSurvey.SurveyItems)
            {
                DTOSurveyItem objDTOSurveyItem = new DTOSurveyItem();

                objDTOSurveyItem.Id        = SurveyItem.Id;
                objDTOSurveyItem.ItemLabel = SurveyItem.ItemLabel;
                objDTOSurveyItem.ItemType  = SurveyItem.ItemType;
                objDTOSurveyItem.Position  = SurveyItem.Position;
                objDTOSurveyItem.Required  = SurveyItem.Required;

                objDTOSurveyItem.SurveyItemOptions =
                    new List <DTOSurveyItemOption>();

                foreach (var SurveyItemOption in SurveyItem.SurveyItemOptions.OrderBy(x => x.Id))
                {
                    DTOSurveyItemOption objDTOSurveyItemOption = new DTOSurveyItemOption();

                    objDTOSurveyItemOption.Id          = SurveyItemOption.Id;
                    objDTOSurveyItemOption.OptionLabel = SurveyItemOption.OptionLabel;

                    objDTOSurveyItem.SurveyItemOptions.Add(objDTOSurveyItemOption);
                }

                objDTOSurvey.SurveyItems.Add(objDTOSurveyItem);
            }
            return(objDTOSurvey);
        }