|
| 1 | +package challenges |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "gorm.io/gorm" |
| 7 | +) |
| 8 | + |
| 9 | +// Challenge to hold all platform challenges |
| 10 | +type Challenge struct { |
| 11 | + gorm.Model |
| 12 | + Title string |
| 13 | + Description string |
| 14 | + CategoryID uint |
| 15 | + Active bool |
| 16 | + Points int |
| 17 | + Bonus int |
| 18 | + BonusDecay int |
| 19 | + Flag string |
| 20 | + Hint string |
| 21 | + Penalty int |
| 22 | + EntID uint `gorm:"index"` |
| 23 | +} |
| 24 | + |
| 25 | +// Category to hold all challenge categories |
| 26 | +type Category struct { |
| 27 | + gorm.Model |
| 28 | + Name string `gorm:"index"` |
| 29 | + Description string |
| 30 | + Logo string |
| 31 | + EntID uint `gorm:"index"` |
| 32 | +} |
| 33 | + |
| 34 | +// ChallengeManager to handle all challenges of the platform |
| 35 | +type ChallengeManager struct { |
| 36 | + DB *gorm.DB |
| 37 | +} |
| 38 | + |
| 39 | +// CreateChallengeManager to initialize the challenges struct and its tables |
| 40 | +func CreateChallengeManager(backend *gorm.DB) (*ChallengeManager, error) { |
| 41 | + if backend == nil { |
| 42 | + return nil, fmt.Errorf("database connection cannot be nil") |
| 43 | + } |
| 44 | + c := &ChallengeManager{ |
| 45 | + DB: backend, |
| 46 | + } |
| 47 | + // table challenges |
| 48 | + if err := backend.AutoMigrate(&Challenge{}); err != nil { |
| 49 | + return nil, fmt.Errorf("Failed to AutoMigrate table (challenges): %w", err) |
| 50 | + } |
| 51 | + // table categories |
| 52 | + if err := backend.AutoMigrate(&Category{}); err != nil { |
| 53 | + return nil, fmt.Errorf("Failed to AutoMigrate table (categories): %w", err) |
| 54 | + } |
| 55 | + return c, nil |
| 56 | +} |
| 57 | + |
| 58 | +// Create challenge |
| 59 | +func (m *ChallengeManager) Create(challenge Challenge) error { |
| 60 | + if err := m.DB.Create(&challenge).Error; err != nil { |
| 61 | + return fmt.Errorf("Create Challenge %w", err) |
| 62 | + } |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +// Create category |
| 67 | +func (m *ChallengeManager) CreateCategory(category Category) error { |
| 68 | + if err := m.DB.Create(&category).Error; err != nil { |
| 69 | + return fmt.Errorf("Create Category: %w", err) |
| 70 | + } |
| 71 | + return nil |
| 72 | +} |
| 73 | + |
| 74 | +// GetByID to get a challenge by id and entity |
| 75 | +func (m *ChallengeManager) GetByID(id uint, entID uint) (Challenge, error) { |
| 76 | + var challenge Challenge |
| 77 | + if err := m.DB.Where("id = ? AND ent_id = ?", id, entID).First(&challenge).Error; err != nil { |
| 78 | + return Challenge{}, fmt.Errorf("Get Challenge by ID and Entity: %w", err) |
| 79 | + } |
| 80 | + return challenge, nil |
| 81 | +} |
| 82 | + |
| 83 | +// GetCategoryByID to get a category by id and entity id |
| 84 | +func (m *ChallengeManager) GetCategoryByID(id uint, entID uint) (Category, error) { |
| 85 | + var category Category |
| 86 | + if err := m.DB.Where("id = ? AND ent_id = ?", id, entID).First(&category).Error; err != nil { |
| 87 | + return Category{}, fmt.Errorf("Get Category by ID and Entity: %w", err) |
| 88 | + } |
| 89 | + return category, nil |
| 90 | +} |
| 91 | + |
| 92 | +// ExistCategory to check if a category exists name |
| 93 | +func (m *ChallengeManager) ExistCategory(name string, entID uint) bool { |
| 94 | + var count int64 |
| 95 | + if err := m.DB.Model(&Category{}).Where("name = ? AND ent_id = ?", name, entID).Count(&count).Error; err != nil { |
| 96 | + return false |
| 97 | + } |
| 98 | + return count > 0 |
| 99 | +} |
| 100 | + |
| 101 | +// New empty challenge |
| 102 | +func (m *ChallengeManager) New(title, description string, categoryID uint, active bool, points, bonus, bonusDecay, penalty int, flag, hint string, entID uint) Challenge { |
| 103 | + return Challenge{ |
| 104 | + Title: title, |
| 105 | + Description: description, |
| 106 | + CategoryID: categoryID, |
| 107 | + Active: active, |
| 108 | + Points: points, |
| 109 | + Bonus: bonus, |
| 110 | + BonusDecay: bonusDecay, |
| 111 | + Flag: flag, |
| 112 | + Hint: hint, |
| 113 | + Penalty: penalty, |
| 114 | + EntID: entID, |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// New empty category |
| 119 | +func (m *ChallengeManager) NewCategory(name, description, logo string, entID uint) (Category, error) { |
| 120 | + if !m.ExistCategory(name, entID) { |
| 121 | + return Category{ |
| 122 | + Name: name, |
| 123 | + Description: description, |
| 124 | + Logo: logo, |
| 125 | + EntID: entID, |
| 126 | + }, nil |
| 127 | + } |
| 128 | + return Category{}, fmt.Errorf("Category with name '%s' already exists", name) |
| 129 | +} |
0 commit comments