public bool Delete(int id, [FromBody]string deleteQuestionPassword) { if (deleteQuestionPassword != "fred.51") return false; try { using (var db = new FlashCardsDb()) { FlashCardEntry e = (from n in db.FlashCards where n.FlashCardEntryId == id select n).FirstOrDefault(); if (e == null) return false; db.FlashCards.Remove(e); db.SaveChanges(); //Now redo the numbering int number = 0; foreach (FlashCardEntry ent in db.FlashCards) { ent.FlashCardQuestionNumber = number++; } db.SaveChanges(); return true; } } catch (Exception ex) { return false; } }
public bool Post(string addQuestionPassword, [FromBody]string questionList) { if (addQuestionPassword != "fred.51") return false; //Parse incoming list into entries List<FlashCardEntry> entries = ParseData(questionList); //Add new entries into the datbase using (var db = new FlashCardsDb()) { try { foreach (FlashCardEntry e in entries) { bool found = false; foreach (FlashCardEntry dbEntry in db.FlashCards) { if (dbEntry.Question.ToUpper().Trim() == e.Question.ToUpper().Trim()) { //update answer if found dbEntry.Answer = e.Answer; found = true; break; } if (found) break; } //not found, so insert if (!found) db.FlashCards.Add(e); } db.SaveChanges(); //Now redo the numbering int number = 0; foreach (FlashCardEntry ent in db.FlashCards) { ent.FlashCardQuestionNumber = number++; } db.SaveChanges(); } catch (Exception ex) { return false; } } return true; }