From e65b85247c7d8becb83bfac8c83740314033416b Mon Sep 17 00:00:00 2001 From: YuMin Kim Date: Mon, 23 Sep 2024 15:51:38 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20Resolve=20nickname=20save?= =?UTF-8?q?=20issue=20in=20class=20signup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix bug where user nicknames were not being saved properly during class signup process. Ensure database row is created with correct nickname data. Related issue: #340 --- models/class_board.go | 2 +- models/class_code.go | 2 +- repositories/class_user_repository.go | 12 +++++++++--- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/models/class_board.go b/models/class_board.go index 068f1c1..83ed439 100644 --- a/models/class_board.go +++ b/models/class_board.go @@ -10,7 +10,7 @@ type ClassBoard struct { CreatedAt time.Time `gorm:"not null;"` UpdatedAt time.Time `gorm:"not null;"` IsAnnounced bool `gorm:"not null;default:false"` - CID uint `gorm:"column:cid;not null;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` + CID uint `gorm:"column:cid;not null;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` UID uint `gorm:"column:uid;not null"` // User ID Class Class `gorm:"foreignKey:CID;constraint:OnDelete:CASCADE"` User User `gorm:"foreignKey:UID"` diff --git a/models/class_code.go b/models/class_code.go index 123883b..27ab4e3 100644 --- a/models/class_code.go +++ b/models/class_code.go @@ -4,7 +4,7 @@ type ClassCode struct { ID uint `gorm:"primaryKey;size:255;autoIncrement"` Code string `gorm:"size:10;not null"` Secret *string `gorm:"size:20"` - CID uint `gorm:"column:cid;not null;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` + CID uint `gorm:"column:cid;not null;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` UID uint `gorm:"column:uid;not null"` // User ID Class Class `gorm:"foreignKey:CID;constraint:OnDelete:CASCADE"` User User `gorm:"foreignKey:UID"` diff --git a/repositories/class_user_repository.go b/repositories/class_user_repository.go index 47ee768..1477d13 100644 --- a/repositories/class_user_repository.go +++ b/repositories/class_user_repository.go @@ -220,10 +220,16 @@ func (r *classUserRepository) RoleExists(uid uint, cid uint) (bool, error) { } func (r *classUserRepository) CreateUserRole(uid uint, cid uint, role string) error { + var user models.User + if err := r.db.First(&user, uid).Error; err != nil { + return err + } + newUserRole := models.ClassUser{ - UID: uid, - CID: cid, - Role: role, + UID: uid, + CID: cid, + Role: role, + Nickname: user.Name, } return r.db.Create(&newUserRole).Error }