feat: login service and implement database storage
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
package database
|
||||
|
||||
import "go.uber.org/fx"
|
||||
|
||||
var Module = fx.Module("database", fx.Provide(
|
||||
NewDB,
|
||||
))
|
||||
@@ -0,0 +1,43 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
staffmodel "github.com/ardeman/project-legalgo-go/database/staff"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type DB struct {
|
||||
*gorm.DB
|
||||
}
|
||||
|
||||
func NewDB() (*DB, error) {
|
||||
var (
|
||||
host = os.Getenv("DB_HOST")
|
||||
user = os.Getenv("DB_USER")
|
||||
password = os.Getenv("DB_PASSWORD")
|
||||
dbName = os.Getenv("DB_NAME")
|
||||
dbPort = os.Getenv("DB_PORT")
|
||||
)
|
||||
|
||||
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%v sslmode=disable", host, user, password, dbName, dbPort)
|
||||
|
||||
if dsn == "" {
|
||||
return nil, fmt.Errorf("DATABASE_URL environment is not set")
|
||||
}
|
||||
|
||||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to connect to database: %w", err)
|
||||
}
|
||||
|
||||
return &DB{db}, nil
|
||||
}
|
||||
|
||||
func (db *DB) Migrate() error {
|
||||
// Auto Migrate the User model
|
||||
return db.AutoMigrate(&staffmodel.Staff{})
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package staffmodel
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Staff struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primaryKey" json:"id"`
|
||||
Email string `gorm:"unique,not null" json:"email"`
|
||||
Password string `gorm:"not null" json:"password"`
|
||||
}
|
||||
Reference in New Issue
Block a user