public static void initAnon() { using (MemoDbConnection conn = new MemoDbConnection()) { using (MemoDbContext ctx = new MemoDbContext(conn.Connection, false)) { //var q = from u in ctx.Users where // u.Username.Equals (ANON_NAME) // select u; var q = from u in ctx.Users select u; System.Console.WriteLine (q == null); if (q.Count() < 1) { var anon = new User (); anon.Username = ANON_NAME; anon.LastOnline = DateTime.Now; anon.Password = "******"; conn.BeginTransaction (); try { ctx.Database.UseTransaction (conn.Transaction); ctx.Users.Add (anon); ctx.SaveChanges (); conn.CommitTransaction (); } catch { conn.RollbackTransaction (); } } } } }
public bool registerNick(string nick, string hash, Uri clientUri) { using (MemoDbConnection conn = new MemoDbConnection()) { conn.BeginTransaction(); try { using (MemoDbContext ctx = new MemoDbContext(conn.Connection, false)) { var qu = from u in ctx.Users where u.Username.Equals (nick) select u; if (qu.Count() > 0) { foreach(User u in qu){ if(u.LastOnline.Add(timeout).CompareTo(DateTime.UtcNow) > 0){ conn.CommitTransaction(); return false; } else { ctx.Users.Remove(u); } } } User nu = new User { LastOnline = DateTime.UtcNow, Username = nick, Password = hash }; ctx.Users.Add(nu); ctx.SaveChanges(); } conn.CommitTransaction(); return true; } catch { conn.RollbackTransaction(); return false; } } }