コード例 #1
0
        async Task ExecuteSaveEntryCommand()
        {
            SaveInProgress = true;
            try
            {
                var entry = _entryService.CreateEntry(Header, Body);
                if (entry == null)
                {
                    _eventAggregator.GetEvent <ErrorMessage>().Publish(new Tuple <ErrorType, string>(ErrorType.EntryCouldNotBeCreated, ""));
                    return;
                }

                try
                {
                    var entryReference = await _entryService.AddEntryToBookAsync(_bookReference, entry);

                    _eventAggregator.GetEvent <InAppInfoMessage>().Publish(new Tuple <InAppInfoMessageType, Dictionary <string, string> >(InAppInfoMessageType.EntrySaved, new Dictionary <string, string>()
                    {
                        { "EntryID", entry.Id }
                    }));

                    bool setAsThumbnail = true;
                    foreach (var attachment in Attachments)
                    {
                        _eventAggregator.GetEvent <InAppInfoMessage>().Publish(new Tuple <InAppInfoMessageType, Dictionary <string, string> >(InAppInfoMessageType.SavingAttachment, new Dictionary <string, string>()
                        {
                            { "AttachmentName", attachment.MediaData.Attachment.FileName }
                        }));

                        var attachmentAdded = await _entryService.AddAttachmentAsync(entryReference, entry, attachment.MediaData.Attachment, attachment.MediaData.Stream, attachment.MediaData.FilePath);

                        if (!attachmentAdded)
                        {
                            _eventAggregator.GetEvent <ErrorMessage>().Publish(new Tuple <ErrorType, string>(ErrorType.AttachmentCouldNotBeSaved, attachment.MediaData.Attachment.FileName));
                            return;
                        }
                        if (setAsThumbnail)
                        {
                            var thumbnailSet = await _entryService.SetThumbnailAsync(entryReference, entry, attachment.MediaData.Attachment, attachment.MediaData.Stream, attachment.MediaData.FilePath);

                            if (!thumbnailSet)
                            {
                                _eventAggregator.GetEvent <ErrorMessage>().Publish(new Tuple <ErrorType, string>(ErrorType.ThumbnailCouldNotBeSet, attachment.MediaData.Attachment.FileName));
                                return;
                            }
                        }
                        setAsThumbnail = false;

                        _eventAggregator.GetEvent <InAppInfoMessage>().Publish(new Tuple <InAppInfoMessageType, Dictionary <string, string> >(InAppInfoMessageType.AttachmentSaved, new Dictionary <string, string>()
                        {
                            { "AttachmentName", attachment.MediaData.Attachment.FileName }
                        }));
                    }

                    _eventAggregator.GetEvent <BookEntrySavedMessage>().Publish(new Tuple <EntryReference, Entry>(entryReference, entry));
                }
                catch (EntryCouldNotBeSavedException ex)
                {
                    _eventAggregator.GetEvent <ErrorMessage>().Publish(new Tuple <ErrorType, string>(ErrorType.EntryCouldNotBeSavedException, ex.Message));
                }
            }
            finally
            {
                SaveInProgress = false;
            }
        }