public async Task<IHttpActionResult> SubmitSurvey(dtoSurveyClient surveyClient) { dtoSurvey dtoSurvey = null; try { surveyClient.IPAddress = GetClientIp(); dtoSurvey = await _yuyanSvc.SaveSurveyClient(surveyClient); } catch (ApplicationException aex) { return BadRequest(aex.Message); } catch (Exception ex) { return InternalServerError(ex); } return Ok(dtoSurvey); }
public async Task<tbSurveyClient> SaveSurveyClient(dtoSurveyClient surveyClient) { tbSurveyClient sc = new tbSurveyClient(); try { sc.Email = surveyClient.Email; sc.IPAddress = surveyClient.IPAddress; sc.SurveyId = surveyClient.SurveyId; sc.TotalScore = surveyClient.TotalScore; sc.City = surveyClient.City; sc.State = surveyClient.State; sc.Country = surveyClient.Country; sc.CreatedDate = DateTime.UtcNow; if (surveyClient.City == null) { ip2location_db3 geoIp = await GetGeoLocationByIpAddress(surveyClient.IPAddress); sc.City = geoIp.city_name; sc.State = geoIp.region_name; sc.Country = geoIp.country_name; } _db.tbSurveyClients.Add(sc); if (surveyClient.dtoClientAnswers.Count() > 0) { foreach (dtoSurveyClientAnswer clientAnswer in surveyClient.dtoClientAnswers) { clientAnswer.ClientId = sc.ClientId; await SaveClientAnswer(clientAnswer); } } await _db.SaveChangesAsync(); } catch (DataException dex) { throw new ApplicationException("Data error!", dex); } return sc; }
public static dtoSurveyClient ConvertToDtoSurveyClient(this tbSurveyClient source, dtoSurveyClient data = null) { if (data == null) data = new dtoSurveyClient(); if (source == null) return null; data.ClientId = source.ClientId; data.Email = source.Email; data.IPAddress = source.IPAddress; data.SurveyId = source.SurveyId; data.TotalScore = source.TotalScore; data.City = source.City; data.State = source.State; data.Country = source.Country; IList<dtoSurveyClientAnswer> answerList = new List<dtoSurveyClientAnswer>(); if (source.tbClientAnswers != null) { foreach (tbSurveyClientAnswer answer in source.tbClientAnswers) { if(answer.IsChecked) answerList.Add(answer.ConvertToDtoAnswwer()); } } data.dtoClientAnswers = answerList; return data; }