IEnumerable <Event> Sync_flashcards(SyncContextModel model) { var flashcards = _db.LoadFlashcards().ToArray(); var events0 = Sync_new_and_changed_flashcards(model, flashcards); var events1 = Sync_deleted_flashcards(model, flashcards); _db.StoreFlashcards(flashcards); return(events0.Concat(events1)); }
private Event[] Sync_new_and_changed_flashcards(SyncContextModel model, FlashcardRecord[] flashcards) { return(flashcards.SelectMany(Sync_flashcard).ToArray()); IEnumerable <Event> Sync_flashcard(FlashcardRecord fc) { if (string.IsNullOrWhiteSpace(fc.Id)) { // new card found; if it comes with a bin then keep it var eImported = new NewCardEncountered { Question = fc.Question, Answer = fc.Answer, Tags = fc.Tags }; var eMoved = new CardMovedTo { CardId = eImported.Id, BinIndex = 0 }; if (string.IsNullOrWhiteSpace(fc.BinIndex) is false) { eMoved.BinIndex = int.Parse(fc.BinIndex); } yield return(eImported); yield return(eMoved); fc.Id = eImported.Id; fc.BinIndex = eMoved.BinIndex.ToString(); } else { // changed card found var fcHash = FlashcardHash.Calculate(fc.Question, fc.Answer, fc.Tags); if (model.Flashcards[fc.Id].hash != fcHash) { yield return new CardWasChanged { CardId = fc.Id, Question = fc.Question, Answer = fc.Answer, Tags = fc.Tags } } ; } // always update the binIndex in the flashcard record to be written back to the file if (model.Flashcards.ContainsKey(fc.Id)) { fc.BinIndex = model.Flashcards[fc.Id].binIndex; } } }
private CardFoundMissing[] Sync_deleted_flashcards(SyncContextModel model, FlashcardRecord[] flashcards) => (from id in model.Flashcards.Keys