init
This commit is contained in:
21
handlers/error.handler.go
Normal file
21
handlers/error.handler.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func CustomHTTPErrorHandler(err error, c echo.Context) {
|
||||
code := http.StatusInternalServerError
|
||||
if he, ok := err.(*echo.HTTPError); ok {
|
||||
code = he.Code
|
||||
}
|
||||
c.Logger().Error(err)
|
||||
|
||||
errorPage := fmt.Sprintf("views/%d.html", code)
|
||||
if err := c.File(errorPage); err != nil {
|
||||
c.Logger().Error(err)
|
||||
}
|
||||
}
|
||||
10
handlers/routes.go
Normal file
10
handlers/routes.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package handlers
|
||||
|
||||
import "github.com/labstack/echo/v4"
|
||||
|
||||
func SetupRoutes(app *echo.Echo, h *UserHandler) {
|
||||
group := app.Group("/user")
|
||||
|
||||
group.GET("", h.HandlerShowUsers)
|
||||
group.GET("/details/:id", h.HandlerShowUserById)
|
||||
}
|
||||
77
handlers/user.handlers.go
Normal file
77
handlers/user.handlers.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
"github.com/emarifer/go-templ-project-structure/services"
|
||||
"github.com/emarifer/go-templ-project-structure/views/user"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"golang.org/x/text/cases"
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
type UserService interface {
|
||||
GetAllUsers() ([]services.User, error)
|
||||
GetUserById(id int) (services.User, error)
|
||||
}
|
||||
|
||||
func New(us UserService) *UserHandler {
|
||||
return &UserHandler{
|
||||
UserService: us,
|
||||
}
|
||||
}
|
||||
|
||||
type UserHandler struct {
|
||||
UserService UserService
|
||||
}
|
||||
|
||||
func (uh *UserHandler) HandlerShowUsers(c echo.Context) error {
|
||||
udata, err := uh.UserService.GetAllUsers()
|
||||
if err != nil {
|
||||
// fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
si := user.ShowIndex("| Home", user.Show(udata))
|
||||
|
||||
return uh.View(c, si)
|
||||
}
|
||||
|
||||
func (uh *UserHandler) HandlerShowUserById(c echo.Context) error {
|
||||
idParam, _ := strconv.Atoi(c.Param("id"))
|
||||
|
||||
tz := ""
|
||||
if len(c.Request().Header["X-Timezone"]) != 0 {
|
||||
tz = c.Request().Header["X-Timezone"][0]
|
||||
}
|
||||
|
||||
udata, err := uh.UserService.GetUserById(idParam)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows in result set") {
|
||||
return echo.NewHTTPError(http.StatusNotFound, err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
di := user.DetailsIndex(
|
||||
fmt.Sprintf(
|
||||
"| User details %s",
|
||||
cases.Title(language.English).String(udata.Username),
|
||||
),
|
||||
user.Details(tz, udata),
|
||||
)
|
||||
|
||||
// return c.JSON(http.StatusOK, udata)
|
||||
return uh.View(c, di)
|
||||
}
|
||||
|
||||
func (uh *UserHandler) View(c echo.Context, cmp templ.Component) error {
|
||||
c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTML)
|
||||
|
||||
return cmp.Render(c.Request().Context(), c.Response().Writer)
|
||||
}
|
||||
Reference in New Issue
Block a user