public async Task <Result <CollectionDocumentChangeEventArgs> > CreateItem(IScreen context, CollectionReference collection) { if (collection is FileCollectionReference) { return(await AddFileToDatabase(context, collection.Database)); } var addDocumentOptions = new AddDocumentOptions(collection); var optionsResult = await ShowHostDialog(context).For(addDocumentOptions); if (optionsResult.Action is AddDocumentOptions.ACTION_CANCEL) { return(Result.Failure <CollectionDocumentChangeEventArgs>(Fails.Canceled)); } var newId = optionsResult.Model.NewId; var newDoc = new BsonDocument { ["_id"] = newId }; var documentReference = collection.AddItem(newDoc); var documentsCreated = new CollectionDocumentChangeEventArgs(ReferenceNodeChangeAction.Add, documentReference, collection) { PostAction = (optionsResult.Model.EditAfterCreate || optionsResult.Action is AddDocumentOptions.ACTION_OK_AND_EDIT) ? "edit" : null }; return(Result.Ok(documentsCreated)); }
public Task <Result <CollectionDocumentChangeEventArgs> > AddFileToDatabase(DatabaseReference database) { var dialog = new OpenFileDialog { Filter = "All files|*.*", Multiselect = false }; if (dialog.ShowDialog() != true) { return(Task.FromResult(Result.Fail <CollectionDocumentChangeEventArgs>("FILE_OPEN_CANCELED"))); } try { if (InputBoxWindow.ShowDialog("New file id:", "Enter new file id", Path.GetFileName(dialog.FileName), out string id) == true) { var file = database.AddFile(id, dialog.FileName); var documentsCreated = new CollectionDocumentChangeEventArgs(ReferenceNodeChangeAction.Add, new [] { file }, file.Collection); _eventAggregator.PublishOnUIThread(documentsCreated); return(Task.FromResult(Result.Ok(documentsCreated))); } } catch (Exception exc) { _applicationInteraction.ShowError(exc, "Failed to upload file:" + Environment.NewLine + exc.Message, "Database error"); } return(Task.FromResult(Result.Fail <CollectionDocumentChangeEventArgs>("FILE_OPEN_FAIL"))); }
public async Task <Result <CollectionDocumentChangeEventArgs> > AddFileToDatabase(DatabaseReference database) { var maybeFileName = await _applicationInteraction.ShowOpenFileDialog("Add file to database"); if (maybeFileName.HasNoValue) { return(Result.Fail <CollectionDocumentChangeEventArgs>("FILE_OPEN_CANCELED")); } try { var maybeId = await _applicationInteraction.ShowInputDialog("New file id:", "Enter new file id", Path.GetFileName(maybeFileName.Value)); if (maybeId.HasValue) { var file = database.AddFile(maybeId.Value, maybeFileName.Value); var documentsCreated = new CollectionDocumentChangeEventArgs(ReferenceNodeChangeAction.Add, new [] { file }, file.Collection); return(Result.Ok(documentsCreated)); } } catch (Exception exc) { _applicationInteraction.ShowError(exc, "Failed to upload file:" + Environment.NewLine + exc.Message, "Database error"); } return(Result.Fail <CollectionDocumentChangeEventArgs>("FILE_OPEN_FAIL")); }
public Task <Result <CollectionDocumentChangeEventArgs> > CreateItem(CollectionReference collection) { if (collection is FileCollectionReference) { return(AddFileToDatabase(collection.Database)); } var newDoc = new BsonDocument { ["_id"] = ObjectId.NewObjectId() }; var documentReference = collection.AddItem(newDoc); var documentsCreated = new CollectionDocumentChangeEventArgs(ReferenceNodeChangeAction.Add, documentReference, collection); return(Task.FromResult(Result.Ok(documentsCreated))); }
public Task <Result <CollectionDocumentChangeEventArgs> > ImportDataFromText(CollectionReference collection, string textData) { try { if (string.IsNullOrWhiteSpace(textData)) { return(Task.FromResult(Result.Ok(CollectionDocumentChangeEventArgs.Nome))); } var newValue = JsonSerializer.Deserialize(textData); var newDocs = new List <DocumentReference>(); if (newValue.IsArray) { foreach (var value in newValue.AsArray) { var doc = value.AsDocument; var documentReference = collection.AddItem(doc); newDocs.Add(documentReference); } } else { var doc = newValue.AsDocument; var documentReference = collection.AddItem(doc); newDocs.Add(documentReference); } var documentsUpdate = new CollectionDocumentChangeEventArgs(ReferenceNodeChangeAction.Add, newDocs, collection); return(Task.FromResult(Result.Ok(documentsUpdate))); } catch (Exception e) { var message = "Failed to import document from text content: " + e.Message; Logger.Warn(e, "Cannot process clipboard data."); _applicationInteraction.ShowError(e, message, "Import Error"); return(Task.FromResult(Result.Fail <CollectionDocumentChangeEventArgs>(message))); } }
public async Task <Result <CollectionDocumentChangeEventArgs> > AddFileToDatabase(IScreen context, DatabaseReference database, string filePath = null) { Maybe <string> maybeFileName = filePath; if (string.IsNullOrEmpty(filePath)) { maybeFileName = await _applicationInteraction.ShowOpenFileDialog("Add file to database"); if (maybeFileName.HasNoValue) { return(Result.Failure <CollectionDocumentChangeEventArgs>(Fails.Canceled)); } } var exportOptions = new AddFileOptions(database, Path.GetFileName(maybeFileName.Value)); var optionsResult = await ShowHostDialog(context).For(exportOptions); if (optionsResult.Action is "cancel") { return(Result.Failure <CollectionDocumentChangeEventArgs>(Fails.Canceled)); } try { var fileId = optionsResult.Model.NewFileId; if (!string.IsNullOrEmpty(fileId)) { var file = database.AddFile(fileId, maybeFileName.Value); var documentsCreated = new CollectionDocumentChangeEventArgs(ReferenceNodeChangeAction.Add, new [] { file }, file.Collection); return(Result.Ok(documentsCreated)); } } catch (Exception exc) { _applicationInteraction.ShowError(exc, "Failed to upload file:" + Environment.NewLine + exc.Message, "Database error"); } return(Result.Failure <CollectionDocumentChangeEventArgs>("FILE_OPEN_FAIL")); }