feat: initial commit
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
.idea/
|
||||||
34
config/database.go
Normal file
34
config/database.go
Normal file
@@ -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"),
|
||||||
|
)
|
||||||
|
}
|
||||||
1
controllers/user.go
Normal file
1
controllers/user.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package controllers
|
||||||
47
database/conn.go
Normal file
47
database/conn.go
Normal file
@@ -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{})
|
||||||
|
}
|
||||||
24
models/organization.go
Normal file
24
models/organization.go
Normal file
@@ -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"
|
||||||
|
}
|
||||||
26
models/quiz.go
Normal file
26
models/quiz.go
Normal file
@@ -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"
|
||||||
|
}
|
||||||
31
models/user.go
Normal file
31
models/user.go
Normal file
@@ -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"
|
||||||
|
}
|
||||||
1
repositories/user.go
Normal file
1
repositories/user.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package repositories
|
||||||
1
services/user.go
Normal file
1
services/user.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package services
|
||||||
10
utils/env.go
Normal file
10
utils/env.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user