Skip to content

Commit fb97c24

Browse files
committed
fix: clean up code with helper function
1 parent f2c2234 commit fb97c24

File tree

4 files changed

+18
-19
lines changed

4 files changed

+18
-19
lines changed

backend/src/database/DB.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,16 @@ func (db *DB) SeedDefaultData(isTesting bool) {
227227
}
228228
}
229229

230+
func (db *DB) softDeleteMap() map[string]any {
231+
updates := map[string]any{
232+
"deleted_at": time.Now(),
233+
}
234+
if userID, ok := db.Statement.Context.Value(models.UserIDKey).(uint); ok {
235+
updates["update_user_id"] = userID
236+
}
237+
return updates
238+
}
239+
230240
const (
231241
defaultLeftMenuLinks = `[{
232242
"title": "Unlocked Labs",

backend/src/database/facilities.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package database
33
import (
44
"UnlockEdv2/src/models"
55
"fmt"
6-
"time"
76

87
log "github.com/sirupsen/logrus"
98
"gorm.io/gorm"
@@ -54,12 +53,7 @@ func (db *DB) UpdateFacility(facility *models.Facility, id uint) error {
5453
}
5554

5655
func (db *DB) DeleteFacility(id int) error {
57-
updates := map[string]any{
58-
"deleted_at": time.Now(),
59-
}
60-
if userID, ok := db.Statement.Context.Value(models.UserIDKey).(uint); ok {
61-
updates["update_user_id"] = userID
62-
}
56+
updates := db.softDeleteMap()
6357
result := db.Model(&models.Facility{}).
6458
Where("id = ? AND deleted_at IS NULL", fmt.Sprintf("%d", id)).
6559
Updates(updates)

backend/src/database/users.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,7 @@ func (db *DB) CreateUser(user *models.User) error {
211211
}
212212

213213
func (db *DB) DeleteUser(id int) error {
214-
updates := map[string]any{
215-
"deleted_at": time.Now(),
216-
}
217-
if userID, ok := db.Statement.Context.Value(models.UserIDKey).(uint); ok {
218-
updates["update_user_id"] = userID
219-
}
214+
updates := db.softDeleteMap()
220215
result := db.Model(&models.User{}).
221216
Where("id = ? AND deleted_at IS NULL", id).
222217
Updates(updates)

backend/src/models/program.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,12 @@ var AllCreditTypes = []CreditType{
9090

9191
type Program struct {
9292
DatabaseFields
93-
Name string `json:"name" gorm:"not null;unique" validate:"required,max=255"`
94-
Description string `json:"description" gorm:"not null" validate:"required,max=255"`
95-
FundingType FundingType `json:"funding_type" gorm:"type:funding_type" validate:"required"`
96-
IsActive bool `json:"is_active" gorm:"not null"`
97-
IsFavorited bool `json:"is_favorited" gorm:"-"`
98-
ArchivedAt *time.Time `json:"archived_at"`
93+
Name string `json:"name" gorm:"not null;unique" validate:"required,max=255"`
94+
Description string `json:"description" gorm:"not null" validate:"required,max=255"`
95+
FundingType FundingType `json:"funding_type" gorm:"type:funding_type" validate:"required"`
96+
IsActive bool `json:"is_active" gorm:"not null"`
97+
IsFavorited bool `json:"is_favorited" gorm:"-"`
98+
ArchivedAt *time.Time `json:"archived_at"`
9999

100100
ProgramTypes []ProgramType `json:"program_types" gorm:"foreignKey:ProgramID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
101101
ProgramCreditTypes []ProgramCreditType `json:"credit_types" gorm:"foreignKey:ProgramID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`

0 commit comments

Comments
 (0)