Skip to content

Commit

Permalink
Merge pull request #36 from ybkuroki/develop
Browse files Browse the repository at this point in the history
Add a new API for getting one book entity
  • Loading branch information
ybkuroki authored Feb 6, 2021
2 parents 2b96542 + decf68e commit aaccdb2
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 4 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,12 @@ There are the following services in the book management.
|Service Name|HTTP Method|URL|Parameter|Summary|
|:---|:---:|:---|:---|:---|
|List Service|GET|``/api/book/list``|Page|Get a list of books.|
|Get Service|GET|``/api/book/get?id=[BOOK_ID]``|Book ID|Get a book data.|
|List Service|GET|``/api/book/list?page=[PAGE_NUMBER]&size=[PAGE_SIZE]``|Page|Get a list of books.|
|Regist Service|POST|``/api/book/new``|Book|Regist a book data.|
|Edit Service|POST|``/api/book/edit``|Book|Edit a book data.|
|Delete Service|POST|``/api/book/delete``|Book|Delete a book data.|
|Search Title Service|GET|``/api/book/search``|Keyword, Page|Search a title with the specified keyword.|
|Search Title Service|GET|``/api/book/search?query=[KEYWORD]&page=[PAGE_NUMBER]&size=[PAGE_SIZE]``|Keyword, Page|Search a title with the specified keyword.|
### Account Management
There are the following services in the Account management.
Expand Down
2 changes: 2 additions & 0 deletions controller/api_const.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const (
API = "/api"
// APIBook represents the group of book management API.
APIBook = API + "/book"
// APIBookGet represents the API to get one book.
APIBookGet = APIBook + "/get"
// APIBookList represents the API to get book's list.
APIBookList = APIBook + "/list"
// APIBookSearch represents the API to search book's list.
Expand Down
7 changes: 7 additions & 0 deletions controller/book.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ func NewBookController(context mycontext.Context) *BookController {
return &BookController{context: context, service: service.NewBookService(context)}
}

// GetBook returns one record matched book's id.
func (controller *BookController) GetBook(c echo.Context) error {
id, _ := strconv.Atoi(c.QueryParam("id"))

return c.JSON(http.StatusOK, controller.service.FindByID(uint(id)))
}

// GetBookList returns the list of all books.
func (controller *BookController) GetBookList(c echo.Context) error {
page, _ := strconv.Atoi(c.QueryParam("page"))
Expand Down
11 changes: 9 additions & 2 deletions model/book.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (b *Book) FindAllByPage(rep repository.Repository, page int, size int) (*Pa
var rec RecordBook
var rows *sql.Rows
var err error
if rows, err = rep.Raw(selectBook+" limit ? offset ? ", size, page*size).Rows(); err != nil {
if rows, err = rep.Raw(createSQL(selectBook, page, size), size, page*size).Rows(); err != nil {
return nil, err
}
for rows.Next() {
Expand All @@ -104,7 +104,7 @@ func (b *Book) FindByTitle(rep repository.Repository, title string, page int, si
var rec RecordBook
var rows *sql.Rows
var err error
if rows, err = rep.Raw(selectBook+" where title like ? limit ? offset ? ", "%"+title+"%", size, page*size).Rows(); err != nil {
if rows, err = rep.Raw(createSQL(selectBook+" where title like ? ", page, size), "%"+title+"%", size, page*size).Rows(); err != nil {
return nil, err
}
for rows.Next() {
Expand All @@ -119,6 +119,13 @@ func (b *Book) FindByTitle(rep repository.Repository, title string, page int, si
return p, nil
}

func createSQL(sql string, page int, size int) string {
if page > 0 && size > 0 {
sql += " limit ? offset ? "
}
return sql
}

func createPage(books *[]Book, page int, size int) *Page {
p := NewPage()
p.Page = page
Expand Down
1 change: 1 addition & 0 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func Init(e *echo.Echo, context mycontext.Context) {
account := controller.NewAccountController(context)
health := controller.NewHealthController(context)

e.GET(controller.APIBookGet, func(c echo.Context) error { return book.GetBook(c) })
e.GET(controller.APIBookList, func(c echo.Context) error { return book.GetBookList(c) })
e.GET(controller.APIBookSearch, func(c echo.Context) error { return book.GetBookSearch(c) })
e.POST(controller.APIBookRegist, func(c echo.Context) error { return book.PostBookRegist(c) })
Expand Down
12 changes: 12 additions & 0 deletions service/book.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ func NewBookService(context mycontext.Context) *BookService {
return &BookService{context: context}
}

// FindByID returns one record matched book's id.
func (b *BookService) FindByID(id uint) *model.Book {
rep := b.context.GetRepository()
book := model.Book{}
result, err := book.FindByID(rep, id)
if err != nil {
b.context.GetLogger().GetZapLogger().Errorf(err.Error())
return nil
}
return result
}

// FindAllBooks returns the list of all books.
func (b *BookService) FindAllBooks() *[]model.Book {
rep := b.context.GetRepository()
Expand Down

0 comments on commit aaccdb2

Please sign in to comment.