@@ -3,16 +3,19 @@ package impl
33import (
44 "context"
55
6+ "github.com/go-kratos/kratos/v2/errors"
67 "github.com/go-kratos/kratos/v2/log"
78 "gorm.io/gen"
89 "gorm.io/gen/field"
10+ "gorm.io/gorm"
911
1012 "github.com/aide-family/moon/cmd/palace/internal/biz/bo"
1113 "github.com/aide-family/moon/cmd/palace/internal/biz/do"
1214 "github.com/aide-family/moon/cmd/palace/internal/biz/do/system"
1315 "github.com/aide-family/moon/cmd/palace/internal/biz/repository"
1416 "github.com/aide-family/moon/cmd/palace/internal/biz/vobj"
1517 "github.com/aide-family/moon/cmd/palace/internal/data"
18+ "github.com/aide-family/moon/pkg/merr"
1619 "github.com/aide-family/moon/pkg/util/crypto"
1720 "github.com/aide-family/moon/pkg/util/slices"
1821 "github.com/aide-family/moon/pkg/util/validate"
@@ -30,7 +33,7 @@ type teamRepoImpl struct {
3033 helper * log.Helper
3134}
3235
33- func (r * teamRepoImpl ) Create (ctx context.Context , team bo.CreateTeamRequest ) (do. Team , error ) {
36+ func (r * teamRepoImpl ) Create (ctx context.Context , team bo.CreateTeamRequest ) error {
3437 teamMutation := getMainQuery (ctx , r ).Team
3538 teamDo := & system.Team {
3639 Name : team .GetName (),
@@ -47,13 +50,10 @@ func (r *teamRepoImpl) Create(ctx context.Context, team bo.CreateTeamRequest) (d
4750 AlarmDBConfig : crypto .NewObject (team .GetAlarmDBConfig ()),
4851 }
4952 teamDo .WithContext (ctx )
50- if err := teamMutation .WithContext (ctx ).Create (teamDo ); err != nil {
51- return nil , err
52- }
53- return teamDo , nil
53+ return teamMutation .WithContext (ctx ).Create (teamDo )
5454}
5555
56- func (r * teamRepoImpl ) Update (ctx context.Context , team bo.UpdateTeamRequest ) (do. Team , error ) {
56+ func (r * teamRepoImpl ) Update (ctx context.Context , team bo.UpdateTeamRequest ) error {
5757 teamMutation := getMainQuery (ctx , r ).Team
5858 wrappers := []gen.Condition {
5959 teamMutation .ID .Eq (team .GetTeam ().GetID ()),
@@ -64,10 +64,7 @@ func (r *teamRepoImpl) Update(ctx context.Context, team bo.UpdateTeamRequest) (d
6464 teamMutation .Logo .Value (team .GetLogo ()),
6565 }
6666 _ , err := teamMutation .WithContext (ctx ).Where (wrappers ... ).UpdateColumnSimple (mutations ... )
67- if err != nil {
68- return nil , err
69- }
70- return r .FindByID (ctx , team .GetTeam ().GetID ())
67+ return err
7168}
7269
7370func (r * teamRepoImpl ) Delete (ctx context.Context , id uint32 ) error {
@@ -81,7 +78,7 @@ func (r *teamRepoImpl) Delete(ctx context.Context, id uint32) error {
8178
8279func (r * teamRepoImpl ) FindByID (ctx context.Context , id uint32 ) (do.Team , error ) {
8380 systemQuery := getMainQuery (ctx , r ).Team
84- teamDo , err := systemQuery .WithContext (ctx ).Where (systemQuery .ID .Eq (id )).First ()
81+ teamDo , err := systemQuery .WithContext (ctx ).Preload ( field . Associations ). Where (systemQuery .ID .Eq (id )).First ()
8582 if err != nil {
8683 return nil , teamNotFound (err )
8784 }
@@ -91,7 +88,7 @@ func (r *teamRepoImpl) FindByID(ctx context.Context, id uint32) (do.Team, error)
9188func (r * teamRepoImpl ) List (ctx context.Context , req * bo.TeamListRequest ) (* bo.TeamListReply , error ) {
9289 query := getMainQuery (ctx , r )
9390 teamQuery := query .Team
94- wrapper := teamQuery .WithContext (ctx )
91+ wrapper := teamQuery .WithContext (ctx ). Preload ( field . Associations )
9592 if ! validate .TextIsNull (req .Keyword ) {
9693 wrapper = wrapper .Where (teamQuery .Name .Like (req .Keyword ))
9794 }
@@ -138,3 +135,35 @@ func (r *teamRepoImpl) List(ctx context.Context, req *bo.TeamListRequest) (*bo.T
138135 rows := slices .Map (teamDos , func (teamDo * system.Team ) do.Team { return teamDo })
139136 return req .ToListReply (rows ), nil
140137}
138+
139+ func (r * teamRepoImpl ) CheckNameUnique (ctx context.Context , name string , teamID uint32 ) error {
140+ teamQuery := getMainQuery (ctx , r ).Team
141+ wrapper := teamQuery .WithContext (ctx )
142+ if teamID > 0 {
143+ wrapper = wrapper .Where (teamQuery .ID .Neq (teamID ))
144+ }
145+ teamDo , err := wrapper .Where (teamQuery .Name .Eq (name )).First ()
146+ if err != nil {
147+ if errors .Is (err , gorm .ErrRecordNotFound ) {
148+ return nil
149+ }
150+ return err
151+ }
152+ if teamDo != nil {
153+ return merr .ErrorConflict ("team name already exists" )
154+ }
155+ return nil
156+ }
157+
158+ func (r * teamRepoImpl ) FindByName (ctx context.Context , name string ) (do.Team , error ) {
159+ teamQuery := getMainQuery (ctx , r ).Team
160+ wrapper := teamQuery .WithContext (ctx ).Preload (field .Associations )
161+ teamDo , err := wrapper .Where (teamQuery .Name .Eq (name )).First ()
162+ if err != nil {
163+ return nil , teamNotFound (err )
164+ }
165+ if teamDo == nil {
166+ return nil , merr .ErrorNotFound ("team not found" )
167+ }
168+ return teamDo , nil
169+ }
0 commit comments