github.com/s7techlab/cckit@v0.10.5/state/testdata/cc_books.go (about) 1 package testdata 2 3 import ( 4 "errors" 5 "fmt" 6 "github.com/s7techlab/cckit/extensions/debug" 7 "github.com/s7techlab/cckit/extensions/owner" 8 "github.com/s7techlab/cckit/router" 9 p "github.com/s7techlab/cckit/router/param" 10 "github.com/s7techlab/cckit/state" 11 "github.com/s7techlab/cckit/state/testdata/schema" 12 ) 13 14 const collection = "SampleCollection" 15 16 func NewBooksCC() *router.Chaincode { 17 r := router.New(`books`) 18 debug.AddHandlers(r, `debug`, owner.Only) 19 20 r.Init(owner.InvokeSetFromCreator). 21 Invoke(`bookList`, bookList). 22 Invoke(`bookListPaginated`, bookListPaginated, p.Struct(`in`, &schema.BookListRequest{})). 23 Invoke(`bookIds`, bookIds). 24 Invoke(`bookGet`, bookGet, p.String(`id`)). 25 Invoke(`bookInsert`, bookInsert, p.Struct(`book`, &schema.Book{})). 26 Invoke(`bookUpsert`, bookUpsert, p.Struct(`book`, &schema.Book{})). 27 Invoke(`bookUpsertWithCache`, bookUpsertWithCache, p.Struct(`book`, &schema.Book{})). 28 Invoke(`bookDelete`, bookDelete, p.String(`id`)). 29 Invoke(`privateBookList`, privateBookList). 30 Invoke(`privateBookGet`, privateBookGet, p.String(`id`)). 31 Invoke(`privateBookInsert`, privateBookInsert, p.Struct(`book`, &schema.PrivateBook{})). 32 Invoke(`privateBookUpsert`, privateBookUpsert, p.Struct(`book`, &schema.PrivateBook{})). 33 Invoke(`privateBookDelete`, privateBookDelete, p.String(`id`)) 34 35 return router.NewChaincode(r) 36 } 37 38 func bookList(c router.Context) (interface{}, error) { 39 return c.State().List(schema.BookEntity, &schema.Book{}) 40 } 41 42 func bookListPaginated(c router.Context) (interface{}, error) { 43 in, ok := c.Param(`in`).(schema.BookListRequest) 44 if !ok { 45 return nil, fmt.Errorf("unexpected argument type") 46 } 47 48 list, md, err := c.State().ListPaginated(schema.BookEntity, in.PageSize, in.Bookmark, &schema.Book{}) 49 if err != nil { 50 return nil, err 51 } 52 53 var books []*schema.Book 54 for _, item := range list.([]interface{}) { 55 var b = item.(schema.Book) 56 books = append(books, &b) 57 } 58 59 return schema.BookList{ 60 Items: books, 61 Next: md.Bookmark, 62 }, nil 63 } 64 65 func bookIds(c router.Context) (interface{}, error) { 66 return c.State().Keys(schema.BookEntity) 67 } 68 69 func bookInsert(c router.Context) (interface{}, error) { 70 book := c.Param(`book`) 71 return book, c.State().Insert(book) 72 } 73 74 func bookUpsert(c router.Context) (interface{}, error) { 75 book := c.Param(`book`).(schema.Book) 76 77 // udate data in state 78 if err := c.State().Put(book); err != nil { 79 return nil, err 80 } 81 82 //try to read new data in same transaction 83 upsertedBook, err := c.State().Get(schema.Book{Id: book.Id}, &schema.Book{}) 84 if err != nil { 85 return nil, err 86 } 87 88 // state read in same tx must return PREVIOOUS value 89 if book.Title == upsertedBook.(schema.Book).Title { 90 return nil, errors.New(`read after write in same tx must return previous value`) 91 } 92 93 return book, err 94 } 95 96 func bookUpsertWithCache(c router.Context) (interface{}, error) { 97 book := c.Param(`book`).(schema.Book) 98 99 stateCached := state.WithCache(c.State()) 100 101 // udate data in state 102 if err := stateCached.Put(book); err != nil { 103 return nil, err 104 } 105 106 //try to read new data in same transaction 107 upsertedBook, err := stateCached.Get(schema.Book{Id: book.Id}, &schema.Book{}) 108 if err != nil { 109 return nil, err 110 } 111 112 // state read in same tx with state caching must return NEW value 113 if book.Title != upsertedBook.(schema.Book).Title { 114 return nil, errors.New(`read after write with tx state caching must return same value`) 115 } 116 117 return book, err 118 } 119 120 func bookGet(c router.Context) (interface{}, error) { 121 return c.State().Get(schema.Book{Id: c.ParamString(`id`)}) 122 } 123 124 func bookDelete(c router.Context) (interface{}, error) { 125 return nil, c.State().Delete(schema.Book{Id: c.ParamString(`id`)}) 126 } 127 128 func privateBookList(c router.Context) (interface{}, error) { 129 return c.State().ListPrivate(collection, false, schema.PrivateBookEntity, &schema.PrivateBook{}) 130 } 131 132 func privateBookInsert(c router.Context) (interface{}, error) { 133 book := c.Param(`book`) 134 err := c.State().Insert(book, "{}") 135 if err != nil { 136 return book, err 137 } 138 return book, c.State().InsertPrivate(collection, book) 139 } 140 141 func privateBookUpsert(c router.Context) (interface{}, error) { 142 book := c.Param(`book`) 143 err := c.State().Put(book, "{}") 144 if err != nil { 145 return book, err 146 } 147 return book, c.State().PutPrivate(collection, book) 148 } 149 150 func privateBookGet(c router.Context) (interface{}, error) { 151 return c.State().GetPrivate(collection, schema.PrivateBook{Id: c.ParamString(`id`)}) 152 } 153 154 func privateBookDelete(c router.Context) (interface{}, error) { 155 c.State().Delete(schema.PrivateBook{Id: c.ParamString(`id`)}) 156 return nil, c.State().DeletePrivate(collection, schema.PrivateBook{Id: c.ParamString(`id`)}) 157 }