public static async Task HandleUserConnections(this EDCLoginUserContext db, HubCallerContext context, string userName, bool isTeacher = false) { if (db != null && context != null) { if (isTeacher) { var teacher = await db.Teachers.Include(p => p.HubConnections) .SingleOrDefaultAsync(p => p.TeacherName == userName); if (teacher != null) { teacher.HubConnections.Add(new Models.EDCHubConnection() { HubConnectionID = context.ConnectionId, Connected = true, LoginDate = DateTime.Now.Date.ToShortDateString(), LoginTime = DateTime.Now.ToString("hh:mm tt") }); await db.SaveChangesToDbAsync(); } } else { var student = await db.Students.Include(p => p.HubConnections) .SingleOrDefaultAsync(p => p.StudentName == userName); if (student != null) { student.HubConnections.Add(new Models.EDCHubConnection() { HubConnectionID = context.ConnectionId, Connected = true, LoginDate = DateTime.Now.Date.ToShortDateString(), LoginTime = DateTime.Now.ToString("hh:mm tt") }); await db.SaveChangesToDbAsync(); } } } }
public static async Task UpdateUserConnection(this EDCLoginUserContext db, string connectionId, bool connected) { if (db != null) { var connection = await db.HubConnections.FindAsync(connectionId); if (connection != null) { connection.Connected = connected; db.SetEntityModified <EDCHubConnection>(connection); await db.SaveChangesToDbAsync(); } } }