Exemple #1
0
 public Deck TrimValues()
 {
     DeckID   = DeckID.Trim();
     UserID   = UserID.Trim();
     DeckName = DeckName.Trim();
     return(this);
 }
        public async void CreateOrEditFlashcard()
        {
            if (!await _internetConnectionService.IsNetworkAvailable())
            {
                Messenger.Default.Send <MessageToMessageBoxControl>(new MessageToMessageBoxControl(true, false, true, true,
                                                                                                   StringResources.GetString("NoInternetConnection")));
                return;
            }
            else if (!_internetConnectionService.IsInternetAccess())
            {
                Messenger.Default.Send <MessageToMessageBoxControl>(new MessageToMessageBoxControl(true, false, StringResources.GetString("AccessDenied")));
                return;
            }
            else
            {
                if (!ValidateForm())
                {
                    return;
                }

                _flashcard.Answer   = Answer.TrimEnd();
                _flashcard.Question = Question.TrimEnd();

                string oldDeckName = _deck.Name;
                bool   oldIsPublic = _deck.IsPublic;
                _deck.Name     = DeckName.Trim();
                _deck.IsPublic = IsPublic;


                List <Tip> tips = new List <Tip>();
                foreach (TipViewModel tip in TipsCollection)
                {
                    tips.Add(new Tip(tip.ID, tip.Essence.Trim()));
                }

                try
                {
                    IsDataLoading = true;

                    switch (_mode)
                    {
                    case Mode.AddNewFlashcardToDeck:

                        if (oldDeckName != DeckName || oldIsPublic != IsPublic)
                        {
                            await _restService.UpdateDeck(_deck);
                        }

                        Flashcard createdFlashcard = await _restService.CreateFlashcard(_flashcard, _deck.ID);

                        foreach (var tip in tips)
                        {
                            await _restService.CreateTip(tip, _deck.ID, createdFlashcard.Id);
                        }

                        break;

                    case Mode.CreateFlashcardAndDeck:

                        Deck createdDeck = await _restService.CreateDeck(_deck);

                        Flashcard addedFlashcard = await _restService.CreateFlashcard(_flashcard, createdDeck.ID);

                        foreach (var tip in tips)
                        {
                            await _restService.CreateTip(tip, createdDeck.ID, addedFlashcard.Id);
                        }

                        break;

                    case Mode.EditFlashcard:

                        if (oldDeckName != DeckName || oldIsPublic != IsPublic)
                        {
                            await _restService.UpdateDeck(_deck);
                        }

                        string flashcardId = _flashcard.Id;
                        string deckId      = _flashcard.DeckID;

                        bool result = await _restService.UpdateFlashcard(_flashcard, deckId);

                        var oldTips = await _restService.GetTips(deckId, flashcardId);

                        if (oldTips != null)
                        {
                            foreach (var oldTip in oldTips)
                            {
                                await _restService.RemoveTip(deckId, flashcardId, oldTip.ID);
                            }
                        }

                        foreach (var tip in tips)
                        {
                            await _restService.CreateTip(tip, deckId, flashcardId);
                        }

                        break;
                    }
                }
                catch
                {
                    Messenger.Default.Send <MessageToMessageBoxControl>(new MessageToMessageBoxControl(true, false, StringResources.GetString("OperationFailed")));
                }
                finally
                {
                    IsDataLoading = false;
                }

                LeaveForm();
            }
        }