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

Commit

Permalink
update to typcal-go 0.11.4 + rename/refactor dbrepo to typdb
Browse files Browse the repository at this point in the history
  • Loading branch information
imantung committed Apr 22, 2021
1 parent 7e7da8c commit ec15f77
Show file tree
Hide file tree
Showing 22 changed files with 243 additions and 245 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ Setup the local environment
./typicalw setup # setup dependency e.g. mysql and postgres
```

Run application:
Generate code by annotation (if any change required)
```bash
./typicalw annotate
```

Build + Run application:
```bash
./typicalw run # run the application
```
Expand Down
8 changes: 1 addition & 7 deletions cmd/typical-rest-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"syscall"

"github.com/sirupsen/logrus"
"github.com/typical-go/typical-go/pkg/typapp"
Expand All @@ -16,12 +15,7 @@ import (
func main() {
fmt.Printf("%s %s\n", typgo.ProjectName, typgo.ProjectVersion)

application := typapp.Application{
StartFn: app.Start,
ShutdownFn: app.Shutdown,
ExitSigs: []syscall.Signal{syscall.SIGTERM, syscall.SIGINT},
}
if err := application.Run(); err != nil {
if err := typapp.StartService(app.Start, app.Shutdown); err != nil {
logrus.Fatal(err.Error())
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ require (
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sirupsen/logrus v1.6.0
github.com/stretchr/testify v1.7.0
github.com/typical-go/typical-go v0.11.3
github.com/typical-go/typical-go v0.11.4
github.com/urfave/cli/v2 v2.3.0
github.com/yuin/gopher-lua v0.0.0-20200816102855-ee81675732da // indirect
go.uber.org/dig v1.10.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/typical-go/typical-go v0.11.3 h1:7nM7HL6JYLPYRCzBcdlq1ze76zm5fn7UU8SdJ2Z5NO0=
github.com/typical-go/typical-go v0.11.3/go.mod h1:ELsfwAHa2z0ztxiZNu1HJmg3+fCTmf2xSyw/eiR6bUM=
github.com/typical-go/typical-go v0.11.4 h1:c/uH1RrKup8NKEWFCXs+8zK6e844M8VFj4DUaJos3gk=
github.com/typical-go/typical-go v0.11.4/go.mod h1:ELsfwAHa2z0ztxiZNu1HJmg3+fCTmf2xSyw/eiR6bUM=
github.com/urfave/cli/v2 v2.2.0 h1:JTTnM6wKzdA0Jqodd966MVj4vWbbquZykeX1sKbe2C4=
github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
Expand Down
13 changes: 6 additions & 7 deletions internal/app/service/book_svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import (
"strings"

"github.com/labstack/echo/v4"

"github.com/typical-go/typical-rest-server/internal/app/entity"
"github.com/typical-go/typical-rest-server/internal/generated/entity/app/repo"
"github.com/typical-go/typical-rest-server/internal/generated/dbrepo"
"github.com/typical-go/typical-rest-server/pkg/echokit"
"github.com/typical-go/typical-rest-server/pkg/sqkit"
"go.uber.org/dig"
Expand All @@ -31,7 +30,7 @@ type (
// BookSvcImpl is implementation of BookSvc
BookSvcImpl struct {
dig.In
Repo repo.BookRepo
Repo dbrepo.BookRepo
}
// FindBookReq find request
FindBookReq struct {
Expand Down Expand Up @@ -92,7 +91,7 @@ func (b *BookSvcImpl) FindOne(ctx context.Context, paramID string) (*entity.Book
}

func (b *BookSvcImpl) findOne(ctx context.Context, id int64) (*entity.Book, error) {
books, err := b.Repo.Find(ctx, sqkit.Eq{repo.BookTable.ID: id})
books, err := b.Repo.Find(ctx, sqkit.Eq{dbrepo.BookTable.ID: id})
if err != nil {
return nil, err
} else if len(books) < 1 {
Expand All @@ -104,7 +103,7 @@ func (b *BookSvcImpl) findOne(ctx context.Context, id int64) (*entity.Book, erro
// Delete book
func (b *BookSvcImpl) Delete(ctx context.Context, paramID string) error {
id, _ := strconv.ParseInt(paramID, 10, 64)
_, err := b.Repo.Delete(ctx, sqkit.Eq{repo.BookTable.ID: id})
_, err := b.Repo.Delete(ctx, sqkit.Eq{dbrepo.BookTable.ID: id})
return err
}

Expand All @@ -124,7 +123,7 @@ func (b *BookSvcImpl) Update(ctx context.Context, paramID string, book *entity.B
}

func (b *BookSvcImpl) update(ctx context.Context, id int64, book *entity.Book) error {
affectedRow, err := b.Repo.Update(ctx, book, sqkit.Eq{repo.BookTable.ID: id})
affectedRow, err := b.Repo.Update(ctx, book, sqkit.Eq{dbrepo.BookTable.ID: id})
if err != nil {
return err
}
Expand All @@ -147,7 +146,7 @@ func (b *BookSvcImpl) Patch(ctx context.Context, paramID string, book *entity.Bo
}

func (b *BookSvcImpl) patch(ctx context.Context, id int64, book *entity.Book) error {
affectedRow, err := b.Repo.Patch(ctx, book, sqkit.Eq{repo.BookTable.ID: id})
affectedRow, err := b.Repo.Patch(ctx, book, sqkit.Eq{dbrepo.BookTable.ID: id})
if err != nil {
return err
}
Expand Down
57 changes: 29 additions & 28 deletions internal/app/service/book_svc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ import (

"github.com/typical-go/typical-rest-server/internal/app/entity"
"github.com/typical-go/typical-rest-server/internal/app/service"
"github.com/typical-go/typical-rest-server/internal/generated/entity/app/repo"
"github.com/typical-go/typical-rest-server/internal/generated/entity/app/repo_mock"
"github.com/typical-go/typical-rest-server/internal/generated/dbrepo"
"github.com/typical-go/typical-rest-server/internal/generated/dbrepo_mock"

"github.com/typical-go/typical-rest-server/pkg/sqkit"
)

type bookSvcFn func(mockRepo *repo_mock.MockBookRepo)
type bookSvcFn func(mockRepo *dbrepo_mock.MockBookRepo)

func createBookSvc(t *testing.T, fn bookSvcFn) (service.BookSvc, *gomock.Controller) {
mock := gomock.NewController(t)
mockRepo := repo_mock.NewMockBookRepo(mock)
mockRepo := dbrepo_mock.NewMockBookRepo(mock)
if fn != nil {
fn(mockRepo)
}
Expand All @@ -46,7 +47,7 @@ func TestBookSvc_Create(t *testing.T) {
testName: "create error",
book: &entity.Book{Author: "some-author", Title: "some-title"},
expectedErr: "create-error",
bookSvcFn: func(mockRepo *repo_mock.MockBookRepo) {
bookSvcFn: func(mockRepo *dbrepo_mock.MockBookRepo) {
mockRepo.EXPECT().
Insert(gomock.Any(), &entity.Book{Author: "some-author", Title: "some-title"}).
Return(int64(-1), errors.New("create-error"))
Expand All @@ -56,7 +57,7 @@ func TestBookSvc_Create(t *testing.T) {
testName: "find error",
book: &entity.Book{Author: "some-author", Title: "some-title"},
expectedErr: "find-error",
bookSvcFn: func(mockRepo *repo_mock.MockBookRepo) {
bookSvcFn: func(mockRepo *dbrepo_mock.MockBookRepo) {
mockRepo.EXPECT().
Insert(gomock.Any(), &entity.Book{Author: "some-author", Title: "some-title"}).
Return(int64(1), nil)
Expand All @@ -71,7 +72,7 @@ func TestBookSvc_Create(t *testing.T) {
Title: "some-title",
},
expected: &entity.Book{Author: "some-author", Title: "some-title"},
bookSvcFn: func(mockRepo *repo_mock.MockBookRepo) {
bookSvcFn: func(mockRepo *dbrepo_mock.MockBookRepo) {
mockRepo.EXPECT().
Insert(gomock.Any(), &entity.Book{Author: "some-author", Title: "some-title"}).
Return(int64(1), nil)
Expand Down Expand Up @@ -108,7 +109,7 @@ func TestBookSvc_FindOne(t *testing.T) {
}{
{
paramID: "1",
bookSvcFn: func(mockRepo *repo_mock.MockBookRepo) {
bookSvcFn: func(mockRepo *dbrepo_mock.MockBookRepo) {
mockRepo.EXPECT().
Find(gomock.Any(), sqkit.Eq{"id": int64(1)}).
Return(nil, errors.New("some-error"))
Expand All @@ -117,7 +118,7 @@ func TestBookSvc_FindOne(t *testing.T) {
},
{
paramID: "1",
bookSvcFn: func(mockRepo *repo_mock.MockBookRepo) {
bookSvcFn: func(mockRepo *dbrepo_mock.MockBookRepo) {
mockRepo.EXPECT().
Find(gomock.Any(), sqkit.Eq{"id": int64(1)}).
Return([]*entity.Book{{ID: 1, Title: "some-title"}}, nil)
Expand All @@ -126,7 +127,7 @@ func TestBookSvc_FindOne(t *testing.T) {
},
{
paramID: "1",
bookSvcFn: func(mockRepo *repo_mock.MockBookRepo) {
bookSvcFn: func(mockRepo *dbrepo_mock.MockBookRepo) {
mockRepo.EXPECT().
Find(gomock.Any(), sqkit.Eq{"id": int64(1)}).
Return([]*entity.Book{}, nil)
Expand Down Expand Up @@ -159,7 +160,7 @@ func TestBookSvc_Find(t *testing.T) {
expectedErr string
}{
{
bookSvcFn: func(mockRepo *repo_mock.MockBookRepo) {
bookSvcFn: func(mockRepo *dbrepo_mock.MockBookRepo) {
mockRepo.EXPECT().Count(gomock.Any()).Return(int64(10), nil)
mockRepo.EXPECT().
Find(gomock.Any(), &sqkit.OffsetPagination{}).
Expand All @@ -179,7 +180,7 @@ func TestBookSvc_Find(t *testing.T) {
},
{
testName: "count error",
bookSvcFn: func(mockRepo *repo_mock.MockBookRepo) {
bookSvcFn: func(mockRepo *dbrepo_mock.MockBookRepo) {
mockRepo.EXPECT().
Count(gomock.Any()).
Return(int64(-1), errors.New("count-error"))
Expand All @@ -189,7 +190,7 @@ func TestBookSvc_Find(t *testing.T) {
},
{
testName: "find error",
bookSvcFn: func(mockRepo *repo_mock.MockBookRepo) {
bookSvcFn: func(mockRepo *dbrepo_mock.MockBookRepo) {
mockRepo.EXPECT().Count(gomock.Any()).Return(int64(10), nil)
mockRepo.EXPECT().
Find(gomock.Any(), &sqkit.OffsetPagination{Limit: 20, Offset: 10}, sqkit.Sorts{"title", "created_at"}).
Expand Down Expand Up @@ -225,26 +226,26 @@ func TestBookSvc_Delete(t *testing.T) {
{
paramID: "1",
expectedErr: `some-error`,
bookSvcFn: func(mockRepo *repo_mock.MockBookRepo) {
bookSvcFn: func(mockRepo *dbrepo_mock.MockBookRepo) {
mockRepo.EXPECT().
Delete(gomock.Any(), sqkit.Eq{repo.BookTable.ID: int64(1)}).
Delete(gomock.Any(), sqkit.Eq{dbrepo.BookTable.ID: int64(1)}).
Return(int64(0), errors.New("some-error"))
},
},
{
paramID: "1",
bookSvcFn: func(mockRepo *repo_mock.MockBookRepo) {
bookSvcFn: func(mockRepo *dbrepo_mock.MockBookRepo) {
mockRepo.EXPECT().
Delete(gomock.Any(), sqkit.Eq{repo.BookTable.ID: int64(1)}).
Delete(gomock.Any(), sqkit.Eq{dbrepo.BookTable.ID: int64(1)}).
Return(int64(1), nil)
},
},
{
testName: "success even if no affected row (idempotent)",
paramID: "1",
bookSvcFn: func(mockRepo *repo_mock.MockBookRepo) {
bookSvcFn: func(mockRepo *dbrepo_mock.MockBookRepo) {
mockRepo.EXPECT().
Delete(gomock.Any(), sqkit.Eq{repo.BookTable.ID: int64(1)}).
Delete(gomock.Any(), sqkit.Eq{dbrepo.BookTable.ID: int64(1)}).
Return(int64(0), nil)
},
},
Expand Down Expand Up @@ -284,7 +285,7 @@ func TestBookSvc_Update(t *testing.T) {
paramID: "1",
book: &entity.Book{Author: "some-author", Title: "some-title"},
expectedErr: "update error",
bookSvcFn: func(mockRepo *repo_mock.MockBookRepo) {
bookSvcFn: func(mockRepo *dbrepo_mock.MockBookRepo) {
mockRepo.EXPECT().
Find(gomock.Any(), sqkit.Eq{"id": int64(1)}).
Return([]*entity.Book{{ID: 1, Title: "some-title"}}, nil)
Expand All @@ -298,7 +299,7 @@ func TestBookSvc_Update(t *testing.T) {
paramID: "1",
book: &entity.Book{Author: "some-author", Title: "some-title"},
expectedErr: "no affected row",
bookSvcFn: func(mockRepo *repo_mock.MockBookRepo) {
bookSvcFn: func(mockRepo *dbrepo_mock.MockBookRepo) {
mockRepo.EXPECT().
Find(gomock.Any(), sqkit.Eq{"id": int64(1)}).
Return([]*entity.Book{{ID: 1, Title: "some-title"}}, nil)
Expand All @@ -312,7 +313,7 @@ func TestBookSvc_Update(t *testing.T) {
paramID: "1",
book: &entity.Book{Author: "some-author", Title: "some-title"},
expectedErr: "find-error",
bookSvcFn: func(mockRepo *repo_mock.MockBookRepo) {
bookSvcFn: func(mockRepo *dbrepo_mock.MockBookRepo) {
mockRepo.EXPECT().
Find(gomock.Any(), sqkit.Eq{"id": int64(1)}).
Return(nil, errors.New("find-error"))
Expand All @@ -323,7 +324,7 @@ func TestBookSvc_Update(t *testing.T) {
paramID: "1",
book: &entity.Book{Author: "some-author", Title: "some-title"},
expectedErr: "find-error",
bookSvcFn: func(mockRepo *repo_mock.MockBookRepo) {
bookSvcFn: func(mockRepo *dbrepo_mock.MockBookRepo) {
mockRepo.EXPECT().
Find(gomock.Any(), sqkit.Eq{"id": int64(1)}).
Return([]*entity.Book{{ID: 1, Title: "some-title"}}, nil)
Expand Down Expand Up @@ -366,7 +367,7 @@ func TestBookSvc_Patch(t *testing.T) {
paramID: "1",
book: &entity.Book{Author: "some-author", Title: "some-title"},
expectedErr: "patch-error",
bookSvcFn: func(mockRepo *repo_mock.MockBookRepo) {
bookSvcFn: func(mockRepo *dbrepo_mock.MockBookRepo) {
mockRepo.EXPECT().
Find(gomock.Any(), sqkit.Eq{"id": int64(1)}).
Return([]*entity.Book{{ID: 1, Title: "some-title"}}, nil)
Expand All @@ -380,7 +381,7 @@ func TestBookSvc_Patch(t *testing.T) {
paramID: "1",
book: &entity.Book{Author: "some-author", Title: "some-title"},
expectedErr: "no affected row",
bookSvcFn: func(mockRepo *repo_mock.MockBookRepo) {
bookSvcFn: func(mockRepo *dbrepo_mock.MockBookRepo) {
mockRepo.EXPECT().
Find(gomock.Any(), sqkit.Eq{"id": int64(1)}).
Return([]*entity.Book{{ID: 1, Title: "some-title"}}, nil)
Expand All @@ -394,7 +395,7 @@ func TestBookSvc_Patch(t *testing.T) {
paramID: "1",
book: &entity.Book{Author: "some-author", Title: "some-title"},
expectedErr: "find-error",
bookSvcFn: func(mockRepo *repo_mock.MockBookRepo) {
bookSvcFn: func(mockRepo *dbrepo_mock.MockBookRepo) {
mockRepo.EXPECT().
Find(gomock.Any(), sqkit.Eq{"id": int64(1)}).
Return(nil, errors.New("find-error"))
Expand All @@ -405,7 +406,7 @@ func TestBookSvc_Patch(t *testing.T) {
paramID: "1",
book: &entity.Book{Author: "some-author", Title: "some-title"},
expectedErr: "find-error",
bookSvcFn: func(mockRepo *repo_mock.MockBookRepo) {
bookSvcFn: func(mockRepo *dbrepo_mock.MockBookRepo) {
mockRepo.EXPECT().
Find(gomock.Any(), sqkit.Eq{"id": int64(1)}).
Return([]*entity.Book{{ID: 1, Title: "some-title"}}, nil)
Expand All @@ -421,7 +422,7 @@ func TestBookSvc_Patch(t *testing.T) {
paramID: "1",
book: &entity.Book{Author: "some-author", Title: "some-title"},
expected: &entity.Book{Author: "some-author", Title: "some-title"},
bookSvcFn: func(mockRepo *repo_mock.MockBookRepo) {
bookSvcFn: func(mockRepo *dbrepo_mock.MockBookRepo) {
mockRepo.EXPECT().
Find(gomock.Any(), sqkit.Eq{"id": int64(1)}).
Return([]*entity.Book{{ID: 1, Title: "some-title"}}, nil)
Expand Down
12 changes: 6 additions & 6 deletions internal/app/service/song_svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/labstack/echo/v4"

"github.com/typical-go/typical-rest-server/internal/app/entity"
"github.com/typical-go/typical-rest-server/internal/generated/entity/app/repo"
"github.com/typical-go/typical-rest-server/internal/generated/dbrepo"
"github.com/typical-go/typical-rest-server/pkg/echokit"
"github.com/typical-go/typical-rest-server/pkg/sqkit"
"go.uber.org/dig"
Expand All @@ -31,7 +31,7 @@ type (
// SongSvcImpl is implementation of SongSvc
SongSvcImpl struct {
dig.In
Repo repo.SongRepo
Repo dbrepo.SongRepo
}
// FindSongReq find request
FindSongReq struct {
Expand Down Expand Up @@ -93,7 +93,7 @@ func (b *SongSvcImpl) FindOne(ctx context.Context, paramID string) (*entity.Song
}

func (b *SongSvcImpl) findOne(ctx context.Context, id int64) (*entity.Song, error) {
books, err := b.Repo.Find(ctx, sqkit.Eq{repo.SongTable.ID: id})
books, err := b.Repo.Find(ctx, sqkit.Eq{dbrepo.SongTable.ID: id})
if err != nil {
return nil, err
} else if len(books) < 1 {
Expand All @@ -105,7 +105,7 @@ func (b *SongSvcImpl) findOne(ctx context.Context, id int64) (*entity.Song, erro
// Delete book
func (b *SongSvcImpl) Delete(ctx context.Context, paramID string) error {
id, _ := strconv.ParseInt(paramID, 10, 64)
_, err := b.Repo.Delete(ctx, sqkit.Eq{repo.SongTable.ID: id})
_, err := b.Repo.Delete(ctx, sqkit.Eq{dbrepo.SongTable.ID: id})
return err
}

Expand All @@ -126,7 +126,7 @@ func (b *SongSvcImpl) Update(ctx context.Context, paramID string, book *entity.S
}

func (b *SongSvcImpl) update(ctx context.Context, id int64, song *entity.Song) error {
affectedRow, err := b.Repo.Update(ctx, song, sqkit.Eq{repo.SongTable.ID: id})
affectedRow, err := b.Repo.Update(ctx, song, sqkit.Eq{dbrepo.SongTable.ID: id})
if err != nil {
return err
}
Expand All @@ -150,7 +150,7 @@ func (b *SongSvcImpl) Patch(ctx context.Context, paramID string, song *entity.So
}

func (b *SongSvcImpl) patch(ctx context.Context, id int64, song *entity.Song) error {
affectedRow, err := b.Repo.Patch(ctx, song, sqkit.Eq{repo.SongTable.ID: id})
affectedRow, err := b.Repo.Patch(ctx, song, sqkit.Eq{dbrepo.SongTable.ID: id})
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit ec15f77

Please sign in to comment.