Example #1
0
        public static Models.SurveyModel MapObject(this SurveyResultDto survey)
        {
            if (survey == null || survey.Survey == null)
            {
                return(null);
            }
            var o = survey.Survey;

            Models.SurveyModel result = new Models.SurveyModel();
            result.Id              = o.Id;
            result.Duration        = o.Duration;
            result.CreateDateTime  = o.CreateDateTime;
            result.Status          = o.Status;
            result.Questions       = o.Questions.Select(p => p.MapObject()).ToList();
            result.UserAnswers     = o.UserAnswers.Select(p => p.MapObject()).ToList();
            result.CreatorUserName = o.CreatorUserName;
            result.ValidOperations = survey.ValidOperations;
            return(result);
        }
        public static SurveyResultDto WithValidOperation(this Domain.Survey surveyDto)
        {
            if (surveyDto == null)
            {
                return(null);
            }
            SurveyResultDto resu = new SurveyResultDto();

            resu.ValidOperations = new List <ValidOperation>();
            resu.Survey          = surveyDto;
            switch (surveyDto.Status)
            {
            case SurveyStatus.New:
                resu.ValidOperations.Add(ValidOperation.Open);

                break;

            case SurveyStatus.Edit:
                resu.ValidOperations.Add(ValidOperation.Open);
                break;

            case SurveyStatus.Open:
                resu.ValidOperations.Add(ValidOperation.Close);
                if (!surveyDto.UserAnswers.Any(p => p.AnswerStatus == (int)SurveyAnswerStatus.Complete || p.AnswerStatus == (int)SurveyAnswerStatus.Initiated))
                {
                    resu.ValidOperations.Add(ValidOperation.Edit);
                }
                break;

            case SurveyStatus.Closed:
                break;

            default:
                break;
            }

            return(resu);
        }
Example #3
0
        public IHttpActionResult Submit(SurveyDto dto)
        {
            SurveyResultDto result = new SurveyResultDto();

            result.Code = 0;
            result.Msg  = "提交成功";
            if (dto == null)
            {
                result.Code = 401;
                result.Msg  = "输入错误";
                return(Ok(result));
            }

            for (int i = dto.Subject2.Count - 1; i >= 0; i--)
            {
                var obj = dto.Subject2[i] ?? "";
                int val = 0;
                if (int.TryParse(obj.ToString(), out val) && obj.GetType() == typeof(Int64))
                {
                    if (!(val >= 1 && val <= 6))
                    {
                        dto.Subject2.RemoveAt(i);
                    }
                }
            }

            //string userIp = GetHostAddress();
            //string userLocalId = Guid.NewGuid().ToString();
            //if (string.IsNullOrEmpty(dto.UserLocalId))
            //{
            //    dto.UserLocalId = userLocalId;
            //}

            //check dto validation
            if (dto.Subject1 <= 0 || dto.Subject1 > 4)
            {
                result.Code = 402;
                result.Msg  = "问题1请选择";
                return(Ok(result));
            }

            if (dto.Subject2.Distinct().Count() < 3)
            {
                result.Code = 403;
                result.Msg  = "问题2请选择3项";
                return(Ok(result));
            }
            //TODO: check dup if it's enabled.

            //turn to entity and save to db
            SurveyEntity entity = new SurveyEntity();

            entity.Subject1 = dto.Subject1;
            entity.Subject2 = JsonConvert.SerializeObject(dto.Subject2.Distinct().ToList());
            entity.Subject3 = HttpUtility.UrlDecode(dto.Subject3 ?? "").Trim();
            entity.Subject4 = HttpUtility.UrlDecode(dto.Subject4 ?? "").Trim();
            entity.Subject5 = HttpUtility.UrlDecode(dto.Subject5 ?? "").Trim();
            entity.Subject6 = HttpUtility.UrlDecode(dto.Subject6 ?? "").Trim();
            entity.Subject7 = HttpUtility.UrlDecode(dto.Subject7 ?? "").Trim();

            // #3. save to db
            _context.SurveyEntities.Add(entity);
            try
            {
                _context.SaveChanges();
            }
            catch
            {
                result.Code = 500;
                result.Msg  = "发生错误,请重试";
                return(Ok(result));
            }

            // #4. return value
            return(Ok(result));
        }