From b63776274411853bd88309889bda65c73b13a6a2 Mon Sep 17 00:00:00 2001 From: ysandler Date: Wed, 19 Mar 2025 12:15:40 -0500 Subject: [PATCH] feat: seed data and fix db config --- .container_volume/scripts/01_users.sql | 34 ++++++++++++++++++-------- .env | 3 ++- docker-compose.yaml | 6 ++--- entities/User.go | 1 - repository/UserRepo.go | 6 ++--- 5 files changed, 32 insertions(+), 18 deletions(-) diff --git a/.container_volume/scripts/01_users.sql b/.container_volume/scripts/01_users.sql index 8c8be72..24e3470 100644 --- a/.container_volume/scripts/01_users.sql +++ b/.container_volume/scripts/01_users.sql @@ -1,13 +1,27 @@ --- I DO NOT KNOW IF THIS WORKS -- --- I AM PLAYING ARROUND WITH START UP SCRIPTS FOR THE CONTAINER -- +CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; --- Drop existing roles/users if they exist -DROP ROLE IF EXISTS admin; -DROP USER IF EXISTS admin; +DROP TABLE dev_users --- Create admin user with strong password using bcrypt -CREATE USER admin WITH PASSWORD 'password'; +CREATE TABLE IF NOT EXISTS dev_users ( + id UUID PRIMARY KEY DEFAULT (uuid_generate_v4()), + first_name character varying(255) NOT NULL, + last_name character varying(255) NOT NULL, + email character varying(255) UNIQUE NOT NULL, + phone_number character varying(255), + created_at timestamp with time zone DEFAULT now(), + updated_at timestamp with time zone DEFAULT now() +); --- Grant all privileges -ALTER ROLE admin SUPERUSER; -GRANT ALL PRIVILEGES TO admin; +-- Seed Data +INSERT INTO dev_users (first_name, last_name, email, phone_number, created_at, updated_at) +VALUES +('John', 'Doe', 'john.doe@example.com', '(555) 123-4567', now(), now()), +('Jane', 'Smith', 'jane.smith@example.com', '(555) 123-4568', now(), now()), +('Bob', 'Johnson', 'bob.johnson@example.com', '(555) 123-4569', now(), now()), +('Alice', 'Brown', 'alice.brown@example.com', '(555) 123-4570', now(), now()), +('Charlie', 'Wilson', 'charlie.wilson@example.com', '(555) 123-4571', now(), now()), +('Eva', 'Davis', 'eva.davis@example.com', '(555) 123-4572', now(), now()), +('Frank', 'Miller', 'frank.miller@example.com', '(555) 123-4573', now(), now()), +('Grace', 'Taylor', 'grace.taylor@example.com', '(555) 123-4574', now(), now()), +('Henry', 'Anderson', 'henry.anderson@example.com', '(555) 123-4575', now(), now()), +('Isabella', 'King', 'isabella.king@example.com', '(555) 123-4576', now(), now()); diff --git a/.env b/.env index 54e02d1..4ae28bd 100644 --- a/.env +++ b/.env @@ -1,9 +1,10 @@ DB_USER=admin DB_PASSWORD=password +DB_ROOT_PASSWORD=rootpassword DB_NAME=worklog DB_HOST=localhost DB_PORT=5432 -DB_EXPOSED_PORT=6767 +DB_EXPOSED_PORT=5432 DB_TZ=America/Chicago VOLUME_ROOT="./.container_volume" diff --git a/docker-compose.yaml b/docker-compose.yaml index e05046f..2705b24 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -5,9 +5,9 @@ services: ports: - "${DB_EXPOSED_PORT}:${DB_PORT}" environment: - POSTGRES_DB: worklog - POSTGRES_USER: admin - POSTGRES_PASSWORD: password + - POSTGRES_DB=worklog + - POSTGRES_USER=${DB_USER} + - POSTGRES_PASSWORD=${DB_PASSWORD} volumes: - "${VOLUME_ROOT}/db:/var/lib/postgresql/data" - "${VOLUME_ROOT}/scripts:/docker-entrypoint-initdb.d/" diff --git a/entities/User.go b/entities/User.go index 8426260..8fd9fbe 100644 --- a/entities/User.go +++ b/entities/User.go @@ -7,7 +7,6 @@ import ( ) type User struct { - gorm.Model ID uuid.UUID `gorm:"type:uuid;primaryKey;default:uuid_generate_v4()"` FirstName string `gorm:"size:255"` LastName string `gorm:"size:255"` diff --git a/repository/UserRepo.go b/repository/UserRepo.go index caa18c7..e3effbc 100644 --- a/repository/UserRepo.go +++ b/repository/UserRepo.go @@ -55,11 +55,11 @@ func (r *UserRepository) Delete(ctx context.Context, id uuid.UUID) error { 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 +func (r *UserRepository) List(ctx context.Context, offset int, limit int) ([]entities.DevUser, int64, error) { + var users []entities.DevUser // TODO: make sure to make this abstractable var totalUserCount int64 - if err := r.db.WithContext(ctx).Where("1 = 1").Count(&totalUserCount).Offset(offset).Limit(limit); err != nil { + if err := r.db.WithContext(ctx).Find(&users).Count(&totalUserCount).Offset(offset).Limit(limit); err != nil { return nil, 0, err.Error }