Compare commits

...

2 Commits

Author SHA1 Message Date
ysandler
83545fb854 feat: connect to db and sample query 2025-03-17 17:55:17 -05:00
ysandler
815a3b2856 feat: setup inital db connection and User helper methods 2025-03-16 16:57:16 -05:00
9 changed files with 245 additions and 33 deletions

View File

@ -0,0 +1,13 @@
-- I DO NOT KNOW IF THIS WORKS --
-- I AM PLAYING ARROUND WITH START UP SCRIPTS FOR THE CONTAINER --
-- Drop existing roles/users if they exist
DROP ROLE IF EXISTS admin;
DROP USER IF EXISTS admin;
-- Create admin user with strong password using bcrypt
CREATE USER admin WITH PASSWORD 'password';
-- Grant all privileges
ALTER ROLE admin SUPERUSER;
GRANT ALL PRIVILEGES TO admin;

9
.env Normal file
View File

@ -0,0 +1,9 @@
DB_USER=admin
DB_PASSWORD=password
DB_NAME=worklog
DB_HOST=localhost
DB_PORT=5432
DB_EXPOSED_PORT=6767
DB_TZ=America/Chicago
VOLUME_ROOT="./.container_volume"

60
db/connection.go Normal file
View File

@ -0,0 +1,60 @@
package db
import (
"fmt"
"log"
"os"
"github.com/joho/godotenv"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
var DB *gorm.DB
func getEnv(key string) string {
log.Printf("Loading environment variables...")
err := godotenv.Load()
if err != nil {
log.Fatal(fmt.Sprintf("Error loading .env file: %v", err))
}
value := os.Getenv(key)
if value != "" {
return value
}
panic(fmt.Sprintf("Missing environment variable: %s", key))
}
func Init() (*gorm.DB, error) {
if DB != nil {
return DB, nil
}
log.Println("Initializing database connection...")
connectionString := fmt.Sprintf("user=%s password=%s dbname=%s host=%s port=%s TimeZone=%s",
getEnv("DB_USER"),
getEnv("DB_PASSWORD"),
getEnv("DB_NAME"),
getEnv("DB_HOST"),
getEnv("DB_EXPOSED_PORT"),
getEnv("DB_TZ"))
log.Printf("Database connection string: %s", connectionString)
db, err := gorm.Open(postgres.Open(connectionString), &gorm.Config{})
if err != nil {
return nil, err
}
sqlDB, err := db.DB()
if err != nil {
return nil, err
}
sqlDB.SetMaxOpenConns(10)
sqlDB.SetMaxIdleConns(5)
log.Println("Database connection established successfully")
return db, nil
}

20
docker-compose.yaml Normal file
View File

@ -0,0 +1,20 @@
services:
postgresdb:
image: postgres:latest
container_name: postgresdb
ports:
- "${DB_EXPOSED_PORT}:${DB_PORT}"
environment:
POSTGRES_DB: worklog
POSTGRES_USER: admin
POSTGRES_PASSWORD: password
volumes:
- "${VOLUME_ROOT}/db:/var/lib/postgresql/data"
- "${VOLUME_ROOT}/scripts:/docker-entrypoint-initdb.d/"
env_file:
- ./.env
networks:
postgres-network:
driver: bridge

View File

@ -6,48 +6,45 @@ import (
"time"
)
type DevUser struct {
type User struct {
gorm.Model
ID uuid.UUID `gorm:"type:uuid;primaryKey;default:uuid_generate_v4()"`
FirstName string
LastName string
Email string
PhoneNumber string
CreatedAt time.Time
UpdatedAt time.Time
FirstName string `gorm:"size:255"`
LastName string `gorm:"size:255"`
Email string `gorm:"unique;not null;size:255"`
PhoneNumber string `gorm:"size:255"`
CreatedAt time.Time `gorm:"default:now()"`
UpdatedAt time.Time `gorm:"default:now()"`
}
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 {
gorm.Model
ID uuid.UUID `gorm:"type:uuid;primaryKey;default:uuid_generate_v4()"`
ClientId uuid.UUID
ProjectId uuid.UUID
CreatedAt time.Time
UpdatedAt time.Time
IsStakeholder bool
Notes string
ClientId uuid.UUID `gorm:"foreignKey:ClientId;index:clientIdProjectIndex" `
ProjectId uuid.UUID `gorm:"foreignKey:ProjectId;index:projectIdClientIndex"`
CreatedAt time.Time `gorm:"default:now()"`
UpdatedAt time.Time `gorm:"default:now()"`
IsStakeholder bool `gorm:"size:1"`
Notes string `gorm:"type:text"`
}
type DevUser_Project_Join struct {
gorm.Model
ID uuid.UUID `gorm:"type:uuid;primaryKey;default:uuid_generate_v4()"`
ClientId uuid.UUID
ProjectId uuid.UUID
CreatedAt time.Time
UpdatedAt time.Time
IsLead bool
Notes string
ClientId uuid.UUID `gorm:"foreignKey:ClientId;index:clientIdDevIndex" `
ProjectId uuid.UUID `gorm:"foreignKey:ProjectId;index:projectIdDevIndex"`
CreatedAt time.Time `gorm:"default:now()"`
UpdatedAt time.Time `gorm:"default:now()"`
IsLead bool `gorm:"size:1"`
Notes string `gorm:"type:text"`
}

16
go.mod
View File

@ -2,15 +2,23 @@ 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
github.com/joho/godotenv v1.5.1
gorm.io/driver/postgres v1.5.11
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
@ -23,7 +31,7 @@ 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
)

22
go.sum
View File

@ -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,10 +13,20 @@ 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/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
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/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
@ -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=

20
main.go
View File

@ -1,12 +1,32 @@
package main
import (
"context"
"log"
"github.com/gofiber/fiber/v3"
"worklog/db"
"worklog/repository"
)
func main() {
DB, err := db.Init()
if err != nil {
log.Fatal(err)
}
repo := repository.GetUserRepo(DB)
ctx := context.Background()
users, totalUserCount, err := repo.List(ctx, 0, 10)
if err != nil {
log.Println(err)
}
log.Printf("Fetched %d users", len(users))
log.Printf("%d total users", totalUserCount)
app := fiber.New()
app.Get("/", func(c fiber.Ctx) error {

67
repository/UserRepo.go Normal file
View File

@ -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, error) {
var users []entities.User
var totalUserCount int64
if err := r.db.WithContext(ctx).Where("1 = 1").Count(&totalUserCount).Offset(offset).Limit(limit); err != nil {
return nil, 0, err.Error
}
return users, totalUserCount, nil
}