Skip to content

Commit 251ea26

Browse files
authored
fix(server): return only books with correct publisher (#71)
the example server was returning books that had different publishers.
1 parent 8797f7d commit 251ea26

3 files changed

Lines changed: 47 additions & 3 deletions

File tree

example/service/service.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,14 @@ func (s BookstoreServer) GetBook(_ context.Context, r *bpb.GetBookRequest) (*bpb
204204
}
205205

206206
func (s BookstoreServer) ListBooks(_ context.Context, r *bpb.ListBooksRequest) (*bpb.ListBooksResponse, error) {
207+
if r.Parent == "" {
208+
return nil, status.Errorf(codes.InvalidArgument, "parent must be specified")
209+
}
210+
207211
rows, err := s.db.Query(`
208212
SELECT path, author, price, published, edition, isbn
209-
FROM books`)
213+
FROM books
214+
WHERE path LIKE ?`, r.Parent+"/%")
210215
if err != nil {
211216
return nil, status.Errorf(codes.Internal, "failed to list books: %v", err)
212217
}
@@ -216,7 +221,6 @@ func (s BookstoreServer) ListBooks(_ context.Context, r *bpb.ListBooksRequest) (
216221
for rows.Next() {
217222
book := &bpb.Book{}
218223

219-
// Deserialize the 'author' field from JSON when listing books
220224
var authorsSerialized string
221225
var isbnSerialized string
222226
if err := rows.Scan(&book.Path, &authorsSerialized, &book.Price, &book.Published, &book.Edition, &isbnSerialized); err != nil {
@@ -655,7 +659,7 @@ func StartServer(targetPort int) {
655659
CREATE TABLE IF NOT EXISTS books (
656660
path TEXT PRIMARY KEY,
657661
author TEXT,
658-
price REAL,
662+
price INTEGER,
659663
published BOOLEAN,
660664
edition INTEGER,
661665
isbn TEXT

example/service/service_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,41 @@ func TestMoveItem(t *testing.T) {
199199
t.Fatalf("expected new path to be 'stores/2/items/1', got %q", newPath)
200200
}
201201
}
202+
203+
func TestListBooksByPublisher(t *testing.T) {
204+
db := setupTestDB(t)
205+
defer db.Close()
206+
207+
s := NewBookstoreServer(db)
208+
209+
// Serialize authors and ISBNs for test data
210+
authorOneSerialized := `[{"name":"Author One"}]`
211+
authorTwoSerialized := `[{"name":"Author Two"}]`
212+
isbnOneSerialized := `["1111111111"]`
213+
isbnTwoSerialized := `["2222222222"]`
214+
215+
// Insert test books
216+
_, err := db.Exec(`
217+
INSERT INTO books (path, author, price, published, edition, isbn)
218+
VALUES (?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?)`,
219+
"publishers/1/books/1", authorOneSerialized, 10, true, 1, isbnOneSerialized,
220+
"publishers/2/books/2", authorTwoSerialized, 15, true, 1, isbnTwoSerialized,
221+
)
222+
if err != nil {
223+
t.Fatalf("failed to insert test books: %v", err)
224+
}
225+
226+
// Test filtering by publisher 1
227+
resp, err := s.ListBooks(context.Background(), &bpb.ListBooksRequest{Parent: "publishers/1"})
228+
if err != nil {
229+
t.Fatalf("ListBooks failed: %v", err)
230+
}
231+
232+
if len(resp.Results) != 1 {
233+
t.Fatalf("expected 1 book, got %d", len(resp.Results))
234+
}
235+
236+
if resp.Results[0].Path != "publishers/1/books/1" {
237+
t.Errorf("unexpected book path: %s", resp.Results[0].Path)
238+
}
239+
}

scripts/serve.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env bash
2+
go run ./...

0 commit comments

Comments
 (0)