mirror of
https://github.com/cirruslabs/orchard.git
synced 2026-10-19 16:36:52 +02:00
Refactored working with a storage (#20)
This commit is contained in:
+14
-10
@@ -7,9 +7,6 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type storeTxFunc func(cb func(txn *storepkg.Txn) error) error
|
||||
type apiTxFunc func(txn *storepkg.Txn) responder.Responder
|
||||
|
||||
func (controller *Controller) initAPI() *gin.Engine {
|
||||
gin.SetMode(gin.DebugMode)
|
||||
ginEngine := gin.Default()
|
||||
@@ -59,19 +56,26 @@ func (controller *Controller) initAPI() *gin.Engine {
|
||||
return ginEngine
|
||||
}
|
||||
|
||||
func (controller *Controller) storeView(cb apiTxFunc) responder.Responder {
|
||||
return mapTxFuncs(controller.store.View, cb)
|
||||
type storeTransactionFunc func(operation func(txn storepkg.Transaction) error) error
|
||||
|
||||
func (controller *Controller) storeView(view func(txn storepkg.Transaction) responder.Responder) responder.Responder {
|
||||
return adaptResponderToStoreOperation(controller.store.View, view)
|
||||
}
|
||||
|
||||
func (controller *Controller) storeUpdate(cb apiTxFunc) responder.Responder {
|
||||
return mapTxFuncs(controller.store.Update, cb)
|
||||
func (controller *Controller) storeUpdate(
|
||||
update func(txn storepkg.Transaction) responder.Responder,
|
||||
) responder.Responder {
|
||||
return adaptResponderToStoreOperation(controller.store.Update, update)
|
||||
}
|
||||
|
||||
func mapTxFuncs(txFunc storeTxFunc, cb apiTxFunc) responder.Responder {
|
||||
func adaptResponderToStoreOperation(
|
||||
storeOperation storeTransactionFunc,
|
||||
responderOperation func(txn storepkg.Transaction) responder.Responder,
|
||||
) responder.Responder {
|
||||
var result responder.Responder
|
||||
|
||||
if err := txFunc(func(txn *storepkg.Txn) error {
|
||||
result = cb(txn)
|
||||
if err := storeOperation(func(txn storepkg.Transaction) error {
|
||||
result = responderOperation(txn)
|
||||
|
||||
return nil
|
||||
}); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user