-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7d6b183
commit 50845af
Showing
2 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package model | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"gorm.io/gorm" | ||
) | ||
|
||
type Configuration struct { | ||
Id int64 `gorm:"primaryKey" json:"id,omitempty"` | ||
Config string `json:"config,omitempty"` | ||
Value int `json:"value,omitempty"` | ||
} | ||
|
||
func (Configuration) TableName() string { | ||
return "tconfig" | ||
} | ||
|
||
func (Configuration) DefaultFilter(db *gorm.DB) *gorm.DB { | ||
return db | ||
} | ||
|
||
func (c Configuration) List(db *gorm.DB, scopes ...func(*gorm.DB) *gorm.DB) (any, error) { | ||
var data []Configuration | ||
rs := db.Scopes(scopes...).Scopes(c.DefaultFilter).Find(&data) | ||
return data, rs.Error | ||
} | ||
|
||
func (Configuration) Get(db *gorm.DB, id int64) (any, error) { | ||
var data Configuration | ||
rs := db.First(&data, id) | ||
return data, rs.Error | ||
} | ||
|
||
func (obj Configuration) Update(db *gorm.DB, id int64, body []byte) (any, error) { | ||
model := Configuration{ | ||
Id: id, | ||
} | ||
|
||
var payload map[string]any | ||
err := json.Unmarshal(body, &payload) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rs := db.Model(&model).Save(payload) | ||
if rs.Error != nil { | ||
return nil, err | ||
} | ||
|
||
return obj.Get(db, id) | ||
} | ||
|
||
func (Configuration) Create(db *gorm.DB, body []byte) (any, error) { | ||
var payload Configuration | ||
err := json.Unmarshal(body, &payload) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rs := db.Create(&payload) | ||
if rs.Error != nil { | ||
return nil, err | ||
} | ||
|
||
return payload, nil | ||
} | ||
|
||
func (obj Configuration) Delete(db *gorm.DB, id int64) (any, error) { | ||
data, err := obj.Get(db, id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rs := db.Delete(&IgnoredName{}, id) | ||
if rs.Error != nil { | ||
return nil, rs.Error | ||
} | ||
|
||
return data, nil | ||
} |