public async Task<IHttpActionResult> GetSurveyByURLToken(string urltoken) { dtoSurvey dtoSurvey = new dtoSurvey(); try { dtoSurvey = await _yuyanSvc.GetSurveyByURLToken(urltoken); if (dtoSurvey == null) return NotFound(); dtoSurveyShare newShare = new dtoSurveyShare() { SurveyId = dtoSurvey.SurveyId, IPAddress = GetClientIp() }; await _yuyanSvc.AddSurveyShare(newShare); } catch (ApplicationException aex) { return BadRequest(aex.Message); } catch (Exception ex) { return InternalServerError(ex); } return Ok(dtoSurvey); }
public async Task TestController_CreateSurvey() { using (YuYanDBContext db = new YuYanDBContext()) using (YuYanDBRepository repos = new YuYanDBRepository(db)) { YuYanService svc = new YuYanService(repos); var controller = new SurveyController(svc); dtoSurvey testObj = new dtoSurvey(); testObj.Title = "Make me happy"; testObj.ShortDesc = "No short description"; var result = await controller.CreateSurvey(testObj); Assert.IsNotNull(result); } }
public async Task<IHttpActionResult> GetSurveyBySurveyId(int surveyId) { dtoSurvey dtoSurvey = new dtoSurvey(); try { dtoSurvey = await _yuyanSvc.GetSurveyBySurveyId(surveyId); if (dtoSurvey == null) return NotFound(); } catch (ApplicationException aex) { return BadRequest(aex.Message); } catch (Exception ex) { return InternalServerError(ex); } return Ok(dtoSurvey); }
public async Task<tbSurvey> UpdateSurvey(dtoSurvey survey) { tbSurvey theSurvey = null; try { theSurvey = await _db.tbSurveys.FirstOrDefaultAsync(x => x.SurveyId == survey.SurveyId && (x.IsActive ?? true) && !(x.IsDeleted ?? false)); if (theSurvey != null) { theSurvey.Title = survey.Title; theSurvey.ShortDescription = survey.ShortDesc; theSurvey.LongDescription = survey.LongDesc; theSurvey.UpdatedDate = DateTime.UtcNow; await _db.SaveChangesAsync(); } } catch (DataException dex) { throw new ApplicationException("Data error!", dex); } return theSurvey; }
public async Task<tbSurvey> CreateNewSurvey(dtoSurvey survey) { tbSurvey newSurvey = new tbSurvey(); try { newSurvey.Title = survey.Title; newSurvey.Slug = survey.Slug; newSurvey.URLToken = Convert.ToBase64String(Guid.NewGuid().ToByteArray()).Replace("=", "").Replace("/", "").Replace("+", ""); newSurvey.UserId = survey.UserId; newSurvey.ShortDescription = survey.ShortDesc; newSurvey.LongDescription = survey.LongDesc; newSurvey.CreatedDate = DateTime.UtcNow; newSurvey.UpdatedDate = DateTime.UtcNow; newSurvey.IsActive = true; newSurvey.IsDeleted = false; _db.tbSurveys.Add(newSurvey); if (survey.dtoQuestions != null) { if (survey.dtoQuestions.Count() > 0) { // do the question create foreach (dtoSurveyQuestion question in survey.dtoQuestions) { question.SurveyId = newSurvey.SurveyId; await CreateNewQuestion(question); } } } await _db.SaveChangesAsync(); } catch (DataException dex) { throw new ApplicationException("Data error!", dex); } return newSurvey; }
public async Task<IHttpActionResult> CreateSurvey([FromBody] dtoSurvey survey) { dtoSurvey dtoSurvey = new dtoSurvey(); try { var user = ControllerContext.RequestContext.Principal as YYUser; if (user != null) survey.UserId = user.UserId; dtoSurvey = await _yuyanSvc.CreateSurvey(survey); } catch (ApplicationException aex) { return BadRequest(aex.Message); } catch (Exception ex) { return InternalServerError(ex); } return Ok(dtoSurvey); }
public async Task<IHttpActionResult> UpdateSurvey(int surveyId, [FromBody] dtoSurvey survey) { dtoSurvey dtoSurvey = new dtoSurvey(); try { survey.SurveyId = surveyId; dtoSurvey = await _yuyanSvc.UpdateSurvey(survey); } catch (ApplicationException aex) { return BadRequest(aex.Message); } catch (Exception ex) { return InternalServerError(ex); } return Ok(dtoSurvey); }
public static dtoSurvey ConvertToDtoSurvey(this tbSurvey source, dtoSurvey data = null) { if (data == null) data = new dtoSurvey(); if (source == null) return null; data.SurveyId = source.SurveyId; data.Title = source.Title; data.Slug = source.Slug; data.URLToken = source.URLToken; data.ShortDesc = source.ShortDescription; data.LongDesc = source.LongDescription; data.UserId = source.UserId; data.IsActive = source.IsActive; data.IsDeleted = source.IsDeleted; IList<dtoSurveyQuestion> questionList = new List<dtoSurveyQuestion>(); if (source.tbSurveyQuestions != null) { foreach (tbSurveyQuestion question in source.tbSurveyQuestions) { if ((question.IsActive ?? true) && !(question.IsDeleted ?? false)) questionList.Add(question.ConvertToDtoSurveyQuestion()); } } data.dtoQuestions = questionList; return data; }