Skip to content

Commit

Permalink
fix: create group when create user
Browse files Browse the repository at this point in the history
  • Loading branch information
bookpanda committed Jul 6, 2024
1 parent a107a83 commit 9893a20
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 20 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
32 changes: 25 additions & 7 deletions internal/user/user.repository.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package user

import (
"github.com/google/uuid"
"github.com/isd-sgcu/rpkm67-model/model"
"gorm.io/gorm"
)

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 {
Expand All @@ -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
}
14 changes: 7 additions & 7 deletions internal/user/user.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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),
Expand Down
15 changes: 10 additions & 5 deletions internal/user/user.utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -38,10 +40,6 @@ func ModelToProto(in *model.User) *proto.User {
},
}

if in.GroupID != nil {
protoUser.GroupId = in.GroupID.String()
}

return protoUser
}

Expand Down Expand Up @@ -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,
Expand All @@ -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,
}
}

0 comments on commit 9893a20

Please sign in to comment.