Пример #1
0
        public async Task <MacroTaskViewModel> GetTask(ObjectId taskID, string name)
        {
            var collection = database.GetCollection <BsonDocument>("tasks");
            var filter     = Builders <BsonDocument> .Filter.Empty;

            if (taskID != ObjectId.Empty)
            {
                filter = Builders <BsonDocument> .Filter.Eq("_id", taskID);
            }
            else
            {
                filter = Builders <BsonDocument> .Filter.Eq("Name", name);
            }

            BsonDocument document = await collection.Find(filter).FirstOrDefaultAsync();

            if (document != null)
            {
                MacroTaskViewModel viewModel = new MacroTaskViewModel
                {
                    ID       = (ObjectId)document.GetValue("_id"),
                    Name     = document.GetValue("Name") != BsonNull.Value ? document.GetValue("Name").ToString() : null,
                    BaseUrl  = document.GetValue("BaseUrl") != BsonNull.Value ? document.GetValue("BaseUrl").ToString() : null,
                    Commands = await GetTaskCommands((ObjectId)document.GetValue("_id"))
                               //CommandCount = document.GetValue("Commands").AsBsonArray != BsonNull.Value ? document.GetValue("Commands").AsBsonArray.Count : 0
                };

                return(viewModel);
            }
            else
            {
                throw new Exception("Could not find the specified Task.");
            }
        }
Пример #2
0
        public async Task <List <MacroTaskViewModel> > GetAllTasks()
        {
            try
            {
                List <MacroTaskViewModel> viewModels = new List <MacroTaskViewModel>();
                var taskCollection = database.GetCollection <BsonDocument>("tasks");
                var filter         = new BsonDocument();

                using (var cursor = await taskCollection.FindAsync(filter))
                {
                    while (await cursor.MoveNextAsync())
                    {
                        var batch = cursor.Current;
                        foreach (var document in batch)
                        {
                            MacroTaskViewModel viewModel = new MacroTaskViewModel
                            {
                                ID       = (ObjectId)document.GetValue("_id"),
                                Name     = document.GetValue("Name") != BsonNull.Value ? document.GetValue("Name").ToString() : null,
                                BaseUrl  = document.GetValue("BaseUrl") != BsonNull.Value ? document.GetValue("BaseUrl").ToString() : null,
                                Commands = new List <CommandViewModel>(),
                                //CommandCount = document.GetValue("Commands").AsBsonArray != BsonNull.Value ? document.GetValue("Commands").AsBsonArray.Count : 0
                            };

                            if (viewModel.CommandCount != 0)
                            {
                                foreach (BsonValue element in document.GetValue("Commands").AsBsonArray)
                                {
                                    viewModel.Commands.Add(DbContextTools.PopulateCommandViewModel(element));
                                }
                            }

                            viewModels.Add(viewModel);
                        }
                    }
                }

                return(viewModels);
            }
            catch (Exception ex)
            {
                return(new List <MacroTaskViewModel>());
            }
        }