/// <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(); } }