Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
fix NoContextSupport error when insert/update data
Browse files Browse the repository at this point in the history
  • Loading branch information
imantung committed Mar 2, 2021
1 parent 93cce42 commit d092b13
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
16 changes: 8 additions & 8 deletions api/rest-client/mymusic.http
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@

### Find Books

http://localhost:8089/mymusic/songs?sort=artist
http://localhost:8089/songs?sort=artist

### Find Books (Pagination)

http://localhost:8089/mymusic/songs?offset=2&limit=2
http://localhost:8089/songs?offset=2&limit=2


### Find One Book (Invalid ID)

http://localhost:8089/mymusic/songs/0
http://localhost:8089/songs/0


### Insert Book

POST http://localhost:8089/mymusic/songs
POST http://localhost:8089/songs
content-type: application/json

{
Expand All @@ -25,12 +25,12 @@ content-type: application/json

### Find One Book

http://localhost:8089/mymusic/songs/6
http://localhost:8089/songs/6


### Update Book

PUT http://localhost:8089/mymusic/songs/6
PUT http://localhost:8089/songs/6
content-type: application/json

{
Expand All @@ -41,7 +41,7 @@ content-type: application/json

### Patch Book

PATCH http://localhost:8089/mymusic/songs/6
PATCH http://localhost:8089/songs/6
content-type: application/json

{
Expand All @@ -50,4 +50,4 @@ content-type: application/json

### Delete Book

DELETE http://localhost:8089/mymusic/songs/6
DELETE http://localhost:8089/songs/6
10 changes: 5 additions & 5 deletions pkg/dbtxn/dbtxn.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ type (
// UseHandler responsible to handle transaction
UseHandler struct {
*Context
sq.BaseRunner
sq.StdSqlCtx
}
// Tx is interface for *db.Tx
Tx interface {
sq.BaseRunner
sq.StdSqlCtx
Rollback() error
Commit() error
}
Expand All @@ -55,15 +55,15 @@ func Use(ctx context.Context, db *sql.DB) (*UseHandler, error) {

c := Find(ctx)
if c == nil { // NOTE: not transactional
return &UseHandler{BaseRunner: db}, nil
return &UseHandler{StdSqlCtx: db}, nil
}

tx, err := c.Begin(ctx, db)
if err != nil {
return nil, err
}

return &UseHandler{BaseRunner: tx, Context: c}, nil
return &UseHandler{StdSqlCtx: tx, Context: c}, nil
}

// Find transaction context
Expand All @@ -88,7 +88,7 @@ func Error(ctx context.Context) error {
//

// Begin transaction
func (c *Context) Begin(ctx context.Context, db *sql.DB) (sq.BaseRunner, error) {
func (c *Context) Begin(ctx context.Context, db *sql.DB) (sq.StdSqlCtx, error) {
tx, ok := c.TxMap[db]
if ok {
return tx, nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/dbtxn/dbtxn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestUse(t *testing.T) {
TestName: "non transactional",
DB: &sql.DB{},
Ctx: context.Background(),
Expected: &dbtxn.UseHandler{BaseRunner: &sql.DB{}},
Expected: &dbtxn.UseHandler{StdSqlCtx: &sql.DB{}},
},
{
TestName: "begin error",
Expand Down Expand Up @@ -91,7 +91,7 @@ func TestUse_success(t *testing.T) {

require.NoError(t, err)
require.Equal(t, map[*sql.DB]dbtxn.Tx{
db: handler.BaseRunner.(dbtxn.Tx),
db: handler.StdSqlCtx.(dbtxn.Tx),
}, handler.Context.TxMap)
}

Expand Down
3 changes: 1 addition & 2 deletions tools/typical-build/typical-build.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/typical-go/typical-rest-server/pkg/dbtool/pgtool"
"github.com/typical-go/typical-rest-server/pkg/typcfg"
"github.com/typical-go/typical-rest-server/pkg/typdocker"
"github.com/typical-go/typical-rest-server/pkg/typrepo"
)

var descriptor = typgo.Descriptor{
Expand All @@ -25,7 +24,7 @@ var descriptor = typgo.Descriptor{
Sources: []string{"internal"},
Annotators: []typast.Annotator{
&typapp.CtorAnnotation{},
&typrepo.EntityAnnotation{},
// &typrepo.EntityAnnotation{},
&typcfg.EnvconfigAnnotation{DotEnv: ".env", UsageDoc: "USAGE.md"},
},
},
Expand Down

0 comments on commit d092b13

Please sign in to comment.