Example #1
0
        public void DeleteKey()
        {
            string projectId   = _fixture.ProjectId;
            string namespaceId = _fixture.NamespaceId;

            // Snippet: Delete(Key, *)
            DatastoreDb db         = DatastoreDb.Create(projectId, namespaceId);
            KeyFactory  keyFactory = db.CreateKeyFactory("message");
            Entity      message    = new Entity
            {
                Key      = keyFactory.CreateIncompleteKey(),
                ["text"] = "Hello",
            };
            Key key = db.Insert(message);

            Entity fetchedBeforeDeletion = db.Lookup(key);

            // Only the key is required to determine what to delete.
            // If you have an entity with the right key, you can use Delete(Entity, CallSettings)
            // or a similar overload for convenience.
            db.Delete(key);

            Entity fetchedAfterDeletion = db.Lookup(key);

            Console.WriteLine($"Entity exists before deletion? {fetchedBeforeDeletion != null}");
            Console.WriteLine($"Entity exists after deletion? {fetchedAfterDeletion != null}");
            // End snippet

            Assert.NotNull(fetchedBeforeDeletion);
            Assert.Null(fetchedAfterDeletion);
        }
Example #2
0
        public void DeleteEntity()
        {
            string projectId   = _fixture.ProjectId;
            string namespaceId = _fixture.NamespaceId;

            // Sample: DeleteEntity
            // Additional: Delete(Entity, *)
            DatastoreDb db         = DatastoreDb.Create(projectId, namespaceId);
            KeyFactory  keyFactory = db.CreateKeyFactory("message");
            Entity      message    = new Entity
            {
                Key      = keyFactory.CreateIncompleteKey(),
                ["text"] = "Hello",
            };

            db.Insert(message);

            Entity fetchedBeforeDeletion = db.Lookup(message.Key);

            // Only the key from the entity is used to determine what to delete.
            // If you already have the key but not the entity, use Delete(Key, CallSettings) or
            // a similar overload.
            db.Delete(message);

            Entity fetchedAfterDeletion = db.Lookup(message.Key);

            Console.WriteLine($"Entity exists before deletion? {fetchedBeforeDeletion != null}");
            Console.WriteLine($"Entity exists after deletion? {fetchedAfterDeletion != null}");
            // End snippet

            Assert.NotNull(fetchedBeforeDeletion);
            Assert.Null(fetchedAfterDeletion);
        }
        public IActionResult GetById(long id)
        {
            Entity      entity = _db.Lookup(_keyFactory.CreateKey(id));
            GroceryList item   = new GroceryList()
            {
                GroceryListId = entity.Key.Path[0].Id.ToString(), UserId = (string)entity["UserId"], GroceryName = (string)entity["GroceryName"], Quantity = (string)entity["Quantity"], Shareable = (bool)entity["Shareable"]
            };

            JsonResult jsonItem = new JsonResult(item);

            return(jsonItem);
        }
        public UserPhoto GetUserPhoto(string topic, string idUser, string idContent)
        {
            var userPhoto = Db.Lookup(DataModelExtensions.ToUserPhotoKey(topic, idUser, idContent)).ToUserPhoto();

            if (userPhoto == null)
            {
                userPhoto = new UserPhoto()
                {
                    IdUser = idUser, IdContent = idContent
                }
            }
            ;

            return(userPhoto);
        }
Example #5
0
        public void Update()
        {
            string projectId   = _fixture.ProjectId;
            string namespaceId = _fixture.NamespaceId;

            // Snippet: Update(Entity, CallSettings)
            DatastoreDb db         = DatastoreDb.Create(projectId, namespaceId);
            KeyFactory  keyFactory = db.CreateKeyFactory("book");
            Entity      book       = 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(book);

            // Correct the typo in memory
            book["title"] = "To Kill a Mockingbird";
            // Then update the entity in Datastore
            db.Update(book);
            // End snippet

            var fetched = db.Lookup(book.Key);

            Assert.Equal("To Kill a Mockingbird", (string)fetched["title"]);
        }
Example #6
0
        public Location Get(long id)
        {
            var entity = _datastore.Lookup(_keyFactory.CreateKey(id));

            var location = entity != null?Location.From(entity) : null;

            return(location);
        }
Example #7
0
        public T Get(int id)
        {
            DatastoreDb db = DatastoreDb.Create("fantasyfootball-204922", "default");

            KeyFactory keyFactory = db.CreateKeyFactory("fpl_player");
            var        result     = db.Lookup(keyFactory.CreateKey(id));

            return(JsonConvert.DeserializeObject <T>(result["text"].StringValue));
        }
Example #8
0
        public void LookupEntity()
        {
            string projectId   = _fixture.ProjectId;
            string namespaceId = _fixture.NamespaceId;
            Key    key         = _fixture.LearnDatastoreKey;

            // Sample: LookupEntity
            DatastoreDb db     = DatastoreDb.Create(projectId, namespaceId);
            Entity      entity = db.Lookup(key);
            // End sample
        }
Example #9
0
        public void DeleteEntity()
        {
            string projectId   = _fixture.ProjectId;
            string namespaceId = _fixture.NamespaceId;

            // Snippet: Delete(Entity[])
            DatastoreDb db         = DatastoreDb.Create(projectId, namespaceId);
            KeyFactory  keyFactory = db.CreateKeyFactory("message");
            Entity      message    = new Entity
            {
                Key      = keyFactory.CreateIncompleteKey(),
                ["text"] = "Hello",
            };

            db.Insert(message);

            Entity fetchedBeforeDeletion = db.Lookup(message.Key);

            using (DatastoreTransaction transaction = db.BeginTransaction())
            {
                // This adds the entity key to a collection of mutations in memory: nothing
                // is sent to the server. Only the key from the entity is used to determine what to delete.
                // If you already have the key but not the entity, use Delete(Key[]) or
                // a similar overload.
                transaction.Delete(message);
                // Without the Commit call, the transaction will automatically
                // be rolled back. The Commit call performs all the mutations
                // within the transaction.
                transaction.Commit();
            }

            Entity fetchedAfterDeletion = db.Lookup(message.Key);

            Console.WriteLine($"Entity exists before deletion? {fetchedBeforeDeletion != null}");
            Console.WriteLine($"Entity exists after deletion? {fetchedAfterDeletion != null}");
            // End snippet

            Assert.NotNull(fetchedBeforeDeletion);
            Assert.Null(fetchedAfterDeletion);
        }
        // [END list]

        public Place Read(long id)
        {
            //return _db.Lookup(id.ToKey())?.ToPlace();

            var   tmp   = _db.Lookup(id.ToKey());
            Place place = null;

            if (tmp != null)
            {
                place = tmp.ToPlace();
            }
            return(place);
        }
Example #11
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"]);
        }
Example #12
0
        public Employee Get(long id)
        {
            DatastoreDb db = GoogleCloudDatastore.CreateDb();

            Entity employeeEntity = db.Lookup(GoogleCloudDatastore.ToKey(id, "Employee"));

            if (employeeEntity != null)
            {
                return(new Employee(employeeEntity));
            }
            else
            {
                return(null);
            }
        }
Example #13
0
    public Model_Account FindAccountByEmail(string email)
    {
        Key k = db.CreateKeyFactory("Account").CreateKey(email);

        k.PartitionId = new PartitionId(projectId, namespaceId);

        Entity e = db.Lookup(k);

        if (e == null)
        {
            return(null);
        }
        else
        {
            return(Controller_Account.BuildController(e).model);
        }
    }
        private async Task <bool> UpdateGameState(GameState item)
        {
            using (var transaction = _db.BeginTransaction())
            {
                Entity entity = _db.Lookup(_keyFactory.CreateKey(Convert.ToInt64(item.PlatformKey)));
                if (entity != null)
                {
                    entity["CurrentLevel"] = item.CurrentLevel;
                    entity["Health"]       = item.Health;
                    entity["Inventory"]    = JsonConvert.SerializeObject(item.Inventory, Formatting.None);

                    transaction.Update(entity);
                }
                await transaction.CommitAsync();

                return(entity != null);
            }
        }
Example #15
0
        public ReleaseState Get(string releaseId)
        {
            var key    = ReleaseKeyFactory.CreateKey(releaseId);
            var entity = Database.Lookup(key);

            if (entity == null)
            {
                return(null);
            }

            return(new ReleaseState(releaseId,
                                    entity.Properties[nameof(ReleaseState.Artist)].StringValue,
                                    entity.Properties[nameof(ReleaseState.Title)].StringValue,
                                    entity.Properties[nameof(ReleaseState.Genre)].StringValue,
                                    Guid.Parse(entity.Properties[nameof(ReleaseState.OwnerId)].StringValue),
                                    entity.Properties[nameof(ReleaseState.Cover)].BlobValue.ToByteArray(),
                                    ((Entity[])entity.Properties[nameof(ReleaseState.TrackList)].ArrayValue).Select(e => ToMetadata(e)),
                                    ToSubscription(entity.Properties[nameof(ReleaseState.Subscription)].EntityValue),
                                    entity.Properties[nameof(ReleaseState.Timestamp)].TimestampValue.ToDateTime()
                                    ));
        }
Example #16
0
        public ActionResult <object> Register([FromBody] User user)
        {
            Environment.SetEnvironmentVariable(
                "GOOGLE_APPLICATION_CREDENTIALS",
                Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "fsndigital-cred.json"));

            DatastoreDb db = DatastoreDb.Create("fsndigital", "Users", DatastoreClient.Create());

            string kind = "user";

            KeyFactory keyFactory = db.CreateKeyFactory(kind);
            Key        key        = keyFactory.CreateKey(user.Email);

            var task = new Entity
            {
                Key          = key,
                ["TypeId"]   = user.TypeId,
                ["UserName"] = user.UserName,
                ["Email"]    = user.Email,
                ["Password"] = user.Password,
            };

            Entity check = db.Lookup(task.Key);

            if (check != null)
            {
                return(Ok(new { succeeded = false, message = "An account with that email already exists." }));
            }



            using (DatastoreTransaction transaction = db.BeginTransaction())
            {
                transaction.Upsert(task);
                transaction.Commit();
            }

            return(Ok(new { succeeded = true }));
        }
Example #17
0
        public void Lookup()
        {
            string projectId   = _fixture.ProjectId;
            string namespaceId = _fixture.NamespaceId;

            // Snippet: Lookup(*)
            DatastoreDb db         = DatastoreDb.Create(projectId, namespaceId);
            KeyFactory  keyFactory = db.CreateKeyFactory("book");
            Key         key1       = keyFactory.CreateKey("pride_and_prejudice");
            Key         key2       = keyFactory.CreateKey("not_present");

            IReadOnlyList <Entity> entities = db.Lookup(key1, key2);

            Console.WriteLine(entities[0]); // Pride and Prejudice entity
            Console.WriteLine(entities[1]); // Nothing (value is null reference)
            // End snippet

            Entity entity = entities[0];

            Assert.Equal("Jane Austen", (string)entity["author"]);
            Assert.Equal("Pride and Prejudice", (string)entity["title"]);
            Assert.Null(entities[1]);
        }
Example #18
0
        public void Update()
        {
            string projectId   = _fixture.ProjectId;
            string namespaceId = _fixture.NamespaceId;

            // Snippet: Update(Entity[])
            DatastoreDb db         = DatastoreDb.Create(projectId, namespaceId);
            KeyFactory  keyFactory = db.CreateKeyFactory("book");
            Entity      book       = 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(book);

            using (DatastoreTransaction transaction = db.BeginTransaction())
            {
                // Correct the typo in memory
                book["title"] = "To Kill a Mockingbird";
                // This adds the entity to a collection of mutations in memory: nothing
                // is sent to the server.
                transaction.Update(book);
                // Without the Commit call, the transaction will automatically
                // be rolled back. The Commit call performs all the mutations
                // within the transaction.
                transaction.Commit();
            }
            // End snippet

            var fetched = db.Lookup(book.Key);

            Assert.Equal("To Kill a Mockingbird", (string)fetched["title"]);
        }
Example #19
0
        public IActionResult Login([FromBody] Credentials credentials)
        {
            Environment.SetEnvironmentVariable(
                "GOOGLE_APPLICATION_CREDENTIALS",
                Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "fsndigital-cred.json"));

            DatastoreDb db = DatastoreDb.Create("fsndigital", "Users", DatastoreClient.Create());

            string kind = "user";

            KeyFactory keyFactory = db.CreateKeyFactory(kind);
            Key        key        = keyFactory.CreateKey(credentials.Email);

            var task = new Entity
            {
                Key = key,
            };

            Entity check = db.Lookup(task.Key);


            if (check == null)
            {
                return(Ok(new { succeeded = false, message = "No such user." }));
            }

            if (credentials.Password == (string)check["Password"])
            {
                return(Ok(new { succeeded = true, typeid = (int)check["TypeId"], username = (string)check["UserName"] }));
            }

            else
            {
                return(Ok(new { succeeded = false, message = "Incorrect Password" }));
            }
        }
 public Item Read(long id)
 {
     return(_db.Lookup(id.ToKey())?.ToItem());
 }
        // [END list]

        public Book Read(long id)
        {
            return(_db.Lookup <Book>(id.ToKey <Book>()));
        }
Example #22
0
        // [END list]

        public Book Read(long id)
        {
            return(_db.Lookup(id.ToKey())?.ToBook());
        }
Example #23
0
 private void AssertValidEntity(Entity original)
 {
     _db.Upsert(original);
     Assert.Equal(original, _db.Lookup(original.Key));
 }
Example #24
0
        // [START list]

        // [END list]

        public ImageMetaDeta Read(long id)
        {
            return(_db.Lookup(id.ToKey())?.ToImageMetaData());
        }
 public Movie Read(long id)
 {
     return(_db.Lookup(id.ToKey())?.Tomovie());
 }
 // [END list]
 public Player Read(long id)
 {
     return(_db.Lookup(id.ToKey())?.ToPlayer());
 }
Example #27
0
 public Owner FindOwner(string ownerid)
 {
     return(db.Lookup(ownerid.ToKey())?.ToOwner());
 }
Example #28
0
        // [END list]

        public T Read(long id)
        {
            var result = _db.Lookup(ToKey(id));

            return(result != null?ToObject(result) : null);
        }
Example #29
0
        // [END list]

        public Team Read(long id)
        {
            return(_db.Lookup(id.ToKey())?.ToTeam());
        }