Skip to content

Commit

Permalink
Merge pull request #34 from ybkuroki/develop
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
ybkuroki authored Dec 28, 2020
2 parents c2e59d4 + 5ba7f25 commit 034961a
Show file tree
Hide file tree
Showing 28 changed files with 509 additions and 370 deletions.
25 changes: 4 additions & 21 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,31 +49,14 @@ const (
DOC = "docker"
)

var config *Config
var env *string

// Load reads the settings written to the yml file
func Load() {
env = flag.String("env", "develop", "To switch configurations.")
func Load() (*Config, string) {
env := flag.String("env", "develop", "To switch configurations.")
flag.Parse()
config = &Config{}
config := &Config{}
if err := configor.Load(config, "application."+*env+".yml"); err != nil {
fmt.Printf("Failed to read application.%s.yml: %s", *env, err)
os.Exit(2)
}
}

// GetConfig returns the configuration data.
func GetConfig() *Config {
return config
}

// SetConfig sets configuration data.
func SetConfig(conf *Config) {
config = conf
}

// GetEnv returns the environment variable.
func GetEnv() *string {
return env
return config, *env
}
72 changes: 39 additions & 33 deletions controller/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,62 @@ import (
"net/http"

"github.com/labstack/echo/v4"
"github.com/ybkuroki/go-webapp-sample/config"
"github.com/ybkuroki/go-webapp-sample/model"
"github.com/ybkuroki/go-webapp-sample/mycontext"
"github.com/ybkuroki/go-webapp-sample/service"
"github.com/ybkuroki/go-webapp-sample/session"
)

var dummyAccount = model.NewAccountWithPlainPassword("test", "test", 1)
// AccountController is a controller for managing user account.
type AccountController struct {
context mycontext.Context
service *service.AccountService
dummyAccount *model.Account
}

// GetLoginStatus returns the status of login.
func GetLoginStatus() echo.HandlerFunc {
return func(c echo.Context) error {
return c.JSON(http.StatusOK, true)
// NewAccountController is constructor.
func NewAccountController(context mycontext.Context) *AccountController {
return &AccountController{
context: context,
service: service.NewAccountService(context),
dummyAccount: model.NewAccountWithPlainPassword("test", "test", 1),
}
}

// GetLoginStatus returns the status of login.
func (controller *AccountController) GetLoginStatus(c echo.Context) error {
return c.JSON(http.StatusOK, true)
}

// GetLoginAccount returns the account data of logged in user.
func GetLoginAccount() echo.HandlerFunc {
return func(c echo.Context) error {
if !config.GetConfig().Extension.SecurityEnabled {
return c.JSON(http.StatusOK, dummyAccount)
}
return c.JSON(http.StatusOK, session.GetAccount(c))
func (controller *AccountController) GetLoginAccount(c echo.Context) error {
if !controller.context.GetConfig().Extension.SecurityEnabled {
return c.JSON(http.StatusOK, controller.dummyAccount)
}
return c.JSON(http.StatusOK, session.GetAccount(c))
}

// PostLogin is the method to login using username and password by http post.
func PostLogin() echo.HandlerFunc {
return func(c echo.Context) error {
username := c.FormValue("username")
password := c.FormValue("password")

account := session.GetAccount(c)
if account == nil {
authenticate, a := service.AuthenticateByUsernameAndPassword(username, password)
if authenticate {
_ = session.SetAccount(c, a)
_ = session.Save(c)
return c.JSON(http.StatusOK, a)
}
return c.NoContent(http.StatusUnauthorized)
func (controller *AccountController) PostLogin(c echo.Context) error {
username := c.FormValue("username")
password := c.FormValue("password")

account := session.GetAccount(c)
if account == nil {
authenticate, a := controller.service.AuthenticateByUsernameAndPassword(username, password)
if authenticate {
_ = session.SetAccount(c, a)
_ = session.Save(c)
return c.JSON(http.StatusOK, a)
}
return c.JSON(http.StatusOK, account)
return c.NoContent(http.StatusUnauthorized)
}
return c.JSON(http.StatusOK, account)
}

// PostLogout is the method to logout by http post.
func PostLogout() echo.HandlerFunc {
return func(c echo.Context) error {
_ = session.SetAccount(c, nil)
_ = session.Delete(c)
return c.NoContent(http.StatusOK)
}
func (controller *AccountController) PostLogout(c echo.Context) error {
_ = session.SetAccount(c, nil)
_ = session.Delete(c)
return c.NoContent(http.StatusOK)
}
17 changes: 11 additions & 6 deletions controller/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import (
"net/http/httptest"
"testing"

"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"github.com/ybkuroki/go-webapp-sample/model"
"github.com/ybkuroki/go-webapp-sample/test"
)

func TestGetLoginStatus(t *testing.T) {
router := test.Prepare()
router.GET(APIAccountLoginStatus, GetLoginStatus())
router, context := test.Prepare()

account := NewAccountController(context)
router.GET(APIAccountLoginStatus, func(c echo.Context) error { return account.GetLoginStatus(c) })

req := httptest.NewRequest("GET", APIAccountLoginStatus, nil)
rec := httptest.NewRecorder()
Expand All @@ -24,15 +27,17 @@ func TestGetLoginStatus(t *testing.T) {
}

func TestGetLoginAccount(t *testing.T) {
router := test.Prepare()
router.GET(APIAccountLoginAccount, GetLoginAccount())
router, context := test.Prepare()

account := NewAccountController(context)
router.GET(APIAccountLoginAccount, func(c echo.Context) error { return account.GetLoginAccount(c) })

req := httptest.NewRequest("GET", APIAccountLoginAccount, nil)
rec := httptest.NewRecorder()

router.ServeHTTP(rec, req)

account := model.NewAccountWithPlainPassword("test", "test", 1)
entity := model.NewAccountWithPlainPassword("test", "test", 1)
assert.Equal(t, http.StatusOK, rec.Code)
assert.JSONEq(t, test.ConvertToString(account), rec.Body.String())
assert.JSONEq(t, test.ConvertToString(entity), rec.Body.String())
}
94 changes: 48 additions & 46 deletions controller/book.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,71 +6,73 @@ import (

"github.com/labstack/echo/v4"
"github.com/ybkuroki/go-webapp-sample/model/dto"
"github.com/ybkuroki/go-webapp-sample/mycontext"
"github.com/ybkuroki/go-webapp-sample/service"
)

// BookController is a controller for managing books.
type BookController struct {
context mycontext.Context
service *service.BookService
}

// NewBookController is constructor.
func NewBookController(context mycontext.Context) *BookController {
return &BookController{context: context, service: service.NewBookService(context)}
}

// GetBookList returns the list of all books.
func GetBookList() echo.HandlerFunc {
return func(c echo.Context) error {
page, _ := strconv.Atoi(c.QueryParam("page"))
size, _ := strconv.Atoi(c.QueryParam("size"))
func (controller *BookController) GetBookList(c echo.Context) error {
page, _ := strconv.Atoi(c.QueryParam("page"))
size, _ := strconv.Atoi(c.QueryParam("size"))

return c.JSON(http.StatusOK, service.FindAllBooksByPage(page, size))
}
return c.JSON(http.StatusOK, controller.service.FindAllBooksByPage(page, size))
}

// GetBookSearch returns the list of matched books by searching.
func GetBookSearch() echo.HandlerFunc {
return func(c echo.Context) error {
title := c.QueryParam("query")
page, _ := strconv.Atoi(c.QueryParam("page"))
size, _ := strconv.Atoi(c.QueryParam("size"))
func (controller *BookController) GetBookSearch(c echo.Context) error {
title := c.QueryParam("query")
page, _ := strconv.Atoi(c.QueryParam("page"))
size, _ := strconv.Atoi(c.QueryParam("size"))

return c.JSON(http.StatusOK, service.FindBooksByTitle(title, page, size))
}
return c.JSON(http.StatusOK, controller.service.FindBooksByTitle(title, page, size))
}

// PostBookRegist register a new book by http post.
func PostBookRegist() echo.HandlerFunc {
return func(c echo.Context) error {
dto := dto.NewRegBookDto()
if err := c.Bind(dto); err != nil {
return c.JSON(http.StatusBadRequest, dto)
}
book, result := service.RegisterBook(dto)
if result != nil {
return c.JSON(http.StatusBadRequest, result)
}
return c.JSON(http.StatusOK, book)
func (controller *BookController) PostBookRegist(c echo.Context) error {
dto := dto.NewRegBookDto()
if err := c.Bind(dto); err != nil {
return c.JSON(http.StatusBadRequest, dto)
}
book, result := controller.service.RegisterBook(dto)
if result != nil {
return c.JSON(http.StatusBadRequest, result)
}
return c.JSON(http.StatusOK, book)
}

// PostBookEdit edit the existing book by http post.
func PostBookEdit() echo.HandlerFunc {
return func(c echo.Context) error {
dto := dto.NewChgBookDto()
if err := c.Bind(dto); err != nil {
return c.JSON(http.StatusBadRequest, dto)
}
book, result := service.EditBook(dto)
if result != nil {
return c.JSON(http.StatusBadRequest, result)
}
return c.JSON(http.StatusOK, book)
func (controller *BookController) PostBookEdit(c echo.Context) error {
dto := dto.NewChgBookDto()
if err := c.Bind(dto); err != nil {
return c.JSON(http.StatusBadRequest, dto)
}
book, result := controller.service.EditBook(dto)
if result != nil {
return c.JSON(http.StatusBadRequest, result)
}
return c.JSON(http.StatusOK, book)
}

// PostBookDelete deletes the existing book by http post.
func PostBookDelete() echo.HandlerFunc {
return func(c echo.Context) error {
dto := dto.NewChgBookDto()
if err := c.Bind(dto); err != nil {
return c.JSON(http.StatusBadRequest, dto)
}
book, result := service.DeleteBook(dto)
if result != nil {
return c.JSON(http.StatusBadRequest, result)
}
return c.JSON(http.StatusOK, book)
func (controller *BookController) PostBookDelete(c echo.Context) error {
dto := dto.NewChgBookDto()
if err := c.Bind(dto); err != nil {
return c.JSON(http.StatusBadRequest, dto)
}
book, result := controller.service.DeleteBook(dto)
if result != nil {
return c.JSON(http.StatusBadRequest, result)
}
return c.JSON(http.StatusOK, book)
}
Loading

0 comments on commit 034961a

Please sign in to comment.