diff --git a/controller/book.go b/controller/book.go index 953c950d..3e53ca58 100644 --- a/controller/book.go +++ b/controller/book.go @@ -3,16 +3,13 @@ package controller import ( "net/http" - "github.com/jinzhu/gorm" "github.com/labstack/echo/v4" - "github.com/ybkuroki/go-webapp-sample/model" + "github.com/ybkuroki/go-webapp-sample/service" ) -// BookList is -func BookList(db *gorm.DB) echo.HandlerFunc { +// GetBookList is +func GetBookList() echo.HandlerFunc { return func(c echo.Context) error { - book := model.NewBook("test", "123-123-1", model.NewCategory("technical"), model.NewFormat("paper")) - result, _ := book.FindByID(db, 1) - return c.JSON(http.StatusOK, result) + return c.JSON(http.StatusOK, service.FindAllBooks()) } } diff --git a/main.go b/main.go index 22a15998..ee82ba9f 100644 --- a/main.go +++ b/main.go @@ -1,56 +1,21 @@ package main import ( - "github.com/ybkuroki/go-webapp-sample/controller" "github.com/ybkuroki/go-webapp-sample/model" - - "github.com/labstack/echo/v4" - "github.com/labstack/echo/v4/middleware" - - "github.com/jinzhu/gorm" - _ "github.com/jinzhu/gorm/dialects/sqlite" + "github.com/ybkuroki/go-webapp-sample/repository" + "github.com/ybkuroki/go-webapp-sample/router" ) -// go get -u github.com/jinzhu/gorm -// go get -u github.com/mattn/go-sqlite3 -// go get -u github.com/ybkuroki/go-webapp-sample func main() { - db := initDB() - - e := echo.New() - - e.Use(middleware.Logger()) - e.Use(middleware.Recover()) - - e.GET("/book/list", controller.BookList(db)) - - e.Start(":8080") - - //c := model.NewCategory("technical") - //c.Create(db) - - //f := model.NewFormat("paper") - //f.Create(db) - - //book := model.NewBook("test", "123-123-1", c, f) - //book.Create(db) + repository.InitDB() + db := repository.GetConnection() - //result, _ := book.FindByID(db, 1) - //fmt.Println(result.Title, result.Isbn, result.Category.Name, result.Format.Name) - - //category := model.NewCategory("magazine") - //category.Create(db) - - defer db.Close() -} - -func initDB() *gorm.DB { - db, err := gorm.Open("sqlite3", "book.db") - if err != nil { - panic(err) - } db.AutoMigrate(&model.Book{}) db.AutoMigrate(&model.Category{}) db.AutoMigrate(&model.Format{}) - return db + + router := router.Init() + router.Start(":8080") + + defer db.Close() } diff --git a/model/book.go b/model/book.go index 735af51d..efe9c741 100644 --- a/model/book.go +++ b/model/book.go @@ -1,19 +1,21 @@ package model import ( + "encoding/json" + "github.com/jinzhu/gorm" "github.com/ybkuroki/go-webapp-sample/repository" ) // Book is struct type Book struct { - gorm.Model - Title string - Isbn string - CategoryID int - Category *Category - FormatID int - Format *Format + ID uint `gorm:"primary_key" json:"id"` + Title string `json:"title"` + Isbn string `json:"isbn"` + CategoryID uint `json:"category_id"` + Category *Category `json:"category"` + FormatID uint `json:"format_id"` + Format *Format `json:"format"` } // NewBook is constructor @@ -82,3 +84,9 @@ func (b *Book) Create(db *gorm.DB) (*Book, error) { } return b, nil } + +// ToString is return string of object +func (b *Book) ToString() (string, error) { + bytes, error := json.Marshal(b) + return string(bytes), error +} diff --git a/model/category.go b/model/category.go index 85694fcf..d5cc7d09 100644 --- a/model/category.go +++ b/model/category.go @@ -1,13 +1,15 @@ package model import ( + "encoding/json" + "github.com/jinzhu/gorm" ) // Category is struct type Category struct { - gorm.Model - Name string + ID uint `gorm:"primary_key" json:"id"` + Name string `json:"name"` } // NewCategory is constructor @@ -45,3 +47,9 @@ func (c *Category) Create(db *gorm.DB) (*Category, error) { } return c, nil } + +// ToString is return string of object +func (c *Category) ToString() (string, error) { + bytes, error := json.Marshal(c) + return string(bytes), error +} diff --git a/model/format.go b/model/format.go index 420957fc..9865c1f1 100644 --- a/model/format.go +++ b/model/format.go @@ -1,11 +1,15 @@ package model -import "github.com/jinzhu/gorm" +import ( + "encoding/json" + + "github.com/jinzhu/gorm" +) // Format is struct type Format struct { - gorm.Model - Name string + ID uint `gorm:"primary_key" json:"id"` + Name string `json:"name"` } // NewFormat is constructor @@ -43,3 +47,9 @@ func (f *Format) Create(db *gorm.DB) (*Format, error) { } return f, nil } + +// ToString is return string of object +func (f *Format) ToString() (string, error) { + bytes, error := json.Marshal(f) + return string(bytes), error +} diff --git a/repository/repository.go b/repository/repository.go index 047d791d..e39b4660 100644 --- a/repository/repository.go +++ b/repository/repository.go @@ -1,9 +1,28 @@ package repository import ( + "fmt" + "github.com/jinzhu/gorm" + _ "github.com/jinzhu/gorm/dialects/sqlite" // indirect ) +var db *gorm.DB +var err error + +// InitDB is +func InitDB() { + db, err = gorm.Open("sqlite3", "book.db") + if err != nil { + panic(fmt.Sprintf("[Error]: %s", err)) + } +} + +// GetConnection is +func GetConnection() *gorm.DB { + return db +} + // Relations is func Relations() func(*gorm.DB) *gorm.DB { return func(db *gorm.DB) *gorm.DB { diff --git a/router/router.go b/router/router.go new file mode 100644 index 00000000..9e04b02b --- /dev/null +++ b/router/router.go @@ -0,0 +1,42 @@ +package router + +import ( + "net/http" + + "github.com/labstack/echo/v4" + "github.com/labstack/echo/v4/middleware" + "github.com/ybkuroki/go-webapp-sample/controller" +) + +// Init is +func Init() *echo.Echo { + e := echo.New() + + e.Use(middleware.Logger()) + e.Use(middleware.CORSWithConfig(middleware.CORSConfig{ + AllowCredentials: true, + AllowOrigins: []string{"*"}, + AllowHeaders: []string{ + echo.HeaderAccessControlAllowHeaders, + echo.HeaderContentType, + echo.HeaderContentLength, + echo.HeaderAcceptEncoding, + }, + AllowMethods: []string{ + http.MethodGet, + http.MethodPost, + }, + MaxAge: 86400, + })) + e.Use(middleware.Recover()) + + api := e.Group("/api") + { + book := api.Group("/book") + { + book.GET("/list", controller.GetBookList()) + } + } + + return e +} diff --git a/service/book.go b/service/book.go new file mode 100644 index 00000000..08182a06 --- /dev/null +++ b/service/book.go @@ -0,0 +1,14 @@ +package service + +import ( + "github.com/ybkuroki/go-webapp-sample/model" + "github.com/ybkuroki/go-webapp-sample/repository" +) + +// FindAllBooks is +func FindAllBooks() *[]model.Book { + db := repository.GetConnection() + book := model.Book{} + result, _ := book.FindAll(db) + return result +}