mirror of
https://gitee.com/dromara/mayfly-go
synced 2026-01-02 20:56:36 +08:00
feat: 新增简易版ioc
This commit is contained in:
@@ -9,7 +9,7 @@ import (
|
||||
)
|
||||
|
||||
type Msg struct {
|
||||
MsgApp application.Msg
|
||||
MsgApp application.Msg `inject:""`
|
||||
}
|
||||
|
||||
// 获取账号接收的消息列表
|
||||
|
||||
@@ -2,12 +2,15 @@ package application
|
||||
|
||||
import (
|
||||
"mayfly-go/internal/msg/infrastructure/persistence"
|
||||
"mayfly-go/pkg/ioc"
|
||||
)
|
||||
|
||||
var (
|
||||
msgApp = newMsgApp(persistence.GetMsgRepo())
|
||||
)
|
||||
func init() {
|
||||
persistence.Init()
|
||||
|
||||
ioc.Register(new(msgAppImpl), ioc.WithComponentName("MsgApp"))
|
||||
}
|
||||
|
||||
func GetMsgApp() Msg {
|
||||
return msgApp
|
||||
return ioc.Get[Msg]("MsgApp")
|
||||
}
|
||||
|
||||
@@ -19,27 +19,21 @@ type Msg interface {
|
||||
CreateAndSend(la *model.LoginAccount, msg *dto.SysMsg)
|
||||
}
|
||||
|
||||
func newMsgApp(msgRepo repository.Msg) Msg {
|
||||
return &msgAppImpl{
|
||||
msgRepo: msgRepo,
|
||||
}
|
||||
}
|
||||
|
||||
type msgAppImpl struct {
|
||||
msgRepo repository.Msg
|
||||
MsgRepo repository.Msg `inject:""`
|
||||
}
|
||||
|
||||
func (a *msgAppImpl) GetPageList(condition *entity.Msg, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
||||
return a.msgRepo.GetPageList(condition, pageParam, toEntity)
|
||||
return a.MsgRepo.GetPageList(condition, pageParam, toEntity)
|
||||
}
|
||||
|
||||
func (a *msgAppImpl) Create(ctx context.Context, msg *entity.Msg) {
|
||||
a.msgRepo.Insert(ctx, msg)
|
||||
a.MsgRepo.Insert(ctx, msg)
|
||||
}
|
||||
|
||||
func (a *msgAppImpl) CreateAndSend(la *model.LoginAccount, wmsg *dto.SysMsg) {
|
||||
now := time.Now()
|
||||
msg := &entity.Msg{Type: 2, Msg: wmsg.Msg, RecipientId: int64(la.Id), CreateTime: &now, CreatorId: la.Id, Creator: la.Username}
|
||||
a.msgRepo.Insert(context.TODO(), msg)
|
||||
a.MsgRepo.Insert(context.TODO(), msg)
|
||||
ws.SendJsonMsg(ws.UserId(la.Id), wmsg.ClientId, wmsg)
|
||||
}
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package persistence
|
||||
|
||||
import "mayfly-go/internal/msg/domain/repository"
|
||||
|
||||
var (
|
||||
msgRepo = newMsgRepo()
|
||||
import (
|
||||
"mayfly-go/internal/msg/domain/repository"
|
||||
"mayfly-go/pkg/ioc"
|
||||
)
|
||||
|
||||
func GetMsgRepo() repository.Msg {
|
||||
return msgRepo
|
||||
func Init() {
|
||||
ioc.Register(newMsgRepo(), ioc.WithComponentName("MsgRepo"))
|
||||
}
|
||||
|
||||
func GetMsgRepo() repository.Msg {
|
||||
return ioc.Get[repository.Msg]("msgRepo")
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package router
|
||||
|
||||
import (
|
||||
"mayfly-go/internal/msg/api"
|
||||
"mayfly-go/internal/msg/application"
|
||||
"mayfly-go/pkg/ioc"
|
||||
"mayfly-go/pkg/req"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -10,9 +10,9 @@ import (
|
||||
|
||||
func InitMsgRouter(router *gin.RouterGroup) {
|
||||
msg := router.Group("msgs")
|
||||
a := &api.Msg{
|
||||
MsgApp: application.GetMsgApp(),
|
||||
}
|
||||
|
||||
a := new(api.Msg)
|
||||
ioc.Inject(a)
|
||||
|
||||
req.NewGet("/self", a.GetMsgs).Group(msg)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package router
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Init(router *gin.RouterGroup) {
|
||||
InitMsgRouter(router)
|
||||
|
||||
Reference in New Issue
Block a user