Skip to content

Commit

Permalink
Merge pull request #25 from marugoshi/18-model
Browse files Browse the repository at this point in the history
[#18] refactoring model
  • Loading branch information
marugoshi committed Apr 2, 2019
2 parents 08db9b2 + 5b438ef commit c98bd9e
Show file tree
Hide file tree
Showing 15 changed files with 1,152 additions and 106 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ fmt:
test:
make reset_db DB=gobm_t
make migrate_up DB=gobm_t
# docker-compose exec gobm bash -c 'mysql -h mysql -uroot -ppassword gobm_t < testdata/masters.sql'
docker-compose exec gobm bash -c 'go test ./...'
6 changes: 4 additions & 2 deletions domain/entity/bookmark.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package entity

import (
"database/sql"
"github.com/marugoshi/gobm/infrastructure/storage/mysql"
)

type Bookmark struct {
Id int64
DirectoryId sql.NullInt64
DirectoryId mysql.NullInt64
Url string
Title string
CreatedAt mysql.NullDateTime
UpdatedAt mysql.NullDateTime
}
8 changes: 4 additions & 4 deletions domain/model/bookmark_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
)

type BookmarkModel interface {
All(ctx context.Context, page int, perPage int) (interface{}, error)
FindById(ctx context.Context, id int64) (interface{}, error)
Create(ctx context.Context, params *entity.Bookmark) (interface{}, error)
Update(ctx context.Context, params *entity.Bookmark) (interface{}, error)
All(ctx context.Context, page int, perPage int) ([]*entity.Bookmark, error)
FindById(ctx context.Context, id int64) (*entity.Bookmark, error)
Create(ctx context.Context, params *entity.Bookmark) (int64, error)
Update(ctx context.Context, params *entity.Bookmark) (int64, error)
Delete(ctx context.Context, id int64) error
}
27 changes: 23 additions & 4 deletions domain/service/bookmark_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import (
)

type BookmarkService interface {
Bookmarks(ctx context.Context) (interface{}, error)
Bookmarks(ctx context.Context, page int, perPage int) (interface{}, error)
Bookmark(ctx context.Context, id int64) (interface{}, error)
Create(ctx context.Context, bookmark *entity.Bookmark) (interface{}, error)
Update(ctx context.Context, bookmark *entity.Bookmark) (interface{}, error)
Delete(ctx context.Context, id int64) error
}

const (
PER_PAGE = 30
)

type bookmarkService struct {
model.BookmarkModel
}
Expand All @@ -22,12 +26,27 @@ func NewBookmarkService(m model.BookmarkModel) BookmarkService {
return &bookmarkService{m}
}

func (b *bookmarkService) Bookmarks(ctx context.Context) (interface{}, error) {
bookmarks, err := b.BookmarkModel.All(ctx, 1, 100)
func (b *bookmarkService) Bookmarks(ctx context.Context, page int, perPage int) (interface{}, error) {
if page == 0 {
page = 1
}

if perPage == 0 {
perPage = PER_PAGE
}

bookmarks, err := b.BookmarkModel.All(ctx, page, perPage)
if err != nil {
return nil, err
}
return bookmarks, nil

data := struct {
Records []*entity.Bookmark
}{
bookmarks,
}

return data, nil
}

func (b *bookmarkService) Bookmark(ctx context.Context, id int64) (interface{}, error) {
Expand Down
85 changes: 0 additions & 85 deletions infrastructure/storage/mysql/bookmark_model.go

This file was deleted.

161 changes: 161 additions & 0 deletions infrastructure/storage/mysql/model/bookmark_model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package model

import (
. "context"
"database/sql"
_ "github.com/go-sql-driver/mysql"
"github.com/marugoshi/gobm/domain/entity"
"github.com/marugoshi/gobm/domain/model"
. "github.com/marugoshi/gobm/infrastructure/storage/mysql"
"github.com/pkg/errors"
)

type BookmarkModel struct {
db *sql.DB
}

func NewBookmarkModel(db *sql.DB) model.BookmarkModel {
return &BookmarkModel{db: db}
}

func (b *BookmarkModel) All(ctx Context, page int, perPage int) ([]*entity.Bookmark, error) {
q := `
SELECT
id, directory_id, url, title, created_at, updated_at
FROM
bookmarks
LIMIT
?
OFFSET
?
`

rows, err := b.db.Query(q, perPage, (page-1)*perPage)
defer rows.Close()
if err != nil {
return nil, errors.Wrap(err, "can not execute query.")
}

records := make([]*entity.Bookmark, 0)
for rows.Next() {
bookmark := &entity.Bookmark{}
if err := rows.Scan(
&bookmark.Id,
&bookmark.DirectoryId,
&bookmark.Url,
&bookmark.Title,
&bookmark.CreatedAt,
&bookmark.UpdatedAt); err != nil {
return nil, errors.Wrap(err, "can not scan.")
}
records = append(records, bookmark)
}

return records, nil
}

func (b *BookmarkModel) FindById(ctx Context, id int64) (*entity.Bookmark, error) {
q := `
SELECT
id, directory_id, url, title, created_at, updated_at
FROM
bookmarks
WHERE
id = ?
`

bookmark := &entity.Bookmark{}
if err := b.db.QueryRow(q, id).Scan(
&bookmark.Id,
&bookmark.DirectoryId,
&bookmark.Url,
&bookmark.Title,
&bookmark.CreatedAt,
&bookmark.UpdatedAt); err != nil {
if err == sql.ErrNoRows {
return nil, nil
} else {
return nil, errors.Wrap(err, "can not execute query.")
}
}

return bookmark, nil
}

func (b *BookmarkModel) Create(ctx Context, params *entity.Bookmark) (int64, error) {
q := `
INSERT INTO
bookmarks
(
url,
directory_id,
title,
created_at,
updated_at
) VALUES(
?, ?, ?, NOW(), NOW()
)
`

result, err := b.db.Exec(q,
params.Url,
params.DirectoryId,
params.Title)
if err != nil {
return 0, errors.Wrap(err, "can not execute query.")
}

return result.LastInsertId()

}

func (b *BookmarkModel) Update(ctx Context, params *entity.Bookmark) (int64, error) {
q := `
UPDATE
bookmarks
SET
directory_id = ?,
url = ?,
title = ?,
updated_at = NOW()
WHERE
id = ?
`

if err := Transaction(b.db, func(tx *sql.Tx) error {
_, err := tx.Exec(q,
params.DirectoryId,
params.Url,
params.Title,
params.Id)
if err != nil {
return err
}
return err
}); err != nil {
return 0, errors.Wrap(err, "can not execute query.")
}

return params.Id, nil
}

func (b *BookmarkModel) Delete(ctx Context, id int64) error {
q := `
DELETE FROM
bookmarks
WHERE
id = ?
`

if err := Transaction(b.db, func(tx *sql.Tx) error {
_, err := tx.Exec(q, id)
if err != nil {
return err
}
return nil
}); err != nil {
return errors.Wrap(err, "can not execute query.")
}

return nil
}

0 comments on commit c98bd9e

Please sign in to comment.