public void SavePalantiri(ObservedGuild guild) { bool newGuild = true; PalantirDbContext Database = new PalantirDbContext(); // If guild of new palantir has already an active palantir, close tether, replace palantir and reopen tether PalantirTethers.ForEach((t) => { if (t.PalantirEndpoint.GuildID == guild.GuildID) { string oldToken = t.PalantirEndpoint.ObserveToken; // update tether t.StopDataflow(); t.SetNewPalantirEndpoint(guild); t.EstablishDataflow(); newGuild = false; // Console.WriteLine("Change token from " + oldToken + " to " + guild.ObserveToken); // update db entry Database.Palantiri.Remove(Database.Palantiri.FirstOrDefault(p => p.Token == oldToken)); PalantirEntity entity = new PalantirEntity(); entity.Palantir = JsonConvert.SerializeObject(guild); entity.Token = guild.ObserveToken; Database.Palantiri.Add(entity); Database.SaveChanges(); } }); if (newGuild) { // add tether Tether tether = new Tether(guild); tether.EstablishDataflow(); PalantirTethers.Add(tether); // Add db entry PalantirEntity entity = new PalantirEntity(); entity.Token = guild.ObserveToken; entity.Palantir = JsonConvert.SerializeObject(guild); Database.Palantiri.Add(entity); Database.SaveChanges(); } Database.Dispose(); UpdateMemberGuilds(); }
public void RemovePalantiri(ObservedGuild guild) { // remove tether PalantirTethers.Remove(PalantirTethers.Find(t => t.PalantirEndpoint.ObserveToken == guild.ObserveToken)); // remove palantir from db PalantirDbContext context = new PalantirDbContext(); PalantirEntity e = context.Palantiri.FirstOrDefault(ptr => ptr.Token == guild.ObserveToken); context.Palantiri.Remove(e); try { context.SaveChanges(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } context.Dispose(); // restart string op = "sudo service palantir restart".Bash(); //Environment.Exit(0); //UpdateMemberGuilds(); }