//! WARNING - DEBUG ONLY public static void ClearDatabase() { using (var db = new CopypastaContext()) { db.Copypastas.RemoveRange(db.Copypastas); db.SaveChanges(); } }
public static void DeleteCopypasta(Copypasta copypasta) { // Delete the given copypasta from the database using (var db = new CopypastaContext()) { var pasta = (from c in db.Copypastas where c.Id == copypasta.Id select c).SingleOrDefault(); db.Copypastas.Remove(pasta); db.SaveChanges(); } }
public static int GetUnnamedCopypastaCount() { // Get the amount of copypastas in the database that do not have a title int count = 0; using (var db = new CopypastaContext()) { count = (from c in db.Copypastas where string.IsNullOrEmpty(c.Title) select c).Count(); } return(count); }
public static int GetCopypastaCount() { // Get the total count of copypastas currently in the database int count; using (var db = new CopypastaContext()) { count = db.Copypastas.Count(); } return(count); }
public static List <Copypasta> WritePastasToList() { var lst = new List <Copypasta>(); using (var db = new CopypastaContext()) { var query = from c in db.Copypastas select c; foreach (var item in query) { lst.Add(item); } } return(lst); }
public static void AddToDatabase(Copypasta copypasta) { //First check if this pasta is already there (title doesn't matter because it isn't key anyway, it can appear multiple times) using (var db = new CopypastaContext()) { var query = from c in db.Copypastas where c.Content.Contains(copypasta.Content) select c; if (query.Any()) { //this copypasta has already been added return; } db.Copypastas.Add(copypasta); db.SaveChanges(); } }
// Updates a given Copypasta - we assume the Id stays the same and other parameters can be chagned public static void UpdateCopypasta(Copypasta newPasta) { using (var db = new CopypastaContext()) { var result = (from c in db.Copypastas where c.Id == newPasta.Id select c).SingleOrDefault(); if (result != null) { result.Title = newPasta.Title; result.Content = newPasta.Content; result.DateAdded = newPasta.DateAdded; result.IsFavourite = newPasta.IsFavourite; result.ChannelFrom = newPasta.ChannelFrom; db.SaveChanges(); } } }
public static void WritePastasToTextFile(string filePath) { using (var db = new CopypastaContext()) { if (db.Copypastas.Count() == 0) { File.AppendAllText(filePath, $"Brak copypast w bazie danych!"); } else { var query = from c in db.Copypastas select c; foreach (var item in query) { File.AppendAllText(filePath, $"{item.Id} {item.Title} {item.Content}" + Environment.NewLine); } } } }
public static DateTime?GetLastCopypastaDate() { // Returns the DateTime for the most recently added copypasta DateTime?date = null; using (var db = new CopypastaContext()) { var latest = (from c in db.Copypastas orderby c.DateAdded descending select c).FirstOrDefault(); if (latest.DateAdded == default) { return(null); } date = latest.DateAdded; } return(date); }
// Returns the amount of pastas added public static int UpdateDatabaseFromList(List <Copypasta> copypastas) { int amount = 0; using (var db = new CopypastaContext()) { for (int i = 0; i < copypastas.Count; i++) { //first check if the copypasta is not blacklisted if (Blacklist.BlacklistedMessages.Contains(copypastas[i].Content)) { continue; } //check for copypastas that have already are in the database var dbCopypastas = db.Copypastas.ToList(); bool found = false; foreach (var dbCopypasta in dbCopypastas) { if (dbCopypasta.Content == copypastas[i].Content) { found = true; break; } } if (!found) { db.Copypastas.Add(copypastas[i]); amount++; } } db.SaveChanges(); } return(amount); }