Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,55 @@ $user = new User("100", ["gender" => "M", "age" => "25"], "my_comment");
$rowsAffected = $client->insertUser($user);
```

Insert multiple users at once:

```php
$users = [
new User("100", ["gender" => "M", "age" => "25"], "user1"),
new User("101", ["gender" => "F", "age" => "30"], "user2"),
new User("102", ["gender" => "M", "age" => "28"], "user3"),
];
$rowsAffected = $client->insertUsers($users);
```

Insert items:

```php
$item = new Item(
"2000",
true,
["embedding" => [0.1, 0.2, 0.3]],
["Comedy", "Animation"],
"2022-11-20T13:55:27Z",
["embedding" => [0.1, 0.2, 0.3]],
"Minions (2015)"
);
$rowsAffected = $client->insertItem($item);
```

Insert multiple items at once:

```php
$items = [
new Item(
"2000",
false,
["Comedy", "Animation"],
"2022-11-20T13:55:27Z",
["comedy", "movie"],
"Minions (2015)"
),
new Item(
"2001",
false,
["Action", "Adventure"],
"2022-11-21T13:55:27Z",
["action", "movie"],
"The Matrix (1999)"
),
];
$rowsAffected = $client->insertItems($items);
```

Insert feedback:

```php
Expand Down
16 changes: 16 additions & 0 deletions src/Gorse.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ function insertUser(User $user): RowAffected
return RowAffected::fromJSON($this->request('POST', '/api/user/', $user));
}

/**
* @throws GuzzleException
*/
function insertUsers(array $users): RowAffected
{
return RowAffected::fromJSON($this->request('POST', '/api/users', $users));
}

/**
* @throws GuzzleException
*/
Expand All @@ -57,6 +65,14 @@ function insertItem(Item $item): RowAffected
return RowAffected::fromJSON($this->request('POST', '/api/item/', $item));
}

/**
* @throws GuzzleException
*/
function insertItems(array $items): RowAffected
{
return RowAffected::fromJSON($this->request('POST', '/api/items', $items));
}

/**
* @throws GuzzleException
*/
Expand Down
48 changes: 48 additions & 0 deletions test/GorseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ public function testUsers(): void
}
}

/**
* @throws GuzzleException
*/
public function testInsertMultipleUsers(): void
{
$client = new Gorse(self::ENDPOINT, self::API_KEY);

$users = array(
new User("1001", array("M", "engineer"), "user1"),
new User("1002", array("F", "designer"), "user2"),
new User("1003", array("M", "developer"), "user3"),
);

// Insert multiple users
$rowsAffected = $client->insertUsers($users);
$this->assertEquals(3, $rowsAffected->rowAffected);

// Verify users were inserted
foreach ($users as $user) {
$returnUser = $client->getUser($user->userId);
$this->assertEquals($user, $returnUser);
}
}

/**
* @throws GuzzleException
*/
Expand All @@ -59,6 +83,30 @@ public function testItems()
}
}

/**
* @throws GuzzleException
*/
public function testInsertMultipleItems(): void
{
$client = new Gorse(self::ENDPOINT, self::API_KEY);

$items = array(
new Item("2001", false, array("Comedy", "Animation"), "2022-11-20T13:55:27Z", array("comedy", "movie"), "Minions (2015)"),
new Item("2002", false, array("Action", "Adventure"), "2022-11-21T13:55:27Z", array("action", "movie"), "The Matrix (1999)"),
new Item("2003", false, array("Drama", "Thriller"), "2022-11-22T13:55:27Z", array("drama", "movie"), "The Silence of the Lambs (1991)"),
);

// Insert multiple items
$rowsAffected = $client->insertItems($items);
$this->assertEquals(3, $rowsAffected->rowAffected);

// Verify items were inserted
foreach ($items as $item) {
$returnItem = $client->getItem($item->itemId);
$this->assertEquals($item, $returnItem);
}
}

/**
* @throws GuzzleException
*/
Expand Down
Loading