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

Commit

Permalink
generate code for repository using entity annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
imantung committed Oct 19, 2020
1 parent e42478d commit 8431376
Show file tree
Hide file tree
Showing 14 changed files with 753 additions and 236 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package postgresdb
package repository

import (
"context"
Expand All @@ -7,6 +7,7 @@ import (
"time"

sq "github.com/Masterminds/squirrel"
"github.com/typical-go/typical-rest-server/internal/app/data_access/postgresdb"
"github.com/typical-go/typical-rest-server/pkg/dbkit"
"github.com/typical-go/typical-rest-server/pkg/dbtxn"
"github.com/typical-go/typical-rest-server/pkg/reflectkit"
Expand Down Expand Up @@ -34,13 +35,12 @@ var (

type (
// BookRepo to get book data from database
// @mock
BookRepo interface {
Find(context.Context, ...dbkit.SelectOption) ([]*Book, error)
Create(context.Context, *Book) (int64, error)
Find(context.Context, ...dbkit.SelectOption) ([]*postgresdb.Book, error)
Create(context.Context, *postgresdb.Book) (int64, error)
Delete(context.Context, dbkit.DeleteOption) (int64, error)
Update(context.Context, *Book, dbkit.UpdateOption) (int64, error)
Patch(context.Context, *Book, dbkit.UpdateOption) (int64, error)
Update(context.Context, *postgresdb.Book, dbkit.UpdateOption) (int64, error)
Patch(context.Context, *postgresdb.Book, dbkit.UpdateOption) (int64, error)
}
// BookRepoImpl is implementation book repository
BookRepoImpl struct {
Expand All @@ -50,13 +50,12 @@ type (
)

// NewBookRepo return new instance of BookRepo
// @ctor
func NewBookRepo(impl BookRepoImpl) BookRepo {
return &impl
}

// Find book
func (r *BookRepoImpl) Find(ctx context.Context, opts ...dbkit.SelectOption) (list []*Book, err error) {
func (r *BookRepoImpl) Find(ctx context.Context, opts ...dbkit.SelectOption) (list []*postgresdb.Book, err error) {
builder := sq.
Select(
BookTable.ID,
Expand All @@ -80,9 +79,9 @@ func (r *BookRepoImpl) Find(ctx context.Context, opts ...dbkit.SelectOption) (li
return
}

list = make([]*Book, 0)
list = make([]*postgresdb.Book, 0)
for rows.Next() {
book := new(Book)
book := new(postgresdb.Book)
if err = rows.Scan(
&book.ID,
&book.Title,
Expand All @@ -98,7 +97,7 @@ func (r *BookRepoImpl) Find(ctx context.Context, opts ...dbkit.SelectOption) (li
}

// Create book
func (r *BookRepoImpl) Create(ctx context.Context, book *Book) (int64, error) {
func (r *BookRepoImpl) Create(ctx context.Context, ent *postgresdb.Book) (int64, error) {
txn, err := dbtxn.Use(ctx, r.DB)
if err != nil {
return -1, err
Expand All @@ -113,8 +112,8 @@ func (r *BookRepoImpl) Create(ctx context.Context, book *Book) (int64, error) {
BookTable.UpdatedAt,
).
Values(
book.Title,
book.Author,
ent.Title,
ent.Author,
time.Now(),
time.Now(),
).
Expand All @@ -133,19 +132,22 @@ func (r *BookRepoImpl) Create(ctx context.Context, book *Book) (int64, error) {
return id, nil
}

// Delete book
func (r *BookRepoImpl) Delete(ctx context.Context, opt dbkit.DeleteOption) (int64, error) {
// Update book
func (r *BookRepoImpl) Update(ctx context.Context, ent *postgresdb.Book, opt dbkit.UpdateOption) (int64, error) {
txn, err := dbtxn.Use(ctx, r.DB)
if err != nil {
return -1, err
}

builder := sq.
Delete(BookTableName).
Update(BookTableName).
Set(BookTable.Title, ent.Title).
Set(BookTable.Author, ent.Author).
Set(BookTable.UpdatedAt, time.Now()).
PlaceholderFormat(sq.Dollar).
RunWith(txn.DB)

if builder, err = opt.CompileDelete(builder); err != nil {
if builder, err = opt.CompileUpdate(builder); err != nil {
txn.SetError(err)
return -1, err
}
Expand All @@ -155,25 +157,31 @@ func (r *BookRepoImpl) Delete(ctx context.Context, opt dbkit.DeleteOption) (int6
txn.SetError(err)
return -1, err
}

return res.RowsAffected()
affectedRow, err := res.RowsAffected()
txn.SetError(err)
return affectedRow, err
}

// Update book
func (r *BookRepoImpl) Update(ctx context.Context, book *Book, opt dbkit.UpdateOption) (int64, error) {
// Patch book to update field of book if available
func (r *BookRepoImpl) Patch(ctx context.Context, ent *postgresdb.Book, opt dbkit.UpdateOption) (int64, error) {
txn, err := dbtxn.Use(ctx, r.DB)
if err != nil {
return -1, err
}

builder := sq.
Update(BookTableName).
Set(BookTable.Title, book.Title).
Set(BookTable.Author, book.Author).
Set(BookTable.UpdatedAt, time.Now()).
PlaceholderFormat(sq.Dollar).
RunWith(txn.DB)

if !reflectkit.IsZero(ent.Title) {
builder = builder.Set(BookTable.Title, ent.Title)
}
if !reflectkit.IsZero(ent.Author) {
builder = builder.Set(BookTable.Author, ent.Author)
}
builder = builder.Set(BookTable.UpdatedAt, time.Now())

if builder, err = opt.CompileUpdate(builder); err != nil {
txn.SetError(err)
return -1, err
Expand All @@ -184,32 +192,25 @@ func (r *BookRepoImpl) Update(ctx context.Context, book *Book, opt dbkit.UpdateO
txn.SetError(err)
return -1, err
}

affectedRow, err := res.RowsAffected()
txn.SetError(err)
return affectedRow, err
}

// Patch book to update field of book if available
func (r *BookRepoImpl) Patch(ctx context.Context, book *Book, opt dbkit.UpdateOption) (int64, error) {
// Delete book
func (r *BookRepoImpl) Delete(ctx context.Context, opt dbkit.DeleteOption) (int64, error) {
txn, err := dbtxn.Use(ctx, r.DB)
if err != nil {
return -1, err
}

builder := sq.
Update(BookTableName).
Delete(BookTableName).
PlaceholderFormat(sq.Dollar).
RunWith(txn.DB)

if !reflectkit.IsZero(book.Title) {
builder = builder.Set(BookTable.Title, book.Title)
}
if !reflectkit.IsZero(book.Author) {
builder = builder.Set(BookTable.Author, book.Author)
}
builder = builder.Set(BookTable.UpdatedAt, time.Now())

if builder, err = opt.CompileUpdate(builder); err != nil {
if builder, err = opt.CompileDelete(builder); err != nil {
txn.SetError(err)
return -1, err
}
Expand All @@ -220,7 +221,5 @@ func (r *BookRepoImpl) Patch(ctx context.Context, book *Book, opt dbkit.UpdateOp
return -1, err
}

affectedRow, err := res.RowsAffected()
txn.SetError(err)
return affectedRow, err
return res.RowsAffected()
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package postgresdb_test
package repository_test

import (
"context"
Expand All @@ -11,19 +11,20 @@ import (
sqlmock "github.com/DATA-DOG/go-sqlmock"
sq "github.com/Masterminds/squirrel"
"github.com/stretchr/testify/require"
"github.com/typical-go/typical-rest-server/EXPERIMENT/repository"
"github.com/typical-go/typical-rest-server/internal/app/data_access/postgresdb"
"github.com/typical-go/typical-rest-server/pkg/dbkit"
"github.com/typical-go/typical-rest-server/pkg/dbtxn"
)

type bookRepoFn func(sqlmock.Sqlmock)

func createBookRepo(fn bookRepoFn) (postgresdb.BookRepo, *sql.DB) {
func createBookRepo(fn bookRepoFn) (repository.BookRepo, *sql.DB) {
db, mock, _ := sqlmock.New()
if fn != nil {
fn(mock)
}
return postgresdb.NewBookRepo(postgresdb.BookRepoImpl{DB: db}), db
return repository.NewBookRepo(repository.BookRepoImpl{DB: db}), db
}

func TestBookRepoImpl_Create(t *testing.T) {
Expand Down Expand Up @@ -100,7 +101,7 @@ func TestBookRepoImpl_Update(t *testing.T) {
{
TestName: "update error",
Book: &postgresdb.Book{Title: "new-title", Author: "new-author"},
Opt: dbkit.Equal(postgresdb.BookTable.ID, 888),
Opt: dbkit.Equal(repository.BookTable.ID, 888),
ExpectedErr: "dbtxn: begin-error",
Expected: -1,
BookRepoFn: func(mock sqlmock.Sqlmock) {
Expand All @@ -110,7 +111,7 @@ func TestBookRepoImpl_Update(t *testing.T) {
{
TestName: "update error",
Book: &postgresdb.Book{Title: "new-title", Author: "new-author"},
Opt: dbkit.Equal(postgresdb.BookTable.ID, 888),
Opt: dbkit.Equal(repository.BookTable.ID, 888),
BookRepoFn: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectExec(regexp.QuoteMeta(`UPDATE books SET title = $1, author = $2, updated_at = $3 WHERE id = $4`)).
Expand All @@ -137,7 +138,7 @@ func TestBookRepoImpl_Update(t *testing.T) {
{
TestName: "success",
Book: &postgresdb.Book{Title: "new-title", Author: "new-author"},
Opt: dbkit.Equal(postgresdb.BookTable.ID, 888),
Opt: dbkit.Equal(repository.BookTable.ID, 888),
BookRepoFn: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectExec(regexp.QuoteMeta(`UPDATE books SET title = $1, author = $2, updated_at = $3 WHERE id = $4`)).
Expand All @@ -149,7 +150,7 @@ func TestBookRepoImpl_Update(t *testing.T) {
{
TestName: "success empty author",
Book: &postgresdb.Book{Title: "new-title"},
Opt: dbkit.Equal(postgresdb.BookTable.ID, 888),
Opt: dbkit.Equal(repository.BookTable.ID, 888),
BookRepoFn: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectExec(regexp.QuoteMeta(`UPDATE books SET title = $1, author = $2, updated_at = $3 WHERE id = $4`)).
Expand All @@ -161,7 +162,7 @@ func TestBookRepoImpl_Update(t *testing.T) {
{
TestName: "success empty title",
Book: &postgresdb.Book{Author: "new-author"},
Opt: dbkit.Equal(postgresdb.BookTable.ID, 888),
Opt: dbkit.Equal(repository.BookTable.ID, 888),
BookRepoFn: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectExec(regexp.QuoteMeta(`UPDATE books SET title = $1, author = $2, updated_at = $3 WHERE id = $4`)).
Expand Down Expand Up @@ -202,7 +203,7 @@ func TestBookRepoImpl_Patch(t *testing.T) {
{
TestName: "begin error",
Book: &postgresdb.Book{Title: "new-title", Author: "new-author"},
Opt: dbkit.Equal(postgresdb.BookTable.ID, 888),
Opt: dbkit.Equal(repository.BookTable.ID, 888),
BookRepoFn: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin().WillReturnError(errors.New("begin-error"))
},
Expand All @@ -212,7 +213,7 @@ func TestBookRepoImpl_Patch(t *testing.T) {
{
TestName: "update error",
Book: &postgresdb.Book{Title: "new-title", Author: "new-author"},
Opt: dbkit.Equal(postgresdb.BookTable.ID, 888),
Opt: dbkit.Equal(repository.BookTable.ID, 888),
BookRepoFn: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectExec(regexp.QuoteMeta(`UPDATE books SET title = $1, author = $2, updated_at = $3 WHERE id = $4`)).
Expand Down Expand Up @@ -240,7 +241,7 @@ func TestBookRepoImpl_Patch(t *testing.T) {
{
TestName: "success",
Book: &postgresdb.Book{Title: "new-title", Author: "new-author"},
Opt: dbkit.Equal(postgresdb.BookTable.ID, 888),
Opt: dbkit.Equal(repository.BookTable.ID, 888),
BookRepoFn: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectExec(regexp.QuoteMeta(`UPDATE books SET title = $1, author = $2, updated_at = $3 WHERE id = $4`)).
Expand All @@ -252,7 +253,7 @@ func TestBookRepoImpl_Patch(t *testing.T) {
{
TestName: "success empty author",
Book: &postgresdb.Book{Title: "new-title"},
Opt: dbkit.Equal(postgresdb.BookTable.ID, 888),
Opt: dbkit.Equal(repository.BookTable.ID, 888),
BookRepoFn: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectExec(regexp.QuoteMeta(`UPDATE books SET title = $1, updated_at = $2 WHERE id = $3`)).
Expand All @@ -264,7 +265,7 @@ func TestBookRepoImpl_Patch(t *testing.T) {
{
TestName: "success empty title",
Book: &postgresdb.Book{Author: "new-author"},
Opt: dbkit.Equal(postgresdb.BookTable.ID, 888),
Opt: dbkit.Equal(repository.BookTable.ID, 888),
BookRepoFn: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectExec(regexp.QuoteMeta(`UPDATE books SET author = $1, updated_at = $2 WHERE id = $3`)).
Expand Down
14 changes: 7 additions & 7 deletions internal/app/data_access/postgresdb/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package postgresdb
import "time"

type (
// Book represented database model
// @entity (table:"books" dialect:"postgres")
// Book represented book model
// @entity (table:"books" dialect:"postgres" ctor_db:"pg")
Book struct {
ID int64 `json:"id"`
Title string `json:"title" validate:"required"`
Author string `json:"author" validate:"required"`
UpdatedAt time.Time `json:"update_at"`
CreatedAt time.Time `json:"created_at"`
ID int64 `column:"id" option:"pk" json:"id"`
Title string `column:"title" json:"title" validate:"required"`
Author string `column:"author" json:"author" validate:"required"`
UpdatedAt time.Time `column:"updated_at" option:"now" json:"update_at"`
CreatedAt time.Time `column:"created_at" option:"now,no_update" json:"created_at"`
}
)
13 changes: 7 additions & 6 deletions internal/app/domain/mylibrary/service/book_svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"

"github.com/typical-go/typical-rest-server/internal/app/data_access/postgresdb"
"github.com/typical-go/typical-rest-server/internal/generated/postgresdb_repo"
"github.com/typical-go/typical-rest-server/pkg/dbkit"
"github.com/typical-go/typical-rest-server/pkg/typrest"
"go.uber.org/dig"
Expand All @@ -26,7 +27,7 @@ type (
// BookSvcImpl is implementation of BookSvc
BookSvcImpl struct {
dig.In
postgresdb.BookRepo
postgresdb_repo.BookRepo
}
)

Expand Down Expand Up @@ -60,7 +61,7 @@ func (b *BookSvcImpl) FindOne(ctx context.Context, paramID string) (*postgresdb.
return nil, typrest.NewValidErr("paramID is missing")
}

books, err := b.BookRepo.Find(ctx, dbkit.Equal(postgresdb.BookTable.ID, id))
books, err := b.BookRepo.Find(ctx, dbkit.Equal(postgresdb_repo.BookTable.ID, id))
if err != nil {
return nil, err
} else if len(books) < 1 {
Expand All @@ -70,7 +71,7 @@ func (b *BookSvcImpl) FindOne(ctx context.Context, paramID string) (*postgresdb.
}

func (b *BookSvcImpl) findOne(ctx context.Context, id int64) (*postgresdb.Book, error) {
books, err := b.BookRepo.Find(ctx, dbkit.Equal(postgresdb.BookTable.ID, id))
books, err := b.BookRepo.Find(ctx, dbkit.Equal(postgresdb_repo.BookTable.ID, id))
if err != nil {
return nil, err
} else if len(books) < 1 {
Expand All @@ -85,7 +86,7 @@ func (b *BookSvcImpl) Delete(ctx context.Context, paramID string) error {
if id < 1 {
return typrest.NewValidErr("paramID is missing")
}
_, err := b.BookRepo.Delete(ctx, dbkit.Equal(postgresdb.BookTable.ID, id))
_, err := b.BookRepo.Delete(ctx, dbkit.Equal(postgresdb_repo.BookTable.ID, id))
return err
}

Expand All @@ -99,7 +100,7 @@ func (b *BookSvcImpl) Update(ctx context.Context, paramID string, book *postgres
if err != nil {
return nil, typrest.NewValidErr(err.Error())
}
affectedRow, err := b.BookRepo.Update(ctx, book, dbkit.Equal(postgresdb.BookTable.ID, id))
affectedRow, err := b.BookRepo.Update(ctx, book, dbkit.Equal(postgresdb_repo.BookTable.ID, id))
if err != nil {
return nil, err
}
Expand All @@ -115,7 +116,7 @@ func (b *BookSvcImpl) Patch(ctx context.Context, paramID string, book *postgresd
if id < 1 {
return nil, typrest.NewValidErr("paramID is missing")
}
affectedRow, err := b.BookRepo.Patch(ctx, book, dbkit.Equal(postgresdb.BookTable.ID, id))
affectedRow, err := b.BookRepo.Patch(ctx, book, dbkit.Equal(postgresdb_repo.BookTable.ID, id))
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 8431376

Please sign in to comment.