示例#1
0
        public void CreateTopics(IEnumerable <string> topics)
        {
            string        kind       = "Topic";
            var           keyFactory = Db.CreateKeyFactory(kind);
            List <Entity> list       = new List <Entity>();

            foreach (var topic in topics)
            {
                list.Add(new Entity()
                {
                    Key          = keyFactory.CreateKey(topic),
                    ["Stars"]    = 0,
                    ["Likes"]    = 0,
                    ["Dislikes"] = 0,
                    ["Shares"]   = 0,
                });
            }

            Db.Upsert(list);

            var keys = list.Select(x => x.Key.Path[0].Name);

            Console.WriteLine($"Saved Topics: {string.Join(", ", keys)}");
        }
示例#2
0
        public void Store(ReleaseState state)
        {
            var entity = new Entity
            {
                Key = ReleaseKeyFactory.CreateKey(state.ReleaseId),
                [nameof(ReleaseState.Artist)]       = state.Artist,
                [nameof(ReleaseState.Title)]        = state.Title,
                [nameof(ReleaseState.Genre)]        = state.Genre,
                [nameof(ReleaseState.OwnerId)]      = state.OwnerId.ToString(),
                [nameof(ReleaseState.Cover)]        = state.Cover,
                [nameof(ReleaseState.TrackList)]    = state.TrackList.Select(t => ToEntity(t.Value)).ToArray(),
                [nameof(ReleaseState.Subscription)] = ToEntity(state.Subscription),
                [nameof(ReleaseState.Timestamp)]    = state.Timestamp,
            };

            Database.Upsert(entity);
        }
示例#3
0
        public void Upsert()
        {
            string projectId   = _fixture.ProjectId;
            string namespaceId = _fixture.NamespaceId;

            // Snippet: Upsert(Entity[])
            DatastoreDb db         = DatastoreDb.Create(projectId, namespaceId);
            KeyFactory  keyFactory = db.CreateKeyFactory("book");
            Entity      book1      = new Entity
            {
                Key                  = keyFactory.CreateIncompleteKey(),
                ["author"]           = "Harper Lee",
                ["title"]            = "Tequila Mockingbird",
                ["publication_date"] = new DateTime(1960, 7, 11, 0, 0, 0, DateTimeKind.Utc),
                ["genres"]           = new[] { "Southern drama", "Courtroom drama", "Bildungsroman" }
            };

            db.Insert(book1);

            // Correct the typo in memory
            book1["title"] = "To Kill a Mockingbird";

            Entity book2 = new Entity
            {
                Key                  = keyFactory.CreateIncompleteKey(),
                ["author"]           = "Charlotte Brontë",
                ["title"]            = "Jane Eyre",
                ["publication_date"] = new DateTime(1847, 10, 16, 0, 0, 0, DateTimeKind.Utc),
                ["genres"]           = new[] { "Gothic", "Romance", "Bildungsroman" }
            };

            // Update book1 and insert book2 into Datastore
            db.Upsert(book1, book2);
            // End snippet

            Assert.Equal("To Kill a Mockingbird", (string)db.Lookup(book1.Key)["title"]);
            Assert.Equal("Jane Eyre", (string)db.Lookup(book2.Key)["title"]);
        }
示例#4
0
 private void AssertValidEntity(Entity original)
 {
     _db.Upsert(original);
     Assert.Equal(original, _db.Lookup(original.Key));
 }
示例#5
0
        public void OnGet()
        {
            /* *****************************************************************
            *  // Your Google Cloud Platform project ID
            *  string projectId = "howzgcp004";
            *
            *  // Instantiates a client
            *  DatastoreDb db = DatastoreDb.Create(projectId);
            ***************************************************************** */

            DatastoreDb db = GoogleCloudDatastore.CreateDb();

            /* *****************************************************************
            *  // The kind for the new entity
            *  string kind = "Task";
            *  // The name/ID for the new entity
            *  string name = "sampletask1";
            *  KeyFactory keyFactory = db.CreateKeyFactory(kind);
            *  // The Cloud Datastore key for the new entity
            *  Key key = keyFactory.CreateKey(name);
            *
            *  var task = new Entity
            *  {
            *   Key = key,
            *   ["description"] = "Buy milk"
            *  };
            *  using (DatastoreTransaction transaction = db.BeginTransaction())
            *  {
            *   // Saves the task
            *   transaction.Upsert(task);
            *   transaction.Commit();
            *
            *   Console.WriteLine($"Saved {task.Key.Path[0].Name}: {(string)task["description"]}");
            *  }
            ***************************************************************** */



            Query query = new Query("Beer")
            {
                Filter = Filter.Equal("Brand", "TOOL")
            };

            foreach (Entity entity in db.RunQueryLazily(query))
            {
                string   beerBrand     = (string)entity["Brand"];
                string   beerName      = (string)entity["Name"];
                int      beerCost      = (int)entity["Cost"];
                int      beerPrice     = (int)entity["Price"];
                DateTime beerStockDate = (DateTime)entity["StockDate"];
                Console.WriteLine("Brand:{0} Name:{1} Cost:{2} Price:{3} StockDate:{4} ",
                                  beerBrand, beerName, beerCost, beerPrice, beerStockDate);

                entity.Key      = db.CreateKeyFactory("Beer").CreateIncompleteKey();
                entity["Brand"] = beerBrand + " again";
                var keys = db.Upsert(new[] { entity });
                Console.WriteLine("Beer Id: {0}", keys.First().Path.First().Id);
            }


            var task = new Entity
            {
                Key      = db.CreateKeyFactory("Task").CreateIncompleteKey(),
                ["memo"] = "買牛奶"
            };
            var taskkeys = db.Insert(new[] { task });

            Console.WriteLine("Task Id: {0}", taskkeys.First().Path.First().Id);

            var employee = new Entity
            {
                Key          = db.CreateKeyFactory("Employee").CreateIncompleteKey(),
                ["PersonId"] = "A123901930",
                ["Name"]     = "陳小寶"
            };
            var employeeKeys = db.Insert(new[] { employee });

            Console.WriteLine("Employee Id: {0}, Employee Kind: {1}", employeeKeys.First().Path.First().Id, employeeKeys.First().Path.First().Kind);

            Message = "Your Goole Cloud Platform Test page. (測試beer and task and employee)";
        }