From 9893a20645532cf80d22023147eef763c0757867 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Sat, 6 Jul 2024 15:07:50 +0700 Subject: [PATCH] fix: create group when create user --- go.mod | 2 +- go.sum | 2 ++ internal/user/user.repository.go | 32 +++++++++++++++++++++++++------- internal/user/user.service.go | 14 +++++++------- internal/user/user.utils.go | 15 ++++++++++----- 5 files changed, 45 insertions(+), 20 deletions(-) diff --git a/go.mod b/go.mod index e02eef3..46239a0 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/golang/mock v1.6.0 github.com/google/uuid v1.6.0 github.com/isd-sgcu/rpkm67-go-proto v0.2.8 - github.com/isd-sgcu/rpkm67-model v0.0.6 + github.com/isd-sgcu/rpkm67-model v0.0.7 github.com/joho/godotenv v1.5.1 github.com/pkg/errors v0.9.1 github.com/redis/go-redis/v9 v9.5.3 diff --git a/go.sum b/go.sum index 3ac9b1a..af3a489 100644 --- a/go.sum +++ b/go.sum @@ -23,6 +23,8 @@ github.com/isd-sgcu/rpkm67-go-proto v0.2.8 h1:YDkxRcu204XD70E+xJSYt/4XmwXuM13nVN github.com/isd-sgcu/rpkm67-go-proto v0.2.8/go.mod h1:w+UCeQnJ3wBuJ7Tyf8LiBiPZVb1KlecjMNCB7kBeL7M= github.com/isd-sgcu/rpkm67-model v0.0.6 h1:pYlqOmeXGQIfHdOhyAta4kXkqnoLc4X3KWcAjPrAuds= github.com/isd-sgcu/rpkm67-model v0.0.6/go.mod h1:dxgLSkrFpbQOXsrzqgepZoEOyZUIG2LBGtm5gsuBbVc= +github.com/isd-sgcu/rpkm67-model v0.0.7 h1:3b8gf1Ocg+Ky4xocKtCqVCB3rFDg90IgEXRwNmHt0OE= +github.com/isd-sgcu/rpkm67-model v0.0.7/go.mod h1:dxgLSkrFpbQOXsrzqgepZoEOyZUIG2LBGtm5gsuBbVc= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= diff --git a/internal/user/user.repository.go b/internal/user/user.repository.go index 81b95a4..e93fe6b 100644 --- a/internal/user/user.repository.go +++ b/internal/user/user.repository.go @@ -1,6 +1,7 @@ package user import ( + "github.com/google/uuid" "github.com/isd-sgcu/rpkm67-model/model" "gorm.io/gorm" ) @@ -8,9 +9,9 @@ import ( type Repository interface { FindOne(id string, user *model.User) error FindByEmail(email string, user *model.User) error - Create(user *model.User) error - CreateUserStamp(stamp *model.Stamp) error + Create(user *model.User, stamp *model.Stamp, group *model.Group) error Update(id string, user *model.User) error + AssignGroup(id string, groupID *uuid.UUID) error } type repositoryImpl struct { @@ -29,14 +30,31 @@ func (r *repositoryImpl) FindByEmail(email string, user *model.User) error { return r.Db.Model(user).Preload("Stamp").First(user, "email = ?", email).Error } -func (r *repositoryImpl) Create(user *model.User) error { - return r.Db.Create(user).Error -} +func (r *repositoryImpl) Create(user *model.User, stamp *model.Stamp, group *model.Group) error { + return r.Db.Transaction(func(tx *gorm.DB) error { + if err := tx.Create(user).Error; err != nil { + return err + } + + stamp.UserID = &user.ID + if err := tx.Create(stamp).Error; err != nil { + return err + } + user.Stamp = stamp + + group.LeaderID = &user.ID + if err := tx.Create(group).Error; err != nil { + return err + } -func (r *repositoryImpl) CreateUserStamp(stamp *model.Stamp) error { - return r.Db.Create(stamp).Error + return nil + }) } func (r *repositoryImpl) Update(id string, user *model.User) error { return r.Db.Model(user).Where("id = ?", id).Updates(user).Error } + +func (r *repositoryImpl) AssignGroup(id string, groupID *uuid.UUID) error { + return r.Db.Model(&model.User{}).Where("id = ?", id).Update("group_id", groupID).Error +} diff --git a/internal/user/user.service.go b/internal/user/user.service.go index 696b2f7..d5d0c52 100644 --- a/internal/user/user.service.go +++ b/internal/user/user.service.go @@ -36,8 +36,10 @@ func (s *serviceImpl) Create(_ context.Context, req *proto.CreateUserRequest) (r Role: constant.Role(req.Role), GroupID: nil, } + newStamp := NewStampModel(&createUser.ID) + newGroup := NewGroupModel(&createUser.ID) - err = s.repo.Create(createUser) + err = s.repo.Create(createUser, newStamp, newGroup) if err != nil { s.log.Named("Create").Error("Create: ", zap.Error(err)) if errors.Is(err, gorm.ErrDuplicatedKey) { @@ -46,14 +48,12 @@ func (s *serviceImpl) Create(_ context.Context, req *proto.CreateUserRequest) (r return nil, err } - emptyStamp := CreateEmptyStamp(&createUser.ID) - createUser.Stamp = emptyStamp - - err = s.repo.CreateUserStamp(emptyStamp) + err = s.repo.AssignGroup(createUser.ID.String(), &newGroup.ID) if err != nil { - s.log.Named("Create").Error("CreateUserStamp: ", zap.Error(err)) - return nil, status.Error(codes.Internal, err.Error()) + s.log.Named("Create").Error("AssignGroup: ", zap.Error(err)) + return nil, err } + createUser.GroupID = &newGroup.ID return &proto.CreateUserResponse{ User: ModelToProto(createUser), diff --git a/internal/user/user.utils.go b/internal/user/user.utils.go index bf2f635..b9f9b86 100644 --- a/internal/user/user.utils.go +++ b/internal/user/user.utils.go @@ -28,8 +28,10 @@ func ModelToProto(in *model.User) *proto.User { PhotoKey: in.PhotoKey, PhotoUrl: in.PhotoUrl, Baan: in.Baan, + GroupId: in.GroupID.String(), ReceiveGift: int32(in.ReceiveGift), Stamp: &stampProto.Stamp{ + Id: in.Stamp.ID.String(), PointA: int32(in.Stamp.PointA), PointB: int32(in.Stamp.PointB), PointC: int32(in.Stamp.PointC), @@ -38,10 +40,6 @@ func ModelToProto(in *model.User) *proto.User { }, } - if in.GroupID != nil { - protoUser.GroupId = in.GroupID.String() - } - return protoUser } @@ -77,7 +75,7 @@ func UpdateRequestToModel(in *proto.UpdateUserRequest) (*model.User, error) { return user, nil } -func CreateEmptyStamp(userId *uuid.UUID) *model.Stamp { +func NewStampModel(userId *uuid.UUID) *model.Stamp { return &model.Stamp{ UserID: userId, PointA: 0, @@ -87,3 +85,10 @@ func CreateEmptyStamp(userId *uuid.UUID) *model.Stamp { Stamp: "00000000000", } } + +func NewGroupModel(userId *uuid.UUID) *model.Group { + return &model.Group{ + LeaderID: userId, + IsConfirmed: false, + } +}