184 lines
5.0 KiB
Go
Executable File
184 lines
5.0 KiB
Go
Executable File
package config
|
|
|
|
import (
|
|
|
|
//mysql
|
|
//"gorm.io/driver/mysql"
|
|
//postgres
|
|
|
|
//sqlite
|
|
//"gorm.io/driver/sqlite"
|
|
//mssql
|
|
//"gorm.io/driver/sqlserver"
|
|
"os"
|
|
|
|
"github.com/joho/godotenv"
|
|
"gorm.io/driver/postgres"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
var DB *gorm.DB
|
|
|
|
func Init() {
|
|
|
|
//env
|
|
errenv := godotenv.Load()
|
|
if errenv != nil {
|
|
panic("Error loading .env file")
|
|
}
|
|
|
|
DB_USERNAME := os.Getenv("DB_USERNAME")
|
|
DB_PASSWORD := os.Getenv("DB_PASSWORD")
|
|
DB_HOST := os.Getenv("DB_HOST")
|
|
DB_PORT := os.Getenv("DB_PORT")
|
|
DB_DATABASE := os.Getenv("DB_DATABASE")
|
|
|
|
//mysql
|
|
//dsn := DB_USERNAME + ":" + DB_PASSWORD + "@tcp(" + DB_HOST + ":" + DB_PORT + ")/" + DB_DATABASE + "?charset=utf8mb4&parseTime=True&loc=Local"
|
|
//db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
|
|
//postgres
|
|
dsn := "host=" + DB_HOST + " user=" + DB_USERNAME + " password=" + DB_PASSWORD + " dbname=" + DB_DATABASE + " port=" + DB_PORT + " sslmode=disable TimeZone=Europe/Moscow"
|
|
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
|
//sqlite
|
|
//db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{})
|
|
//mssql
|
|
//dsn := "sqlserver://"+DB_USERNAME+":"+DB_PASSWORD+"@"+DB_HOST+":"+DB_PORT+"?database="+DB_DATABASE
|
|
//db, err := gorm.Open(sqlserver.Open(dsn), &gorm.Config{})
|
|
|
|
if err != nil {
|
|
panic("Failed to connect to database!")
|
|
}
|
|
|
|
// Migrations.
|
|
|
|
db.AutoMigrate(&ExerciseCourseModel{})
|
|
db.AutoMigrate(&UserExerciseCourseModel{})
|
|
db.AutoMigrate(&UserCourseModel{})
|
|
db.AutoMigrate(&UserModel{})
|
|
db.AutoMigrate(&CourseModel{})
|
|
db.AutoMigrate(&ExerciseModel{})
|
|
db.AutoMigrate(&CasbinRoleModel{})
|
|
db.AutoMigrate(&ResPassUserModel{})
|
|
db.AutoMigrate(&CasbinRoleConf{})
|
|
|
|
//End Migrations.
|
|
|
|
DB = db
|
|
}
|
|
|
|
// Run the migrations.
|
|
|
|
type UserModel struct {
|
|
gorm.Model
|
|
ID uint `gorm:"column:id;primary_key"`
|
|
Name string `gorm:"column:name;unique_index"`
|
|
Email string `gorm:"column:email;unique_index"`
|
|
Password string `gorm:"column:password;not null"`
|
|
Role string `gorm:"column:role"`
|
|
UserDoctorID uint `gorm:"column:user_doctor_id"`
|
|
ExerciseCourses []ExerciseCourseModel `gorm:"many2many:rh_users_exercise_course;foreignKey:id;References:id"`
|
|
Courses []CourseModel `gorm:"many2many:rh_user_course;foreignKey:id;References:id"`
|
|
}
|
|
|
|
func (UserModel) TableName() string {
|
|
return "rh_users"
|
|
}
|
|
|
|
type CourseModel struct {
|
|
gorm.Model
|
|
ID uint `gorm:"column:id;primary_key"`
|
|
Title string `gorm:"column:title"`
|
|
Desc string `gorm:"column:desc"`
|
|
TimeCourse string `gorm:"column:time_course"`
|
|
DayCourse int `gorm:"column:Day_course"`
|
|
URLFileImg string `gorm:"column:url_file_img"`
|
|
Users []UserModel `gorm:"many2many:rh_user_course;foreignKey:id;References:id"`
|
|
Exercises []ExerciseModel `gorm:"many2many:rh_exercise_course;foreignKey:id;References:id"`
|
|
}
|
|
|
|
func (CourseModel) TableName() string {
|
|
return "rh_course"
|
|
}
|
|
|
|
type ExerciseModel struct {
|
|
gorm.Model
|
|
ID uint `gorm:"column:id;primary_key"`
|
|
Title string `gorm:"column:title"`
|
|
Desc string `gorm:"column:desc"`
|
|
URLFile string `gorm:"column:url_file"`
|
|
URLFileImg string `gorm:"column:url_file_img"`
|
|
Courses []CourseModel `gorm:"many2many:rh_exercise_course;foreignKey:id;References:id"`
|
|
}
|
|
|
|
func (ExerciseModel) TableName() string {
|
|
return "rh_exercise"
|
|
}
|
|
|
|
type ExerciseCourseModel struct {
|
|
gorm.Model
|
|
Day int `gorm:"column:day"`
|
|
Position int `gorm:"column:position"`
|
|
Time string `gorm:"column:time"`
|
|
Users []UserModel `gorm:"many2many:rh_users_exercise_course;foreignKey:id;References:id"`
|
|
}
|
|
|
|
func (ExerciseCourseModel) TableName() string {
|
|
return "rh_exercise_course"
|
|
}
|
|
|
|
type UserCourseModel struct {
|
|
gorm.Model
|
|
Status int `gorm:"column:status"`
|
|
UserTimeCourse string `gorm:"column:user_time_course"`
|
|
}
|
|
|
|
func (UserCourseModel) TableName() string {
|
|
return "rh_user_course"
|
|
}
|
|
|
|
type UserExerciseCourseModel struct {
|
|
gorm.Model
|
|
TimeUsers string `gorm:"column:time_users"`
|
|
Status int `gorm:"column:status"`
|
|
}
|
|
|
|
func (UserExerciseCourseModel) TableName() string {
|
|
return "rh_users_exercise_course"
|
|
}
|
|
|
|
type CasbinRoleModel struct {
|
|
gorm.Model
|
|
ID uint `gorm:"column:id;primary_key"`
|
|
RoleName string `gorm:"column:v0"`
|
|
Path string `gorm:"column:v1"`
|
|
Method string `gorm:"column:v2"`
|
|
}
|
|
|
|
func (CasbinRoleModel) TableName() string {
|
|
return "rh_user_role"
|
|
}
|
|
|
|
type ResPassUserModel struct {
|
|
gorm.Model
|
|
ID uint `gorm:"column:id;primary_key"`
|
|
Email string `gorm:"column:email"`
|
|
Url string `gorm:"column:url"`
|
|
Url_full string `gorm:"column:url_full"`
|
|
}
|
|
|
|
func (ResPassUserModel) TableName() string {
|
|
return "rh_res_pass_users"
|
|
}
|
|
|
|
type CasbinRoleConf struct {
|
|
ID uint `gorm:"column:id;primary_key"`
|
|
Ptype string `gorm:"column:ptype"`
|
|
RoleName string `gorm:"column:v0"`
|
|
Path string `gorm:"column:v1"`
|
|
Method string `gorm:"column:v2"`
|
|
}
|
|
|
|
func (CasbinRoleConf) TableName() string {
|
|
return "casbin_rule"
|
|
}
|