Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring #34

Merged
merged 3 commits into from
Dec 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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