change to GORM, add member and rank handlers

This commit is contained in:
2024-03-08 00:39:46 -08:00
parent 84424fdae9
commit 3b715cf331
36 changed files with 3491 additions and 230 deletions

View File

@@ -3,15 +3,43 @@ package db
import (
"database/sql"
"sync"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/spf13/viper"
"gopkg.in/guregu/null.v3"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
var ActiveDB *sql.DB
var ActiveDB *gorm.DB
var activeSQL *sql.DB
var lock = new(sync.Mutex)
func GetDB() (*sql.DB, error) {
type ObjectBase struct {
ID int `json:"id" gorm:"primarykey"`
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime; not null"`
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime; not null"`
Deleted bool `json:"deleted" gorm:"default:false; index; not null"`
DeletedAt null.Time `json:"deleted_at"`
}
func AutoMigrate() error {
db, err := GetDB()
if err != nil {
return err
}
err = db.AutoMigrate(
&Award{},
&Course{},
&Member{},
&Rank{},
&TrainingEvent{},
)
return err
}
func GetDB() (*gorm.DB, error) {
if ActiveDB != nil {
return ActiveDB, nil
@@ -26,17 +54,22 @@ func GetDB() (*sql.DB, error) {
return db, nil
}
func ConnectDB() (*sql.DB, error) {
func ConnectDB() (*gorm.DB, error) {
lock.Lock()
defer lock.Unlock()
if ActiveDB != nil {
ActiveDB.Close()
ActiveDB = nil
if activeSQL != nil {
activeSQL.Close()
activeSQL = nil
}
cfg := viper.GetViper()
db, err := sql.Open("mysql", cfg.GetString("MARIADB_CONNSTRING"))
db, err := gorm.Open(mysql.Open(cfg.GetString("MARIADB_CONNSTRING")), &gorm.Config{
FullSaveAssociations: true,
PrepareStmt: true,
})
activeSQL, err = db.DB()
if err != nil {
return nil, err
}