Skip to content

Instantly share code, notes, and snippets.

@mehmetcemyucel
Created April 28, 2022 20:59
Show Gist options
  • Save mehmetcemyucel/706b3b377b49bd9e7f4612fc784f1db5 to your computer and use it in GitHub Desktop.
Save mehmetcemyucel/706b3b377b49bd9e7f4612fc784f1db5 to your computer and use it in GitHub Desktop.
package api
import (
xconv "github.com/howcrazy/xconv"
"github.com/labstack/echo/v4"
"github.com/valyala/fasthttp"
"go.uber.org/zap"
"helloWorld/pkg/model/example/dto/request"
"helloWorld/pkg/model/example/dto/response"
"helloWorld/pkg/model/example/entity"
"helloWorld/pkg/service"
)
type IExampleApi interface {
Check(*zap.Logger)
}
type ExampleApi struct {
ExampleService service.IExampleService
log *zap.Logger
}
func NewExampleApi(s service.IExampleService, log *zap.Logger) ExampleApi {
return ExampleApi{ExampleService: s, log: log}
}
// Check godoc
// @Summary Check endpoint of go example app
// @Description Check Service details
// @Tags ExampleApi
// @Accept json
// @Produce json
// @Param account body request.ExampleReq true "ExampleReq"
// @Success 200 {object} response.ExampleResp
// @Router /api/v1/check [post]
func (p ExampleApi) Check(c echo.Context) error {
var req request.ExampleReq
if err := c.Bind(&req); err != nil {
return err
}
if checkEntity, err := p.ExampleService.Check(req); err != nil {
return err
} else {
return c.JSON(fasthttp.StatusOK, ofSlice(checkEntity))
}
}
func ofSlice(examples []entity.Example) []response.ExampleResp {
ls := make([]response.ExampleResp, len(examples))
for i := 0; i < len(examples); i++ {
xconv.Convert(examples[i], &ls[i])
}
return ls
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment