Skip to content

Commit

Permalink
Merge pull request #5 from ybkuroki/develop
Browse files Browse the repository at this point in the history
Implement routing by echo
  • Loading branch information
ybkuroki authored Apr 29, 2020
2 parents 4eaf1a7 + de95e97 commit 7bae869
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 63 deletions.
11 changes: 4 additions & 7 deletions controller/book.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
53 changes: 9 additions & 44 deletions main.go
Original file line number Diff line number Diff line change
@@ -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()
}
22 changes: 15 additions & 7 deletions model/book.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}
12 changes: 10 additions & 2 deletions model/category.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}
16 changes: 13 additions & 3 deletions model/format.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}
19 changes: 19 additions & 0 deletions repository/repository.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
42 changes: 42 additions & 0 deletions router/router.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 14 additions & 0 deletions service/book.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 7bae869

Please sign in to comment.