From f37d1dc44cd0d598eaf39321f46e2145d90929d3 Mon Sep 17 00:00:00 2001 From: andyosyndoh Date: Sat, 6 Sep 2025 09:04:18 +0300 Subject: [PATCH 01/14] feat: add .gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d998d62 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +#ignore +.env From d9a789935c0c0d9da414d5456145d721eca8a4e7 Mon Sep 17 00:00:00 2001 From: andyosyndoh Date: Sat, 6 Sep 2025 09:05:45 +0300 Subject: [PATCH 02/14] feat: add database dependencies --- go.mod | 12 ++++++++++++ go.sum | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/go.mod b/go.mod index b823f9e..8a90f66 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,14 @@ require github.com/gofiber/fiber/v2 v2.52.9 require ( github.com/andybalholm/brotli v1.1.0 // indirect github.com/google/uuid v1.6.0 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + github.com/jackc/pgx/v5 v5.6.0 // indirect + github.com/jackc/puddle/v2 v2.2.2 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect github.com/klauspost/compress v1.17.9 // indirect + github.com/lib/pq v1.10.9 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-runewidth v0.0.16 // indirect @@ -17,5 +24,10 @@ require ( github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasthttp v1.51.0 // indirect github.com/valyala/tcplisten v1.0.0 // indirect + golang.org/x/crypto v0.31.0 // indirect + golang.org/x/sync v0.10.0 // indirect golang.org/x/sys v0.33.0 // indirect + golang.org/x/text v0.21.0 // indirect + gorm.io/driver/postgres v1.6.0 // indirect + gorm.io/gorm v1.30.3 // indirect ) diff --git a/go.sum b/go.sum index dc4d804..df9cedf 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,26 @@ github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gofiber/fiber/v2 v2.52.9 h1:YjKl5DOiyP3j0mO61u3NTmK7or8GzzWzCFzkboyP5cw= github.com/gofiber/fiber/v2 v2.52.9/go.mod h1:YEcBbO/FB+5M1IZNBP9FO3J9281zgPAreiI1oqg8nDw= 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-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.6.0 h1:SWJzexBzPL5jb0GEsrPMLIsi/3jOo7RHlzTjcAeDrPY= +github.com/jackc/pgx/v5 v5.6.0/go.mod h1:DNZ/vlrUnhWCoFGxHAG8U2ljioxukquj7utPDgtQdTw= +github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= +github.com/jackc/puddle/v2 v2.2.2/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.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= +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.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= @@ -13,15 +28,33 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +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/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA= github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g= github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= +golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U= +golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk= +golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= +golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw= golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug= +golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4= +golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= +golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= +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= +gorm.io/driver/postgres v1.6.0 h1:2dxzU8xJ+ivvqTRph34QX+WrRaJlmfyPqXmoGVjMBa4= +gorm.io/driver/postgres v1.6.0/go.mod h1:vUw0mrGgrTK+uPHEhAdV4sfFELrByKVGnaVRkXDhtWo= +gorm.io/gorm v1.30.3 h1:QiG8upl0Sg9ba2Zatfjy0fy4It2iNBL2/eMdvEkdXNs= +gorm.io/gorm v1.30.3/go.mod h1:8Z33v652h4//uMA76KjeDH8mJXPm1QNCYrMeatR0DOE= From 3c7cc2835c61426872025fdec94e3d1377c55fb9 Mon Sep 17 00:00:00 2001 From: andyosyndoh Date: Sat, 6 Sep 2025 09:06:10 +0300 Subject: [PATCH 03/14] feat: add database package --- backend/database/db.go | 73 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 backend/database/db.go diff --git a/backend/database/db.go b/backend/database/db.go new file mode 100644 index 0000000..07d82a2 --- /dev/null +++ b/backend/database/db.go @@ -0,0 +1,73 @@ +// database/db.go +package database + +import ( + "database/sql" + "fmt" + "log" + + "github.com/lib/pq" // PostgreSQL driver for checking specific errors + "gorm.io/driver/postgres" + "gorm.io/gorm" +) + +var DB *gorm.DB + +func ConnectDB() { + // --- Step 1: Connect to the default 'postgres' database to check if our DB exists --- + const ( + host = "localhost" + port = 5432 + user = "postgres" + password = "aaosindo" + dbname = "task_manager" + ) + + // DSN for the initial connection to the 'postgres' database + initialDSN := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=postgres sslmode=disable", host, port, user, password) + + // Open a standard SQL connection + sqlDB, err := sql.Open("postgres", initialDSN) + if err != nil { + log.Fatal("failed to connect to postgres database:", err) + } + defer sqlDB.Close() + + // Ping the database to ensure the connection is alive + err = sqlDB.Ping() + if err != nil { + log.Fatal("failed to ping postgres database:", err) + } + + // --- Step 2: Try to create the 'task_manager' database --- + _, err = sqlDB.Exec(fmt.Sprintf("CREATE DATABASE %s", dbname)) + if err != nil { + // If the error is that the database already exists, we can ignore it. + // The pq library is needed to inspect the specific PostgreSQL error code. + if pgErr, ok := err.(*pq.Error); ok && pgErr.Code == "42P04" { // 42P04 is for "duplicate_database" + fmt.Printf("Database '%s' already exists. Continuing.\n", dbname) + } else { + // A different error occurred + log.Fatal("failed to create database:", err) + } + } else { + fmt.Printf("Database '%s' created successfully.\n", dbname) + } + + // --- Step 3: Now, connect to the 'task_manager' database with GORM --- + dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname) + + DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{}) + if err != nil { + log.Fatal("failed to connect to task_manager database:", err) + } + + // The AutoMigrate function should be called in your main function or a setup function, + // after you've defined your models. For example: + // err = DB.AutoMigrate(&models.User{}, &models.Task{}) + // if err != nil { + // log.Fatal("failed to migrate database:", err) + // } + + fmt.Println("Connected to the database successfully.") +} \ No newline at end of file From d6ba64db3f2deabcb9bc25cbc69e688f3c24c9b7 Mon Sep 17 00:00:00 2001 From: andyosyndoh Date: Sat, 6 Sep 2025 09:06:18 +0300 Subject: [PATCH 04/14] feat: connect to database --- backend/cmd/main.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/backend/cmd/main.go b/backend/cmd/main.go index 044ee0d..da8ae51 100644 --- a/backend/cmd/main.go +++ b/backend/cmd/main.go @@ -3,18 +3,20 @@ package main import ( "fmt" "log" + "task/backend/database" - "github.com/gofiber/fiber/v2/middleware/cors" "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/cors" ) func main() { + database.ConnectDB() app := fiber.New() // Add CORS middleware app.Use(cors.New(cors.Config{ - AllowMethods: "GET,POST,PUT,DELETE,OPTIONS", - AllowHeaders: "Origin,Content-Type,Accept,Authorization", + AllowMethods: "GET,POST,PUT,DELETE,OPTIONS", + AllowHeaders: "Origin,Content-Type,Accept,Authorization", // AllowCredentials: true, })) From df99603b07d10c90225fd6ffc42bd7ea2d4f3d5a Mon Sep 17 00:00:00 2001 From: andyosyndoh Date: Sat, 6 Sep 2025 09:22:00 +0300 Subject: [PATCH 05/14] feat: add godotenv dependency --- go.mod | 11 +++++++---- go.sum | 11 +++++++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 8a90f66..b894bf6 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,13 @@ go 1.23.0 toolchain go1.24.7 -require github.com/gofiber/fiber/v2 v2.52.9 +require ( + github.com/gofiber/fiber/v2 v2.52.9 + github.com/joho/godotenv v1.5.1 + github.com/lib/pq v1.10.9 + gorm.io/driver/postgres v1.6.0 + gorm.io/gorm v1.30.3 +) require ( github.com/andybalholm/brotli v1.1.0 // indirect @@ -16,7 +22,6 @@ require ( github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/klauspost/compress v1.17.9 // indirect - github.com/lib/pq v1.10.9 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-runewidth v0.0.16 // indirect @@ -28,6 +33,4 @@ require ( golang.org/x/sync v0.10.0 // indirect golang.org/x/sys v0.33.0 // indirect golang.org/x/text v0.21.0 // indirect - gorm.io/driver/postgres v1.6.0 // indirect - gorm.io/gorm v1.30.3 // indirect ) diff --git a/go.sum b/go.sum index df9cedf..d54cb94 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,8 @@ github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= 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/gofiber/fiber/v2 v2.52.9 h1:YjKl5DOiyP3j0mO61u3NTmK7or8GzzWzCFzkboyP5cw= github.com/gofiber/fiber/v2 v2.52.9/go.mod h1:YEcBbO/FB+5M1IZNBP9FO3J9281zgPAreiI1oqg8nDw= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= @@ -17,6 +19,8 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD 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.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= @@ -28,12 +32,15 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +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/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= 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.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA= @@ -48,12 +55,12 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw= golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= -golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug= -golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4= golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= 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.6.0 h1:2dxzU8xJ+ivvqTRph34QX+WrRaJlmfyPqXmoGVjMBa4= gorm.io/driver/postgres v1.6.0/go.mod h1:vUw0mrGgrTK+uPHEhAdV4sfFELrByKVGnaVRkXDhtWo= gorm.io/gorm v1.30.3 h1:QiG8upl0Sg9ba2Zatfjy0fy4It2iNBL2/eMdvEkdXNs= From 9b88ccf425349e8295cff21707bc4eba43fa0041 Mon Sep 17 00:00:00 2001 From: andyosyndoh Date: Sat, 6 Sep 2025 09:22:05 +0300 Subject: [PATCH 06/14] refactor: load db credentials from .env --- backend/database/db.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/backend/database/db.go b/backend/database/db.go index 07d82a2..eef96c8 100644 --- a/backend/database/db.go +++ b/backend/database/db.go @@ -5,7 +5,9 @@ import ( "database/sql" "fmt" "log" + "os" + "github.com/joho/godotenv" "github.com/lib/pq" // PostgreSQL driver for checking specific errors "gorm.io/driver/postgres" "gorm.io/gorm" @@ -14,14 +16,18 @@ import ( var DB *gorm.DB func ConnectDB() { + err := godotenv.Load() + if err != nil { + log.Fatal("Error loading .env file") + } // --- Step 1: Connect to the default 'postgres' database to check if our DB exists --- const ( - host = "localhost" - port = 5432 - user = "postgres" - password = "aaosindo" - dbname = "task_manager" + host = "localhost" + port = 5432 + dbname = "task_manager" ) + user := os.Getenv("DB_USER") + password := os.Getenv("DB_PASSWORD") // DSN for the initial connection to the 'postgres' database initialDSN := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=postgres sslmode=disable", host, port, user, password) From 7ba543ab62611476eb402ba49a19a483bfc408d8 Mon Sep 17 00:00:00 2001 From: andyosyndoh Date: Sat, 6 Sep 2025 09:25:41 +0300 Subject: [PATCH 07/14] feat: add models package --- backend/models/user.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 backend/models/user.go diff --git a/backend/models/user.go b/backend/models/user.go new file mode 100644 index 0000000..c7f902f --- /dev/null +++ b/backend/models/user.go @@ -0,0 +1,30 @@ +package models + +import ( + "time" +) + +type User struct { + ID int `json:"id" db:"id"` + Username string `json:"username" db:"username"` + Email string `json:"email" db:"email"` + PasswordHash string `json:"-" db:"password_hash"` + CreatedAt time.Time `json:"created_at" db:"created_at"` + UpdatedAt time.Time `json:"updated_at" db:"updated_at"` +} + +type CreateUserRequest struct { + Username string `json:"username" validate:"required,min=3,max=50"` + Email string `json:"email" validate:"required,email"` + Password string `json:"password" validate:"required,min=6"` +} + +type LoginRequest struct { + Username string `json:"username" validate:"required"` + Password string `json:"password" validate:"required"` +} + +type AuthResponse struct { + Token string `json:"token"` + User User `json:"user"` +} \ No newline at end of file From 9371bdf342e2e23f3d3c32d912e0b53c9df7b0e0 Mon Sep 17 00:00:00 2001 From: andyosyndoh Date: Sat, 6 Sep 2025 09:47:39 +0300 Subject: [PATCH 08/14] refactor: remove user model --- backend/models/user.go | 30 ------------------------------ 1 file changed, 30 deletions(-) delete mode 100644 backend/models/user.go diff --git a/backend/models/user.go b/backend/models/user.go deleted file mode 100644 index c7f902f..0000000 --- a/backend/models/user.go +++ /dev/null @@ -1,30 +0,0 @@ -package models - -import ( - "time" -) - -type User struct { - ID int `json:"id" db:"id"` - Username string `json:"username" db:"username"` - Email string `json:"email" db:"email"` - PasswordHash string `json:"-" db:"password_hash"` - CreatedAt time.Time `json:"created_at" db:"created_at"` - UpdatedAt time.Time `json:"updated_at" db:"updated_at"` -} - -type CreateUserRequest struct { - Username string `json:"username" validate:"required,min=3,max=50"` - Email string `json:"email" validate:"required,email"` - Password string `json:"password" validate:"required,min=6"` -} - -type LoginRequest struct { - Username string `json:"username" validate:"required"` - Password string `json:"password" validate:"required"` -} - -type AuthResponse struct { - Token string `json:"token"` - User User `json:"user"` -} \ No newline at end of file From 13c2e62224223bce719d6a60ca33ddfec059f5ac Mon Sep 17 00:00:00 2001 From: andyosyndoh Date: Sat, 6 Sep 2025 09:47:45 +0300 Subject: [PATCH 09/14] feat: add task model --- backend/models/task.go | 50 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 backend/models/task.go diff --git a/backend/models/task.go b/backend/models/task.go new file mode 100644 index 0000000..0ee9ca9 --- /dev/null +++ b/backend/models/task.go @@ -0,0 +1,50 @@ +package models + +import ( + "database/sql/driver" + "time" +) + +type TaskStatus string + +const ( + TaskStatusPending TaskStatus = "pending" + TaskStatusInProgress TaskStatus = "in_progress" + TaskStatusCompleted TaskStatus = "completed" +) + + +type Task struct { + ID int `json:"id" db:"id"` + Title string `json:"title" db:"title"` + Description string `json:"description" db:"description"` + Status TaskStatus `json:"status" db:"status"` + CreatedAt time.Time `json:"created_at" db:"created_at"` + UpdatedAt time.Time `json:"updated_at" db:"updated_at"` +} + +type CreateTaskRequest struct { + Title string `json:"title" validate:"required,min=1,max=200"` + Description string `json:"description"` + Status TaskStatus `json:"status"` + DueDate *time.Time `json:"due_date"` +} + +type UpdateTaskRequest struct { + Title *string `json:"title,omitempty"` + Description *string `json:"description,omitempty"` + Status *TaskStatus `json:"status,omitempty"` + DueDate *time.Time `json:"due_date,omitempty"` +} + +type TasksResponse struct { + Tasks []Task `json:"tasks"` + Total int `json:"total"` + Page int `json:"page"` + Size int `json:"size"` +} + +// Implement driver.Valuer interface for TaskStatus +func (ts TaskStatus) Value() (driver.Value, error) { + return string(ts), nil +} From fcdd7c23aedc34f9e7b76a628224e771874bc955 Mon Sep 17 00:00:00 2001 From: andyosyndoh Date: Sat, 6 Sep 2025 09:50:41 +0300 Subject: [PATCH 10/14] feat: add database migration file --- backend/database/migrate.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 backend/database/migrate.go diff --git a/backend/database/migrate.go b/backend/database/migrate.go new file mode 100644 index 0000000..2a54585 --- /dev/null +++ b/backend/database/migrate.go @@ -0,0 +1,20 @@ +package database + +import ( + "log" + "task/backend/models" +) + +func RunMigrations() { + log.Println("Running database migrations...") + + err := DB.AutoMigrate( + &models.Task{}, + ) + + if err != nil { + log.Fatalf("Failed to run migrations: %v", err) + } + + log.Println("✅ Database migrations completed successfully") +} From bab0a3973660ce89a2a4d7ac7e20b664076364e3 Mon Sep 17 00:00:00 2001 From: andyosyndoh Date: Sat, 6 Sep 2025 09:50:47 +0300 Subject: [PATCH 11/14] feat: add database migration call --- backend/cmd/main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/cmd/main.go b/backend/cmd/main.go index da8ae51..b446328 100644 --- a/backend/cmd/main.go +++ b/backend/cmd/main.go @@ -11,6 +11,7 @@ import ( func main() { database.ConnectDB() + database.RunMigrations() app := fiber.New() // Add CORS middleware From 62230708618e3cc469aef580d1061c213a024a4d Mon Sep 17 00:00:00 2001 From: andyosyndoh Date: Sat, 6 Sep 2025 09:58:04 +0300 Subject: [PATCH 12/14] fix: fixed go version from 1.23.0 to 1.23 --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index b894bf6..1c73d09 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module task -go 1.23.0 +go 1.23 toolchain go1.24.7 From ec8fb616b8bfe51e524fdcd828e830f92bb88df5 Mon Sep 17 00:00:00 2001 From: andyosyndoh Date: Sat, 6 Sep 2025 10:00:57 +0300 Subject: [PATCH 13/14] fix: action issues --- go.mod | 2 -- 1 file changed, 2 deletions(-) diff --git a/go.mod b/go.mod index 1c73d09..c6208cd 100644 --- a/go.mod +++ b/go.mod @@ -2,8 +2,6 @@ module task go 1.23 -toolchain go1.24.7 - require ( github.com/gofiber/fiber/v2 v2.52.9 github.com/joho/godotenv v1.5.1 From 614101980ed56c07ee83f0eb6881b759c2cf2c81 Mon Sep 17 00:00:00 2001 From: andyosyndoh Date: Sat, 6 Sep 2025 10:03:30 +0300 Subject: [PATCH 14/14] fix: action issues --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index c6208cd..0a9969c 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module task -go 1.23 +go 1.23.0 require ( github.com/gofiber/fiber/v2 v2.52.9