-
Notifications
You must be signed in to change notification settings - Fork 3
Refactor/hooks #630
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Refactor/hooks #630
Changes from 22 commits
10c2049
c237f0b
01e21dc
7502bb2
21f6fd7
b44693c
c9f3b04
2990035
3e8582a
e7e0267
1fe4e8b
30e0e03
6f7de9b
c9834b8
ec9dcf1
904fe1f
3cea8dc
6f7bcc7
e621dc1
6c66f9f
7898f41
e84a208
fb911d6
db6e5cc
70f6c91
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -76,31 +76,104 @@ | |||||||||||||||||||
|
|
||||||||||||||||||||
| func createEvent(db *gorm.DB, args domain.UpsertEventArgs) (*Event, error) { | ||||||||||||||||||||
| event := ConvWriteEventParamsToEvent(args) | ||||||||||||||||||||
| var err error | ||||||||||||||||||||
| var r *Room | ||||||||||||||||||||
| r, err = getRoom(db, event.RoomID) | ||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||
| return nil, err | ||||||||||||||||||||
| } | ||||||||||||||||||||
| event.Room = *r | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // tagを生成する | ||||||||||||||||||||
| for i := range event.Tags { | ||||||||||||||||||||
| tag, err := createOrGetTag(db, event.Tags[i].Tag.Name) | ||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||
| return nil, err | ||||||||||||||||||||
| } | ||||||||||||||||||||
| event.Tags[i].EventID = event.ID | ||||||||||||||||||||
| event.Tags[i].TagID = tag.ID | ||||||||||||||||||||
| event.Tags[i].Tag = *tag | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| err := db.Create(&event).Error | ||||||||||||||||||||
| // 対応する Room, Group はService層で確認済み | ||||||||||||||||||||
| event.ID, err = uuid.NewV4() | ||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||
| return nil, err | ||||||||||||||||||||
| } | ||||||||||||||||||||
| err = db.Create(&event).Error | ||||||||||||||||||||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||
| return &event, err | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| func updateEvent(db *gorm.DB, eventID uuid.UUID, args domain.UpsertEventArgs) (*Event, error) { | ||||||||||||||||||||
| event := ConvWriteEventParamsToEvent(args) | ||||||||||||||||||||
| event.ID = eventID | ||||||||||||||||||||
| var err error | ||||||||||||||||||||
| var r *Room | ||||||||||||||||||||
| r, err = getRoom(db, event.RoomID) | ||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||
| return nil, err | ||||||||||||||||||||
| } | ||||||||||||||||||||
| event.Room = *r | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // tagは自動生成する | ||||||||||||||||||||
| for i := range event.Tags { | ||||||||||||||||||||
| tag, err := createOrGetTag(db, event.Tags[i].Tag.Name) | ||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||
| return nil, err | ||||||||||||||||||||
| } | ||||||||||||||||||||
| event.Tags[i].EventID = event.ID | ||||||||||||||||||||
| event.Tags[i].TagID = tag.ID | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // 対応する Room, Group はService層で確認済み | ||||||||||||||||||||
| err = db.Save(&event).Error | ||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||
| return nil, err | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| for _, admin := range event.Admins { | ||||||||||||||||||||
| err = db.Save(&admin).Error | ||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||
| return nil, err | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
| for _, attendee := range event.Attendees { | ||||||||||||||||||||
| err = db.Save(&attendee).Error | ||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||
| return nil, err | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
| for _, etag := range event.Tags { | ||||||||||||||||||||
| err = db.Save(&etag).Error | ||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||
| return nil, err | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
Nzt3-gh marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+132
to
+154
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 削除された Admin / Attendee / Tag が残り続けます。 今の更新処理は現在の配列を 🤖 Prompt for AI Agents |
||||||||||||||||||||
|
|
||||||||||||||||||||
| err := db.Session(&gorm.Session{FullSaveAssociations: true}). | ||||||||||||||||||||
| Omit("CreatedAt").Save(&event).Error | ||||||||||||||||||||
| return &event, err | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| func addEventTag(db *gorm.DB, eventID uuid.UUID, params domain.EventTagParams) error { | ||||||||||||||||||||
| eventTag := ConvdomainEventTagParamsToEventTag(params) | ||||||||||||||||||||
| eventTag.EventID = eventID | ||||||||||||||||||||
| err := db.Create(&eventTag).Error | ||||||||||||||||||||
| tag, err := createOrGetTag(db, eventTag.Tag.Name) | ||||||||||||||||||||
| eventTag.TagID = tag.ID | ||||||||||||||||||||
| err = db.Create(&eventTag).Error | ||||||||||||||||||||
|
Comment on lines
+162
to
+167
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. createOrGetTag のエラーが確認されていません Line 159 で 🔧 修正案 tag, err := createOrGetTag(db, eventTag.Tag.Name)
+ if err != nil {
+ return err
+ }
eventTag.TagID = tag.ID
err = db.Create(&eventTag).Error📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||
| if errors.Is(defaultErrorHandling(err), ErrDuplicateEntry) { | ||||||||||||||||||||
| return db.Omit("CreatedAt").Save(&eventTag).Error | ||||||||||||||||||||
| } | ||||||||||||||||||||
| return err | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| func deleteEvent(db *gorm.DB, eventID uuid.UUID) error { | ||||||||||||||||||||
| err := db.Where("event_id = ?", eventID).Delete(&EventTag{}).Error | ||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||
| return err | ||||||||||||||||||||
| } | ||||||||||||||||||||
| err = db.Where("event_id = ?", eventID).Delete(&EventAdmin{}).Error | ||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||
| return err | ||||||||||||||||||||
| } | ||||||||||||||||||||
| return db.Delete(&Event{ID: eventID}).Error | ||||||||||||||||||||
|
Comment on lines
+175
to
183
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 依存行削除と Event 本体削除も同一トランザクションにしてください。
🤖 Prompt for AI Agents |
||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -97,7 +97,13 @@ func convSPGroupToSuuidUUID(src []*Group) (dst []uuid.UUID) { | |
|
|
||
| func createGroup(db *gorm.DB, args domain.UpsertGroupArgs) (*Group, error) { | ||
| group := ConvWriteGroupParamsToGroup(args) | ||
| err := db.Create(&group).Error | ||
| // IDを新規発行 | ||
| var err error | ||
| group.ID, err = uuid.NewV4() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| err = db.Create(&group).Error | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
@@ -107,8 +113,41 @@ func createGroup(db *gorm.DB, args domain.UpsertGroupArgs) (*Group, error) { | |
| func updateGroup(db *gorm.DB, groupID uuid.UUID, args domain.UpsertGroupArgs) (*Group, error) { | ||
| group := ConvWriteGroupParamsToGroup(args) | ||
| group.ID = groupID | ||
| err := db.Session(&gorm.Session{FullSaveAssociations: true}). | ||
| Omit("CreatedAt").Save(&group).Error | ||
| // groupIDは存在する | ||
| var err error | ||
| // CreatedBy は readonly | ||
| // GroupMember, GroupAdmin も User は readonly | ||
|
|
||
| // err = db.Session(&gorm.Session{FullSaveAssociations: true}). | ||
| // Omit("CreatedAt").Save(&group).Error | ||
|
|
||
| // GroupMenberの削除 | ||
| err = db.Where("group_id = ?", group.ID).Delete(&GroupMember{}).Error | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| // GroupAdminの削除 | ||
| err = db.Where("group_id = ?", group.ID).Delete(&GroupAdmin{}).Error | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| err = db.Omit("CreatedAt").Save(&group).Error | ||
|
|
||
| // GroupMember の更新 | ||
| for _, member := range group.Members { | ||
| err = db.Save(&member).Error | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // GroupAdmin の更新 | ||
| for _, admin := range group.Admins { | ||
| err := db.Save(&admin).Error | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
| return &group, err | ||
|
Comment on lines
+145
to
181
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
ここは 🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
|
|
@@ -121,14 +160,25 @@ func addMemberToGroup(db *gorm.DB, groupID, userID uuid.UUID) error { | |
| onConflictClause := clause.OnConflict{ | ||
| DoUpdates: clause.AssignmentColumns([]string{"updated_at", "deleted_at"}), | ||
| } | ||
|
|
||
| // groupMemberはhook登録なし | ||
| return db.Clauses(onConflictClause).Create(&groupMember).Error | ||
| } | ||
|
|
||
| func deleteGroup(db *gorm.DB, groupID uuid.UUID) error { | ||
| group := Group{ | ||
| ID: groupID, | ||
| } | ||
| // Group の GroupMember を削除 | ||
| err := db.Where("group_id = ?", group.ID).Delete(&GroupMember{}).Error | ||
| if err != nil { | ||
| return err | ||
| } | ||
| // GroupAdmin を削除 | ||
| err = db.Where("group_id = ?", group.ID).Delete(&GroupAdmin{}).Error | ||
| if err != nil { | ||
| return err | ||
| } | ||
| // Group を削除 | ||
| return db.Delete(&group).Error | ||
|
Comment on lines
+201
to
212
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
migration/v8/model.go:106-131 では group 側に 🤖 Prompt for AI Agents |
||
| } | ||
|
Comment on lines
197
to
213
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. deleteGroup がトランザクションで囲まれていません
🤖 Prompt for AI Agents |
||
|
|
||
|
|
@@ -137,6 +187,7 @@ func deleteMemberOfGroup(db *gorm.DB, groupID, userID uuid.UUID) error { | |
| GroupID: groupID, | ||
| UserID: userID, | ||
| } | ||
| // 1メンバーの削除 hooksは登録なし | ||
| return db.Delete(&groupMember).Error | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nil返却が呼び出し側の panic 経路になっています。CreateEvent/UpdateEventはこの戻り値をerrチェック前にデリファレンスしています。ここでnil, errを返すと、getRoomやcreateOrGetTagの失敗がそのまま panic に化けます。呼び出し側を先にerr判定へ寄せるか、少なくともこの helper 群の「エラー時でも非nilを返す」契約は崩さない方が安全です。Also applies to: 94-95, 115-117, 123-125
🤖 Prompt for AI Agents