|
| 1 | +package examples |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/blaberg/aep-go/pagination" |
| 8 | + bookv1 "github.com/blaberg/aep-go/proto/gen/example/books/v1" |
| 9 | + "github.com/blaberg/aep-go/resourceid" |
| 10 | + "github.com/blaberg/aep-go/resourcepath" |
| 11 | + "github.com/blaberg/aep-go/validate" |
| 12 | + "google.golang.org/grpc/codes" |
| 13 | + "google.golang.org/grpc/status" |
| 14 | + "google.golang.org/protobuf/types/known/timestamppb" |
| 15 | +) |
| 16 | + |
| 17 | +// Service implements the BookService |
| 18 | +type Service struct { |
| 19 | + paginator *pagination.Paginator |
| 20 | + storage *Storage |
| 21 | +} |
| 22 | + |
| 23 | +// CreateBook implements the CreateBook RPC |
| 24 | +func (s *Service) CreateBook(ctx context.Context, req *bookv1.CreateBookRequest) (*bookv1.Book, error) { |
| 25 | + // Generate book ID if not provided |
| 26 | + bookID := req.Id |
| 27 | + if bookID == "" { |
| 28 | + bookID = resourceid.New() |
| 29 | + } |
| 30 | + if err := validate.ResourceID(bookID); err != nil { |
| 31 | + return nil, status.Errorf(codes.InvalidArgument, "invalid book ID: %v", err) |
| 32 | + } |
| 33 | + |
| 34 | + // Create the book's resource path |
| 35 | + path := bookv1.NewBookPath(bookID) |
| 36 | + |
| 37 | + // Create the book |
| 38 | + now := timestamppb.New(time.Now()) |
| 39 | + book := &bookv1.Book{ |
| 40 | + Path: path.String(), |
| 41 | + DisplayName: req.Book.DisplayName, |
| 42 | + CreateTime: now, |
| 43 | + UpdateTime: now, |
| 44 | + } |
| 45 | + |
| 46 | + // Store the book |
| 47 | + s.storage.Create(book) |
| 48 | + |
| 49 | + return book, nil |
| 50 | +} |
| 51 | + |
| 52 | +// GetBook implements the GetBook RPC |
| 53 | +func (s *Service) GetBook(ctx context.Context, req *bookv1.GetBookRequest) (*bookv1.Book, error) { |
| 54 | + // Validate path format |
| 55 | + path, err := bookv1.ParseBookResourcePath(req.GetPath()) |
| 56 | + if err != nil { |
| 57 | + return nil, status.Errorf(codes.InvalidArgument, "invalid resource path format: %v", err) |
| 58 | + } |
| 59 | + |
| 60 | + // Get the book |
| 61 | + book, ok := s.storage.Get(path.GetBook()) |
| 62 | + if !ok { |
| 63 | + return nil, status.Errorf(codes.NotFound, "book not found: %s", req.Path) |
| 64 | + } |
| 65 | + |
| 66 | + return book, nil |
| 67 | +} |
| 68 | + |
| 69 | +// ListBooks implements the ListBooks RPC |
| 70 | +func (s *Service) ListBooks(ctx context.Context, req *bookv1.ListBooksRequest) (*bookv1.ListBooksResponse, error) { |
| 71 | + // Validate parent format |
| 72 | + _, err := resourcepath.ParseString(req.Parent, "publishers/{publisher}") |
| 73 | + if err != nil { |
| 74 | + return nil, status.Errorf(codes.InvalidArgument, "invalid parent format: %v", err) |
| 75 | + } |
| 76 | + |
| 77 | + // Parse the page token |
| 78 | + token, err := s.paginator.ParsePageToken(req) |
| 79 | + if err != nil { |
| 80 | + return nil, status.Errorf(codes.InvalidArgument, "invalid page token: %v", err) |
| 81 | + } |
| 82 | + |
| 83 | + // Get books with pagination |
| 84 | + books, hasMore := s.storage.List(req.Parent, token.Offset, req.MaxPageSize) |
| 85 | + if books == nil { |
| 86 | + return &bookv1.ListBooksResponse{ |
| 87 | + Results: []*bookv1.Book{}, |
| 88 | + NextPageToken: "", |
| 89 | + }, nil |
| 90 | + } |
| 91 | + |
| 92 | + // Generate the next page token |
| 93 | + nextToken := token.Next(hasMore, req.MaxPageSize) |
| 94 | + nextPageToken := "" |
| 95 | + if nextToken != nil { |
| 96 | + nextPageToken = nextToken.String() |
| 97 | + } |
| 98 | + |
| 99 | + return &bookv1.ListBooksResponse{ |
| 100 | + Results: books, |
| 101 | + NextPageToken: nextPageToken, |
| 102 | + }, nil |
| 103 | +} |
0 commit comments