public async Task <IActivityAndChallengesResponse> GetReferenceAsync(int profileId, int activityId) { try { var userInfo = await _appDbContext.UserInfos.FirstOrDefaultAsync(k => k.Id == profileId); if (userInfo == null) { return(new ActivityAndChallengesResponse(ClientMessageConstant.ProfileNotExist, HttpStatusCode.NotFound)); } var initiativeProfile = await _appDbContext.InitiativeProfiles.FirstOrDefaultAsync(k => k.ProfileId == profileId && k.InitiativeId == activityId); var initiative = await _appDbContext.Initiatives.FirstOrDefaultAsync(k => k.Id == activityId); if (initiativeProfile == null) { if (initiative != null && initiative.QuestionGroupId == null) { initiativeProfile = new InitiativeProfile() { InitiativeId = activityId, ProfileId = profileId, StatusItemId = (int)InitiativeStatus.Accepted, CreatedBy = userInfo.Email, Created = DateTime.Now, Modified = DateTime.Now, ModifiedBy = userInfo.Email }; await _appDbContext.InitiativeProfiles.AddAsync(initiativeProfile); initiative.RemainingSeatsCount = initiative.RemainingSeatsCount - 1; await _appDbContext.SaveChangesAsync(); } else { return(new ActivityAndChallengesResponse(ClientMessageConstant.FileNotFound, HttpStatusCode.NotFound)); } } var parti = await _appDbContext.ParticipationReferences.FirstOrDefaultAsync(k => k.InitiativeParticipationID == initiativeProfile.Id && k.ProfileID == profileId); if (parti == null) { parti = await GetReferenceAsync(initiativeProfile.Id, activityId, userInfo); initiativeProfile.ParticipationReferenceID = parti.Id; await _appDbContext.SaveChangesAsync(); } if (initiative != null && initiative.QuestionGroupId != null) { initiativeProfile.StatusItemId = (int)InitiativeStatus.PendingApproval; } await _appDbContext.SaveChangesAsync(); var initiativeProfileView = _mapper.Map <InitiativeProfileViewModel>(initiativeProfile); initiativeProfileView.ReferenceNumber = parti.ReferenceNumber; return(new ActivityAndChallengesResponse(initiativeProfileView)); } catch (Exception e) { return(new ActivityAndChallengesResponse(e)); } }
public async Task <IActivityAndChallengesResponse> AddActivityAnswerAsync(ActivityAnswerViewModel model) { try { logger.Info($"{ GetType().Name} { ExtensionUtility.GetCurrentMethod() } input: {model.ToJsonString()} UserIPAddress: { _userIPAddress.GetUserIP().Result }"); InitiativeProfile initiativeProfile; var userInfo = await _appDbContext.UserInfos.FirstOrDefaultAsync(k => k.Id == model.ProfileId); if (userInfo == null) { return(new ActivityAndChallengesResponse(ClientMessageConstant.ProfileNotExist, HttpStatusCode.NotFound)); } var isExist = true; initiativeProfile = await _appDbContext.InitiativeProfiles.FirstOrDefaultAsync(k => k.ProfileId == model.ProfileId && k.InitiativeId == model.ActivityId); if (initiativeProfile == null) { initiativeProfile = new InitiativeProfile() { InitiativeId = model.ActivityId, ProfileId = model.ProfileId, // StatusItemId = (int)InitiativeStatus.PendingApproval, CreatedBy = userInfo.Email, Created = DateTime.Now, Modified = DateTime.Now, ModifiedBy = userInfo.Email }; await _appDbContext.InitiativeProfiles.AddAsync(initiativeProfile); await _appDbContext.SaveChangesAsync(); } var parti = await GetReferenceAsync(initiativeProfile.Id, model.ActivityId, userInfo); initiativeProfile = await _appDbContext.InitiativeProfiles.FirstOrDefaultAsync(k => k.ProfileId == model.ProfileId && k.InitiativeId == model.ActivityId); initiativeProfile.ParticipationReferenceID = parti.Id; await _appDbContext.SaveChangesAsync(); foreach (var answer in model.Answers) { var questionAnswer = await _appDbContext.QuestionAnswers.Include(a => a.Questionansweroptions).FirstOrDefaultAsync(k => k.ProfileId == model.ProfileId && k.InitiativeId == model.ActivityId && k.QuestionId == answer.QuestionId) ?? GetQuestionAnswer(answer, model, ref isExist); questionAnswer.CreatedBy = userInfo.Email; questionAnswer.ModifiedBy = userInfo.Email; switch (answer.QuestionType) { case ApplicationQuestionType.Text: questionAnswer.Text = answer.Text; break; case ApplicationQuestionType.MultiSelect: // questionAnswer.SelectedOptionId = answer.SelectedOptionId; break; case ApplicationQuestionType.TrueOrFalse: questionAnswer.YnquestionAnswer = answer.YesNoAnswer; break; case ApplicationQuestionType.Scale: questionAnswer.Scale = answer.Scale; break; case ApplicationQuestionType.File: questionAnswer.AnswerFileId = (await SaveFileAsync(answer.AnswerFile, model.ProfileId, (int)ApplicationQuestionType.File)).Id; break; case ApplicationQuestionType.VideoAttachment: questionAnswer.AnswerFileId = (await SaveFileAsync(answer.AnswerFile, model.ProfileId, (int)ApplicationQuestionType.VideoAttachment)).Id; break; } if (!isExist) { await _appDbContext.QuestionAnswers.AddAsync(questionAnswer); await _appDbContext.SaveChangesAsync(); switch (answer.QuestionType) { case ApplicationQuestionType.MultiSelect: _appDbContext.QuestionAnswerOptions.Add(new QuestionAnswerOption() { QuestionanswerID = questionAnswer.Id, optionID = answer.SelectedOptionId ?? 0 }); break; case ApplicationQuestionType.MultipleChoice: // questionAnswer.YnquestionAnswer = answer.YesNoAnswer; char[] spearator = { ',' }; var Ids = answer.MultipleChoice.Split(spearator); foreach (var item in Ids) { if (item != "") { _appDbContext.QuestionAnswerOptions.Add(new QuestionAnswerOption() { QuestionanswerID = questionAnswer.Id, optionID = Convert.ToInt32(item) }); } } break; } //await _appDbContext.SaveChangesAsync(); } else { switch (answer.QuestionType) { case ApplicationQuestionType.MultiSelect: if (questionAnswer.Questionansweroptions != null) { List <QuestionAnswerOption> _optionsdata = new List <QuestionAnswerOption>(); _optionsdata = questionAnswer.Questionansweroptions.ToList(); foreach (var item in _optionsdata) { _appDbContext.QuestionAnswerOptions.Remove(item); await _appDbContext.SaveChangesAsync(); } } _appDbContext.QuestionAnswerOptions.Add(new QuestionAnswerOption() { QuestionanswerID = questionAnswer.Id, optionID = answer.SelectedOptionId ?? 0 }); await _appDbContext.SaveChangesAsync(); break; case ApplicationQuestionType.MultipleChoice: // questionAnswer.YnquestionAnswer = answer.YesNoAnswer; if (questionAnswer.Questionansweroptions != null) { List <QuestionAnswerOption> _optionsdata = new List <QuestionAnswerOption>(); _optionsdata = questionAnswer.Questionansweroptions.ToList(); foreach (var item in _optionsdata) { _appDbContext.QuestionAnswerOptions.Remove(item); await _appDbContext.SaveChangesAsync(); } } char[] spearator = { ',' }; var Ids = answer.MultipleChoice.Split(spearator); foreach (var item in Ids) { if (item != "") { _appDbContext.QuestionAnswerOptions.Add(new QuestionAnswerOption() { QuestionanswerID = questionAnswer.Id, optionID = Convert.ToInt32(item) }); } } break; } } await _appDbContext.SaveChangesAsync(); } var initiativeProfileView = _mapper.Map <InitiativeProfileViewModel>(initiativeProfile); initiativeProfileView.ReferenceNumber = parti.ReferenceNumber; return(new ActivityAndChallengesResponse(initiativeProfileView)); } catch (Exception e) { logger.Error(e); return(new ActivityAndChallengesResponse(e)); } }