From 692f8a29c9428ecd5bde5ffdffe0f6678ee7474e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Benouarets?= Date: Sat, 18 Oct 2025 21:02:31 +0200 Subject: [PATCH] feat: initial commit --- .gitignore | 1 + config/database.go | 34 ++++++++++++++++++++++++++++++ controllers/user.go | 1 + database/conn.go | 47 ++++++++++++++++++++++++++++++++++++++++++ main.go | 1 + models/organization.go | 24 +++++++++++++++++++++ models/quiz.go | 26 +++++++++++++++++++++++ models/user.go | 31 ++++++++++++++++++++++++++++ repositories/user.go | 1 + services/user.go | 1 + utils/env.go | 10 +++++++++ 11 files changed, 177 insertions(+) create mode 100644 .gitignore create mode 100644 config/database.go create mode 100644 controllers/user.go create mode 100644 database/conn.go create mode 100644 main.go create mode 100644 models/organization.go create mode 100644 models/quiz.go create mode 100644 models/user.go create mode 100644 repositories/user.go create mode 100644 services/user.go create mode 100644 utils/env.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f11b75 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ diff --git a/config/database.go b/config/database.go new file mode 100644 index 0000000..7b1d434 --- /dev/null +++ b/config/database.go @@ -0,0 +1,34 @@ +package config + +import "git.secnex.io/cluequest/go-sdk/utils" + +type DatabaseConfiguration struct { + Host string + Port string + User string + Pass string + DB string + SSL string +} + +func NewDatabaseConfiguration(host, port, user, password, name, ssl string) DatabaseConfiguration { + return DatabaseConfiguration{ + Host: host, + Port: port, + User: user, + Pass: password, + DB: name, + SSL: ssl, + } +} + +func NewDatabaseConfigurationFromEnv() DatabaseConfiguration { + return NewDatabaseConfiguration( + utils.GetEnv("DB_HOST", "localhost"), + utils.GetEnv("DB_PORT", "5432"), + utils.GetEnv("DB_USER", "postgres"), + utils.GetEnv("DB_PASS", ""), + utils.GetEnv("DB_NAME", "postgres"), + utils.GetEnv("DB_SSL", "disable"), + ) +} diff --git a/controllers/user.go b/controllers/user.go new file mode 100644 index 0000000..2d32936 --- /dev/null +++ b/controllers/user.go @@ -0,0 +1 @@ +package controllers diff --git a/database/conn.go b/database/conn.go new file mode 100644 index 0000000..d0d809b --- /dev/null +++ b/database/conn.go @@ -0,0 +1,47 @@ +package database + +import ( + "fmt" + + "git.secnex.io/cluequest/go-sdk/config" + "git.secnex.io/cluequest/go-sdk/models" + "gorm.io/driver/postgres" + "gorm.io/gorm" +) + +type Connection struct { + *gorm.DB +} + +var Conn Connection + +func NewConnection(cfg config.DatabaseConfiguration) error { + dsn := fmt.Sprintf( + "host=%s user=%s password=%s dbname=%s port=%s sslmode=%s", + cfg.Host, cfg.User, cfg.Pass, cfg.DB, cfg.Port, cfg.SSL, + ) + + db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) + if err != nil { + return err + } + + fmt.Println("✅ Connection to database established!") + Conn = Connection{DB: db} + + // AutoMigration() + err = Conn.AutoMigrate(&models.User{}, &models.Quiz{}, &models.Organization{}) + if err != nil { + return err + } + + return nil +} + +func NewConnectionFromEnv() error { + return NewConnection(config.NewDatabaseConfigurationFromEnv()) +} + +func AutoMigration() error { + return Conn.AutoMigrate(&models.User{}, &models.Quiz{}, &models.Organization{}) +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/main.go @@ -0,0 +1 @@ +package main diff --git a/models/organization.go b/models/organization.go new file mode 100644 index 0000000..6f6dc4c --- /dev/null +++ b/models/organization.go @@ -0,0 +1,24 @@ +package models + +import ( + "time" + + "github.com/google/uuid" + "gorm.io/gorm" +) + +type Organization struct { + ID uuid.UUID `gorm:"type:uuid;primaryKey;default:uuid_generate_v4()" json:"id"` + Name string `gorm:"unique;not null" json:"name"` + DisplayName string `gorm:"not null" json:"display_name"` + CreatedBy uuid.UUID `gorm:"type:uuid;" json:"created_by"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` + DeletedAt gorm.DeletedAt `gorm:"index" json:"-"` + + Creator User `gorm:"foreignKey:CreatedBy" json:"creator,omitempty"` +} + +func (Organization) TableName() string { + return "organizations" +} diff --git a/models/quiz.go b/models/quiz.go new file mode 100644 index 0000000..81d192e --- /dev/null +++ b/models/quiz.go @@ -0,0 +1,26 @@ +package models + +import ( + "time" + + "github.com/google/uuid" + "gorm.io/gorm" +) + +type Quiz struct { + ID uuid.UUID `gorm:"type:uuid;primaryKey;default:uuid_generate_v4()" json:"id"` + Name string `gorm:"not null" json:"name"` + Config interface{} `gorm:"type:jsonb" json:"config"` + CreatedBy uuid.UUID `gorm:"type:uuid;not null" json:"created_by"` + UpdatedBy uuid.UUID `gorm:"type:uuid" json:"updated_by"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` + DeletedAt gorm.DeletedAt `gorm:"index" json:"-"` + + Creator User `gorm:"foreignKey:CreatedBy;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"creator,omitempty"` + Updater User `gorm:"foreignKey:UpdatedBy;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"updater,omitempty"` +} + +func (Quiz) TableName() string { + return "quizzes" +} diff --git a/models/user.go b/models/user.go new file mode 100644 index 0000000..9fc9905 --- /dev/null +++ b/models/user.go @@ -0,0 +1,31 @@ +package models + +import ( + "time" + + "github.com/google/uuid" + "gorm.io/gorm" +) + +type User struct { + ID uuid.UUID `gorm:"type:uuid;primaryKey;default:uuid_generate_v4()" json:"id"` + FirstName string `gorm:"not null" json:"first_name"` + LastName string `gorm:"not null" json:"last_name"` + Username string `gorm:"unique;not null" json:"username"` + Email string `gorm:"unique;not null" json:"email"` + Password string `gorm:"unique;not null" json:"-"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` + DeletedAt gorm.DeletedAt `gorm:"index" json:"-"` + + Organizations []Organization `gorm:"many2many:user_organizations;" json:"organizations,omitempty"` + + CreatedOrganization []Organization `gorm:"foreignKey:CreatedBy" json:"created_organization,omitempty"` + + CreatedQuiz []Quiz `gorm:"foreignKey:CreatedBy" json:"created_quiz,omitempty"` + UpdatedQuiz []Quiz `gorm:"foreignKey:UpdatedBy" json:"updated_quiz,omitempty"` +} + +func (User) TableName() string { + return "users" +} diff --git a/repositories/user.go b/repositories/user.go new file mode 100644 index 0000000..3f43206 --- /dev/null +++ b/repositories/user.go @@ -0,0 +1 @@ +package repositories diff --git a/services/user.go b/services/user.go new file mode 100644 index 0000000..5e568ea --- /dev/null +++ b/services/user.go @@ -0,0 +1 @@ +package services diff --git a/utils/env.go b/utils/env.go new file mode 100644 index 0000000..6ed1931 --- /dev/null +++ b/utils/env.go @@ -0,0 +1,10 @@ +package utils + +import "os" + +func GetEnv(key, def string) string { + if val, ok := os.LookupEnv(key); ok { + return val + } + return def +}