Skip to content
Merged
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
10 changes: 7 additions & 3 deletions example/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,14 @@ func (s BookstoreServer) GetBook(_ context.Context, r *bpb.GetBookRequest) (*bpb
}

func (s BookstoreServer) ListBooks(_ context.Context, r *bpb.ListBooksRequest) (*bpb.ListBooksResponse, error) {
if r.Parent == "" {
return nil, status.Errorf(codes.InvalidArgument, "parent must be specified")
}

rows, err := s.db.Query(`
SELECT path, author, price, published, edition, isbn
FROM books`)
FROM books
WHERE path LIKE ?`, r.Parent+"/%")
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to list books: %v", err)
}
Expand All @@ -216,7 +221,6 @@ func (s BookstoreServer) ListBooks(_ context.Context, r *bpb.ListBooksRequest) (
for rows.Next() {
book := &bpb.Book{}

// Deserialize the 'author' field from JSON when listing books
var authorsSerialized string
var isbnSerialized string
if err := rows.Scan(&book.Path, &authorsSerialized, &book.Price, &book.Published, &book.Edition, &isbnSerialized); err != nil {
Expand Down Expand Up @@ -655,7 +659,7 @@ func StartServer(targetPort int) {
CREATE TABLE IF NOT EXISTS books (
path TEXT PRIMARY KEY,
author TEXT,
price REAL,
price INTEGER,
published BOOLEAN,
edition INTEGER,
isbn TEXT
Expand Down
38 changes: 38 additions & 0 deletions example/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,41 @@ func TestMoveItem(t *testing.T) {
t.Fatalf("expected new path to be 'stores/2/items/1', got %q", newPath)
}
}

func TestListBooksByPublisher(t *testing.T) {
db := setupTestDB(t)
defer db.Close()

s := NewBookstoreServer(db)

// Serialize authors and ISBNs for test data
authorOneSerialized := `[{"name":"Author One"}]`
authorTwoSerialized := `[{"name":"Author Two"}]`
isbnOneSerialized := `["1111111111"]`
isbnTwoSerialized := `["2222222222"]`

// Insert test books
_, err := db.Exec(`
INSERT INTO books (path, author, price, published, edition, isbn)
VALUES (?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?)`,
"publishers/1/books/1", authorOneSerialized, 10, true, 1, isbnOneSerialized,
"publishers/2/books/2", authorTwoSerialized, 15, true, 1, isbnTwoSerialized,
)
if err != nil {
t.Fatalf("failed to insert test books: %v", err)
}

// Test filtering by publisher 1
resp, err := s.ListBooks(context.Background(), &bpb.ListBooksRequest{Parent: "publishers/1"})
if err != nil {
t.Fatalf("ListBooks failed: %v", err)
}

if len(resp.Results) != 1 {
t.Fatalf("expected 1 book, got %d", len(resp.Results))
}

if resp.Results[0].Path != "publishers/1/books/1" {
t.Errorf("unexpected book path: %s", resp.Results[0].Path)
}
}
2 changes: 2 additions & 0 deletions scripts/serve.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
go run ./...