/// <summary> /// Gets the most recent shown survey, can be active/completed, or null if none was found /// </summary> /// <param name="tid">The user to whom the survey was shown</param> /// <returns></returns> public static async Task <Survey?> GetMostRecent(long tid) { using (var db = new MessagingDb()) { var ss = await(from survs in db.Surveys.Include(x => x.Questions) //.ThenInclude(y => y.FieldType) where tid == survs.TelegramUserId select survs).OrderByDescending(x => x.TelegramMessageId).OrderByDescending(x => x.Id).FirstOrDefaultAsync(); if (ss != null) { ss.Questions = ss.Questions.OrderBy(x => x.Id).ToList(); } return(ss); } }
/// <summary> /// Retrieves the most recent Active and not Completed survey or null if there is not /// </summary> /// <returns></returns> public static async Task <Survey?> GetCurrentSurvey(long telegramUserId) { using (var db = new MessagingDb()) { var ss = await(from survs in db.Surveys.Include(x => x.Questions) //.ThenInclude(y => y.FieldType) where survs.IsActive && survs.IsCompleted == false && survs.IsCancelled == false && survs.LastInteractionUtc.AddSeconds(SURVEY_EXPIRES_AFTER_SECONDS) > DateTime.UtcNow && telegramUserId == survs.TelegramUserId select survs).OrderByDescending(x => x.TelegramMessageId).OrderByDescending(x => x.Id).FirstOrDefaultAsync(); if (ss != null) { ss.Questions = ss.Questions.OrderBy(x => x.Id).ToList(); } return(ss); } }
/// <summary> /// Retrieves the Survey through the answer id /// </summary> /// <returns></returns> public static async Task <Survey?> GetSurveyByQuestionId(int questionId) { using (var db = new MessagingDb()) { var ss = await(from survs in db.Surveys.Include(x => x.Questions) //.ThenInclude(y => y.FieldType) join quests in db.Questions on survs.Id equals quests.SurveyId where quests.Id == questionId select survs).OrderByDescending(x => x.TelegramMessageId).OrderByDescending(x => x.Id).FirstOrDefaultAsync(); if (ss != null) { ss.Questions = ss.Questions.OrderBy(x => x.Id).ToList(); } return(ss); } }
/// <summary> /// Adds a survey to the DB /// </summary> /// <param name="telegramMessageId"></param> /// <param name="userId"></param> /// <returns></returns> public static async Task <Survey?> AddSurvey(int?telegramMessageId, long userId) { using (var db = new MessagingDb()) { var survey = new Survey() { CreatedUtc = DateTime.UtcNow, IsActive = true, IsCancelled = false, IsCompleted = false, TelegramMessageId = telegramMessageId, TelegramUserId = userId }; db.Surveys.Add(survey); await db.SaveChangesAsync(); return(survey); } }
public static async Task <FieldType?> CreateOrGet(string name) { using (var db = new MessagingDb()) { var ft = new FieldType() { Name = name }; try { db.FieldTypes.Add(ft); await db.SaveChangesAsync(); } catch (Exception) { ft = await(from fts in db.FieldTypes where fts.Name == name select fts).SingleOrDefaultAsync(); } return(ft); } }
/// <summary> /// Updates this survey to the DB /// </summary> /// <param name="updateQuestions">Whether the answers should be updated</param> /// <returns></returns> public async Task UpdateSurvey(bool updateQuestions) { using (var db = new MessagingDb()) { db.Entry(this).State = EntityState.Modified; LastInteractionUtc = DateTime.UtcNow; if (updateQuestions) { Questions.ForEach(q => { db.Attach(q); var entry = db.Entry(q); if (entry.State != EntityState.Added) { entry.State = EntityState.Modified; } }); } await db.SaveChangesAsync(); } }