diff --git a/.env b/.env new file mode 100644 index 0000000..6829ebc --- /dev/null +++ b/.env @@ -0,0 +1,8 @@ +DB_USER=admin +DB_PASSWORD=password +DB_NAME=worklog +DB_HOST=localhost +DB_PORT=5432 +DB_EXPOSED_PORT=6767 + +VOLUME_ROOT="./container_volume" diff --git a/db/connection.go b/db/connection.go new file mode 100644 index 0000000..1eacfd6 --- /dev/null +++ b/db/connection.go @@ -0,0 +1,47 @@ +package db + +import ( + "fmt" + "os" + + "gorm.io/driver/postgres" + "gorm.io/gorm" +) + +var DB *gorm.DB + +func getEnv(key string) string { + if value, ok := os.LookupEnv(key); ok { + return value + } + panic(fmt.Sprintf("Missing environment variable: %s", key)) +} + +func Init() *gorm.DB { + if DB != nil { + return DB + } + + connectionString := fmt.Sprintf("host=%s user=%s password=%s dbname=%s host=%s port=%s TimeZone=%s", + getEnv("DB_HOST"), + getEnv("DB_USER"), + getEnv("DB_PASSWORD"), + getEnv("DB_NAME"), + getEnv("DB_HOST"), + getEnv("DB_PORT"), + getEnv("DB_TZ")) + db, err := gorm.Open(postgres.Open(connectionString), &gorm.Config{}) + if err != nil { + panic(fmt.Errorf("failed to connect to database: %v", err)) + } + + sqlDB, err := db.DB() + if err != nil { + panic(err) + } + sqlDB.SetMaxOpenConns(10) + sqlDB.SetMaxIdleConns(5) + + DB = db + return DB +} diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..2e9bce8 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,22 @@ +version: '3' +services: + postgresdb: + image: postgres:latest + container_name: postgresdb + ports: + - "${DB_EXPOSED_PORT}:${DB_PORT}" + environment: + POSTGRES_DB: ${POSTGRES_DB} + POSTGRES_USER: ${POSTGRES_USER} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} + volumes: + - "${VOLUME_ROOT}/db:/var/lib/postgresql/data" + env_file: + - ./.env + depends_on: + - postgresdb + +networks: + postgres-network: + driver: bridge + diff --git a/entities/User.go b/entities/User.go index 5ebeaf5..ae7a88e 100644 --- a/entities/User.go +++ b/entities/User.go @@ -6,7 +6,7 @@ import ( "time" ) -type DevUser struct { +type User struct { gorm.Model ID uuid.UUID `gorm:"type:uuid;primaryKey;default:uuid_generate_v4()"` FirstName string @@ -17,17 +17,14 @@ type DevUser struct { UpdatedAt time.Time } +type DevUser struct { + User +} + type ClientUser struct { - gorm.Model - ID uuid.UUID `gorm:"type:uuid;primaryKey;default:uuid_generate_v4()"` - FirstName string - LastName string - Email string - PhoneNumber string + User ClientId uuid.UUID RoleInCompany string - CreatedAt time.Time - UpdatedAt time.Time } type ClientUser_Project_Join struct { diff --git a/go.mod b/go.mod index c119695..7693504 100644 --- a/go.mod +++ b/go.mod @@ -2,18 +2,25 @@ module worklog go 1.23.2 -require github.com/gofiber/fiber/v3 v3.0.0-beta.4 +require ( + github.com/gofiber/fiber/v3 v3.0.0-beta.4 + github.com/google/uuid v1.6.0 + gorm.io/gorm v1.25.12 +) require ( github.com/andybalholm/brotli v1.1.1 // indirect github.com/fxamacker/cbor/v2 v2.7.0 // indirect - github.com/gofiber/contrib v1.0.1 // indirect github.com/gofiber/schema v1.3.0 // indirect github.com/gofiber/utils/v2 v2.0.0-beta.7 // indirect - github.com/google/uuid v1.6.0 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect + github.com/jackc/pgx/v5 v5.5.5 // indirect + github.com/jackc/puddle/v2 v2.2.1 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/klauspost/compress v1.18.0 // indirect + github.com/lib/pq v1.10.9 // indirect github.com/mattn/go-colorable v0.1.14 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c // indirect @@ -23,7 +30,8 @@ require ( github.com/x448/float16 v0.8.4 // indirect golang.org/x/crypto v0.36.0 // indirect golang.org/x/net v0.37.0 // indirect + golang.org/x/sync v0.12.0 // indirect golang.org/x/sys v0.31.0 // indirect golang.org/x/text v0.23.0 // indirect - gorm.io/gorm v1.25.12 // indirect + gorm.io/driver/postgres v1.5.11 // indirect ) diff --git a/go.sum b/go.sum index 6fb7bc5..17635f2 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,10 @@ github.com/andybalholm/brotli v1.1.1 h1:PR2pgnyFznKEugtsUo0xLdDop5SKXd5Qf5ysW+7XdTA= github.com/andybalholm/brotli v1.1.1/go.mod h1:05ib4cKhjx3OQYUY22hTVd34Bc8upXjOLL2rKwwZBoA= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fxamacker/cbor/v2 v2.7.0 h1:iM5WgngdRBanHcxugY4JySA0nk1wZorNOpTgCMedv5E= github.com/fxamacker/cbor/v2 v2.7.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXEeXQVeNoDQ= -github.com/gofiber/contrib v1.0.1 h1:pQ8pQ2e8qBQ4koUGRZ4+wCSHUOip8FpjmPOhRTp+DlU= -github.com/gofiber/contrib v1.0.1/go.mod h1:e15MOdipEOlXrU5SUT5p0tfkZkhzDqfdiT2kfRYy1c0= github.com/gofiber/fiber/v3 v3.0.0-beta.4 h1:KzDSavvhG7m81NIsmnu5l3ZDbVS4feCidl4xlIfu6V0= github.com/gofiber/fiber/v3 v3.0.0-beta.4/go.mod h1:/WFUoHRkZEsGHyy2+fYcdqi109IVOFbVwxv1n1RU+kk= github.com/gofiber/schema v1.3.0 h1:K3F3wYzAY+aivfCCEHPufCthu5/13r/lzp1nuk6mr3Q= @@ -14,12 +13,22 @@ github.com/gofiber/utils/v2 v2.0.0-beta.7 h1:NnHFrRHvhrufPABdWajcKZejz9HnCWmT/as github.com/gofiber/utils/v2 v2.0.0-beta.7/go.mod h1:J/M03s+HMdZdvhAeyh76xT72IfVqBzuz/OJkrMa7cwU= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= +github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw= +github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A= +github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= +github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo= github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ= +github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= +github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE= github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= @@ -28,6 +37,9 @@ github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c h1:dAMKvw0MlJT1Gsh github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/tinylib/msgp v1.2.5 h1:WeQg1whrXRFiZusidTQqzETkRpGjFjcIhW6uqWH09po= @@ -44,12 +56,18 @@ golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34= golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc= golang.org/x/net v0.37.0 h1:1zLorHbz+LYj7MQlSf1+2tPIIgibq2eL5xkrGk6f+2c= golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8= +golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw= +golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik= golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY= golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/postgres v1.5.11 h1:ubBVAfbKEUld/twyKZ0IYn9rSQh448EdelLYk9Mv314= +gorm.io/driver/postgres v1.5.11/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI= gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8= gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ= diff --git a/main.go b/main.go index 40283b5..8c96f59 100644 --- a/main.go +++ b/main.go @@ -4,9 +4,17 @@ import ( "log" "github.com/gofiber/fiber/v3" + "worklog/db" + "worklog/repository" ) func main() { + if err := db.Init(); err != nil { + log.Fatal(err) + } + + repo := repository.GetUserRepo(db.DB) + app := fiber.New() app.Get("/", func(c fiber.Ctx) error { diff --git a/repository/UserRepo.go b/repository/UserRepo.go new file mode 100644 index 0000000..a44e683 --- /dev/null +++ b/repository/UserRepo.go @@ -0,0 +1,67 @@ +package repository + +import ( + "context" + + "github.com/google/uuid" + "gorm.io/gorm" + "worklog/entities" +) + +type UserRepository struct { + db *gorm.DB +} + +var userRepo *UserRepository + +func GetUserRepo(db *gorm.DB) *UserRepository { + if userRepo != nil { + return userRepo + } + + userRepo = &UserRepository{ + db: db, + } + + return userRepo +} + +type User interface { + GetByEmail(ctx context.Context, email string) (*entities.User, error) + Create(ctx context.Context, user entities.User) error + Update(ctx context.Context, user entities.User) error + Delete(ctx context.Context, id uuid.UUID) error + List(ctx context.Context, offset int, limit int) ([]entities.User, int64) +} + +func (r *UserRepository) Create(ctx context.Context, user *entities.User) error { + return r.db.WithContext(ctx).Create(user).Error +} + +func (r *UserRepository) GetByEmail(ctx context.Context, email string) (*entities.User, error) { + var user entities.User + if err := r.db.WithContext(ctx).Where("email = ?", email).First(&user).Error; err != nil { + return nil, err + } + return &user, nil +} + +func (r *UserRepository) Update(ctx context.Context, user *entities.User) error { + return r.db.WithContext(ctx).Save(user).Error +} + +func (r *UserRepository) Delete(ctx context.Context, id uuid.UUID) error { + user := entities.User{ID: id} + return r.db.WithContext(ctx).Delete(&user).Error +} + +func (r *UserRepository) List(ctx context.Context, offset int, limit int) ([]entities.User, int64) { + var users []entities.User + var totalUserCount int64 + + query := r.db.WithContext(ctx).Model(&users).Where("1 = 1") // All Users + query.Count(&totalUserCount) // Count of All Users + query.Offset(offset).Limit(limit).Find(&users) // Filtered Users + + return users, totalUserCount +}