feat: complete template
This commit is contained in:
@@ -1 +0,0 @@
|
||||
package users
|
||||
@@ -1 +0,0 @@
|
||||
package modules
|
||||
18
backend/modules/users/controller.go
Normal file
18
backend/modules/users/controller.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package users
|
||||
|
||||
import "github.com/gofiber/fiber/v3"
|
||||
|
||||
// @provider
|
||||
type Controller struct {
|
||||
svc *Service
|
||||
}
|
||||
|
||||
// List
|
||||
func (c *Controller) List(ctx fiber.Ctx) error {
|
||||
resp, err := c.svc.List(ctx.Context())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return ctx.JSON(resp)
|
||||
}
|
||||
53
backend/modules/users/provider.gen.go
Executable file
53
backend/modules/users/provider.gen.go
Executable file
@@ -0,0 +1,53 @@
|
||||
package users
|
||||
|
||||
import (
|
||||
"backend/providers/http"
|
||||
"database/sql"
|
||||
|
||||
"git.ipao.vip/rogeecn/atom"
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
"git.ipao.vip/rogeecn/atom/contracts"
|
||||
"git.ipao.vip/rogeecn/atom/utils/opt"
|
||||
)
|
||||
|
||||
func Provide(opts ...opt.Option) error {
|
||||
if err := container.Container.Provide(func(
|
||||
svc *Service,
|
||||
) (*Controller, error) {
|
||||
obj := &Controller{
|
||||
svc: svc,
|
||||
}
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := container.Container.Provide(func(
|
||||
controller *Controller,
|
||||
http *http.Service,
|
||||
) (contracts.HttpRoute, error) {
|
||||
obj := &Router{
|
||||
controller: controller,
|
||||
http: http,
|
||||
}
|
||||
return obj, nil
|
||||
}, atom.GroupRoutes); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := container.Container.Provide(func(
|
||||
db *sql.DB,
|
||||
) (*Service, error) {
|
||||
obj := &Service{
|
||||
db: db,
|
||||
}
|
||||
if err := obj.Prepare(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
25
backend/modules/users/router.go
Executable file
25
backend/modules/users/router.go
Executable file
@@ -0,0 +1,25 @@
|
||||
package users
|
||||
|
||||
import (
|
||||
"backend/providers/http"
|
||||
|
||||
_ "git.ipao.vip/rogeecn/atom"
|
||||
_ "git.ipao.vip/rogeecn/atom/contracts"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// @provider:except contracts.HttpRoute atom.GroupRoutes
|
||||
type Router struct {
|
||||
http *http.Service
|
||||
|
||||
controller *Controller
|
||||
}
|
||||
|
||||
func (r *Router) Register() error {
|
||||
group := r.http.Engine.Group("users")
|
||||
log.Infof("register route group: %s", group.(*fiber.Group).Prefix)
|
||||
group.Get("", r.controller.List)
|
||||
|
||||
return nil
|
||||
}
|
||||
36
backend/modules/users/service.go
Normal file
36
backend/modules/users/service.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package users
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"backend/database/models/qvyun/public/model"
|
||||
"backend/database/models/qvyun/public/table"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// @provider:except
|
||||
type Service struct {
|
||||
db *sql.DB
|
||||
log *logrus.Entry `inject:"false"`
|
||||
}
|
||||
|
||||
func (svc *Service) Prepare() error {
|
||||
svc.log = logrus.WithField("module", "users.service")
|
||||
return nil
|
||||
}
|
||||
|
||||
// List
|
||||
func (svc *Service) List(ctx context.Context) ([]model.Users, error) {
|
||||
tbl := table.Users
|
||||
stmt := tbl.SELECT(tbl.AllColumns)
|
||||
svc.log.WithField("method", "List").Debug(stmt.DebugSql())
|
||||
|
||||
var items []model.Users
|
||||
if err := stmt.QueryContext(ctx, svc.db, &items); err != nil {
|
||||
return nil, errors.Wrap(err, "failed to query users")
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
Reference in New Issue
Block a user