From 16fa142dc3848f987ec9f71ad714e1851d919fd9 Mon Sep 17 00:00:00 2001 From: sneha <55897319+sneha-afk@users.noreply.github.com> Date: Mon, 11 Mar 2024 23:03:39 -0700 Subject: [PATCH] cleanup: fix sqlx warning, all c.String -> c.JSON, debug logout, etc. --- README.md | 27 ++--- authentication/auth_handler.go | 9 ++ crud/db_tasks.go | 16 +-- flake.lock | 18 ++-- go.mod | 4 +- go.sum | 31 ------ gomod2nix.toml | 8 +- main.go | 11 +- router.go | 187 ++++++++++++++------------------- testdb | Bin 32768 -> 0 bytes 10 files changed, 136 insertions(+), 175 deletions(-) delete mode 100644 testdb diff --git a/README.md b/README.md index ddcbadd..429f26e 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,17 @@ -# The backend for slugquest +# The backend for SlugQuest -## Resources - -* [Go Website](https://go.dev/) - -## Commands +## Running the server +The server can be compiled into an executable and ran locally: ```bash -# Run the program -go run main.go +# Compile into an executable +go build -o server +./server ``` ## Nix -An easy way to develop and run - -Install from [here](https://nixos.org/download#nix-install-linux) +An easy way to develop and run. Install from [here](https://nixos.org/download#nix-install-linux). Set up flakes with: ```bash @@ -23,13 +19,18 @@ mkdir ~/.config/nix echo "experimental-features = nix-command flakes" > ~/.config/nix/nix.conf ``` -Commands: +### Commands: ```bash -# Get a development sheel +# Get a development shell nix develop + # Build the program nix build + # Run the program nix run ``` +## Resources + +* [Go Website](https://go.dev/) diff --git a/authentication/auth_handler.go b/authentication/auth_handler.go index 1995765..447e106 100644 --- a/authentication/auth_handler.go +++ b/authentication/auth_handler.go @@ -103,6 +103,15 @@ func LogoutHandler(c *gin.Context) { parameters.Add("client_id", os.Getenv("AUTH0_CLIENT_ID")) logoutUrl.RawQuery = parameters.Encode() + // Remove from session + session := sessions.Default(c) + session.Set("profile", nil) + session.Set("user_id", nil) + if err := session.Save(); err != nil { + c.String(http.StatusInternalServerError, err.Error()) + return + } + c.Redirect(http.StatusTemporaryRedirect, logoutUrl.String()) } diff --git a/crud/db_tasks.go b/crud/db_tasks.go index 0ac36b8..54d04ae 100644 --- a/crud/db_tasks.go +++ b/crud/db_tasks.go @@ -92,7 +92,7 @@ func GetUserTaskDateTime(uid string, startq time.Time, endq time.Time) ([]RecurT return utaskArr, err } defer prep.Close() - log.Println(startq) + rows, err := prep.Query(uid, startq, endq) if err != nil { log.Printf("GetUserTaskDateTime() #2: %v", err) @@ -102,19 +102,19 @@ func GetUserTaskDateTime(uid string, startq time.Time, endq time.Time) ([]RecurT for rows.Next() { var taskprev RecurTypeTask - log.Printf("nonrecurtaskfound") err := rows.Scan(&taskprev.TaskID, &taskprev.UserID, &taskprev.Category, &taskprev.TaskName, &taskprev.StartTime, &taskprev.EndTime, &taskprev.Status, &taskprev.IsRecurring, &taskprev.IsAllDay) if err != nil { log.Printf("GetUserTaskDateTime() #3: %v", err) rows.Close() return utaskArr, err } - fmt.Printf("%v", utaskArr) + taskprev.RecurrenceId = -1 utaskArr = append(utaskArr, taskprev) } prep.Close() rows.Close() + p2, err := DB.Preparex("SELECT c.TaskID, c.UserID, c.Category, c.TaskName, c.StartTime, c.EndTime, c.Status, c.IsRecurring, c.IsAllDay, l.timestamp, l.LogId FROM TaskTable c, RecurringLog l WHERE l.TaskID = c.TaskID AND c.UserID = ? AND l.timestamp > ? AND l.timestamp < ?;") if err != nil { log.Printf("GetUserTaskDateTime() #4: %v", err) @@ -131,9 +131,9 @@ func GetUserTaskDateTime(uid string, startq time.Time, endq time.Time) ([]RecurT rowrec.Close() return utaskArr, err } + taskprev.EndTime = reftime.Add(taskprev.EndTime.Sub(taskprev.StartTime)) taskprev.StartTime = reftime - fmt.Printf("%v", utaskArr) utaskArr = append(utaskArr, taskprev) } p2.Close() @@ -158,8 +158,8 @@ func CreateTask(task Task) (bool, int64, error) { log.Printf("CreateTask(): could not prepare statement %v", err) return false, -1, err } - log.Printf("%d", task.TeamID) defer stmt.Close() // Defer the closing of SQL statement to ensure it closes once the function completes + res, err := stmt.Exec(task.UserID, task.Category, task.TaskName, task.Description, task.StartTime, task.EndTime, task.Status, task.IsRecurring, task.IsAllDay, task.Difficulty, task.CronExpression, task.TeamID) if err != nil { log.Printf("CreateTask(): could not insert into table: %v", err) @@ -522,7 +522,7 @@ func FailTask(tid int, uid string) (bool, error) { points := CalculatePoints(task.Difficulty) _, err = DB.Exec("UPDATE UserTable SET Points = Points - ? WHERE UserID = ?", points, task.UserID) if err != nil { - fmt.Printf("PassTask(): could not update user's points: %v\n", err) + log.Printf("PassTask(): could not update user's points: %v\n", err) return false, err } } @@ -542,7 +542,7 @@ func FailRecurringTask(tid int, recurrenceID int, uid string) (bool, error) { } if !ok { - fmt.Println("FailRecurringTask(): Task not found") + log.Println("FailRecurringTask(): Task not found") return false, fmt.Errorf("task not found") } @@ -564,7 +564,7 @@ func FailRecurringTask(tid int, recurrenceID int, uid string) (bool, error) { `, "failed", recurrenceID) if err != nil { - fmt.Printf("FailRecurringTask(): could not update status: %v\n", err) + log.Printf("FailRecurringTask(): could not update status: %v\n", err) return false, err } diff --git a/flake.lock b/flake.lock index 2461ddb..a938a97 100644 --- a/flake.lock +++ b/flake.lock @@ -5,11 +5,11 @@ "systems": "systems" }, "locked": { - "lastModified": 1709126324, - "narHash": "sha256-q6EQdSeUZOG26WelxqkmR7kArjgWCdw5sfJVHPH/7j8=", + "lastModified": 1710146030, + "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", "owner": "numtide", "repo": "flake-utils", - "rev": "d465f4819400de7c8d874d50b982301f28a84605", + "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", "type": "github" }, "original": { @@ -28,11 +28,11 @@ ] }, "locked": { - "lastModified": 1705314449, - "narHash": "sha256-yfQQ67dLejP0FLK76LKHbkzcQqNIrux6MFe32MMFGNQ=", + "lastModified": 1710154385, + "narHash": "sha256-4c3zQ2YY4BZOufaBJB4v9VBBeN2dH7iVdoJw8SDNCfI=", "owner": "nix-community", "repo": "gomod2nix", - "rev": "30e3c3a9ec4ac8453282ca7f67fca9e1da12c3e6", + "rev": "872b63ddd28f318489c929d25f1f0a3c6039c971", "type": "github" }, "original": { @@ -43,11 +43,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1709479366, - "narHash": "sha256-n6F0n8UV6lnTZbYPl1A9q1BS0p4hduAv1mGAP17CVd0=", + "lastModified": 1709961763, + "narHash": "sha256-6H95HGJHhEZtyYA3rIQpvamMKAGoa8Yh2rFV29QnuGw=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "b8697e57f10292a6165a20f03d2f42920dfaf973", + "rev": "3030f185ba6a4bf4f18b87f345f104e6a6961f34", "type": "github" }, "original": { diff --git a/go.mod b/go.mod index 785f133..d4333bf 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75 github.com/jmoiron/sqlx v1.3.5 github.com/joho/godotenv v1.5.1 - github.com/mattn/go-sqlite3 v2.0.3+incompatible + github.com/mattn/go-sqlite3 v1.14.22 golang.org/x/oauth2 v0.18.0 ) @@ -23,7 +23,7 @@ require ( github.com/coreos/go-oidc/v3 v3.9.0 github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/gin-contrib/cors v1.6.0 - github.com/gin-contrib/sessions v0.0.5 + github.com/gin-contrib/sessions v0.0.4 github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-jose/go-jose/v3 v3.0.2 // indirect github.com/go-playground/locales v0.14.1 // indirect diff --git a/go.sum b/go.sum index d27c62f..ba2df7a 100644 --- a/go.sum +++ b/go.sum @@ -4,8 +4,6 @@ github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b/go.mod h1:H0wQ github.com/bradleypeabody/gorilla-sessions-memcache v0.0.0-20181103040241-659414f458e1/go.mod h1:dkChI7Tbtx7H1Tj7TqGSZMOeGpMP5gLHtjroHd4agiI= github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM= -github.com/bytedance/sonic v1.10.2 h1:GQebETVBxYB7JGWJtLBi07OVzWwt+8dWA00gEVW2ZFE= -github.com/bytedance/sonic v1.10.2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4= github.com/bytedance/sonic v1.11.2 h1:ywfwo0a/3j9HR8wsYGWsIWl2mvRsI950HyoxiBERw5A= github.com/bytedance/sonic v1.11.2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4= github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= @@ -23,22 +21,16 @@ 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/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= -github.com/gin-contrib/cors v1.5.0 h1:DgGKV7DDoOn36DFkNtbHrjoRiT5ExCe+PC9/xp7aKvk= -github.com/gin-contrib/cors v1.5.0/go.mod h1:TvU7MAZ3EwrPLI2ztzTt3tqgvBCq+wn8WpZmfADjupI= github.com/gin-contrib/cors v1.6.0 h1:0Z7D/bVhE6ja07lI8CTjTonp6SB07o8bNuFyRbsBUQg= github.com/gin-contrib/cors v1.6.0/go.mod h1:cI+h6iOAyxKRtUtC6iF/Si1KSFvGm/gK+kshxlCi8ro= github.com/gin-contrib/sessions v0.0.4 h1:gq4fNa1Zmp564iHP5G6EBuktilEos8VKhe2sza1KMgo= github.com/gin-contrib/sessions v0.0.4/go.mod h1:pQ3sIyviBBGcxgyR8mkeJuXbeV3h3NYmhJADQTq5+Vo= -github.com/gin-contrib/sessions v0.0.5 h1:CATtfHmLMQrMNpJRgzjWXD7worTh7g7ritsQfmF+0jE= -github.com/gin-contrib/sessions v0.0.5/go.mod h1:vYAuaUPqie3WUSsft6HUlCjlwwoJQs97miaG2+7neKY= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.7.4/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY= github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q= -github.com/go-jose/go-jose/v3 v3.0.1 h1:pWmKFVtt+Jl0vBZTIpz/eAKwsm6LkIxDVVbFHKkchhA= -github.com/go-jose/go-jose/v3 v3.0.1/go.mod h1:RNkWWRld676jZEYoV3+XK8L2ZnNSvIsxFMht0mSX+u8= github.com/go-jose/go-jose/v3 v3.0.2 h1:2Edjn8Nrb44UvTdp84KU0bBPs1cO7noRCybtS3eJEUQ= github.com/go-jose/go-jose/v3 v3.0.2/go.mod h1:5b+7YgP7ZICgJDBdfjZaIt+H/9L9T/YQrVfLAMboGkQ= github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= @@ -51,8 +43,6 @@ github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= -github.com/go-playground/validator/v10 v10.17.0 h1:SmVVlfAOtlZncTxRuinDPomC2DkXJ4E5T9gDA0AIH74= -github.com/go-playground/validator/v10 v10.17.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= github.com/go-playground/validator/v10 v10.19.0 h1:ol+5Fu+cSq9JD7SoSqe04GMI92cbn0+wvQ3bZ8b/AU4= github.com/go-playground/validator/v10 v10.19.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= @@ -62,12 +52,9 @@ github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MG github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= -github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= -github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4= -github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= @@ -96,8 +83,6 @@ github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnr github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/kidstuff/mongostore v0.0.0-20181113001930-e650cd85ee4b/go.mod h1:g2nVr8KZVXJSS97Jo8pJ0jgq29P6H7dG0oplUA86MQw= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= -github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc= -github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM= github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= @@ -106,8 +91,6 @@ github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NB github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= -github.com/leodido/go-urn v1.3.0 h1:jX8FDLfW4ThVXctBNZ+3cIWnCSnrACDV73r76dy0aQQ= -github.com/leodido/go-urn v1.3.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= @@ -119,8 +102,6 @@ github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= -github.com/mattn/go-sqlite3 v2.0.3+incompatible h1:gXHsfypPkaMZrKbD5209QV9jbUTJKjyR5WD3HYQSd+U= -github.com/mattn/go-sqlite3 v2.0.3+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/memcachier/mc v2.0.1+incompatible/go.mod h1:7bkvFE61leUBvXz+yxsOnGBQSZpBSPIMUQSmmSHvuXc= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= @@ -141,7 +122,6 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= @@ -159,11 +139,8 @@ golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUu golang.org/x/arch v0.7.0 h1:pskyeJh/3AmoQ8CPE95vxHLqp1G1GfGNXTmcl9NEKTc= golang.org/x/arch v0.7.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc= -golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg= golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= @@ -175,12 +152,8 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= -golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo= -golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY= golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc= golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= -golang.org/x/oauth2 v0.16.0 h1:aDkGMBSYxElaoP81NpoUoz2oo2R2wHdZpGToUxfyQrQ= -golang.org/x/oauth2 v0.16.0/go.mod h1:hqZ+0LWXsiVoZpeld6jVt06P3adbS2Uu911W1SsJv2o= golang.org/x/oauth2 v0.18.0 h1:09qnuIAgzdx1XplqJvW6CQqMCtGZykZWcXzPMPUusvI= golang.org/x/oauth2 v0.18.0/go.mod h1:Wf7knwG0MPoWIMMBgFlEaSUDaKskp0dCfrlJRJXbBi8= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -196,8 +169,6 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= -golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= @@ -225,8 +196,6 @@ google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAs google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= -google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/gomod2nix.toml b/gomod2nix.toml index e6897d4..0e9bf35 100644 --- a/gomod2nix.toml +++ b/gomod2nix.toml @@ -20,8 +20,8 @@ schema = 3 version = "v1.6.0" hash = "sha256-50DR7bbPxvATv0Y3fxT/WplLE0AYOAbOVmuKsPU6rPo=" [mod."github.com/gin-contrib/sessions"] - version = "v0.0.5" - hash = "sha256-pqw/taGJ+E0wkeKC8zcg/8rxP+ihMJJoW42X+O6IDOk=" + version = "v0.0.4" + hash = "sha256-qe84JKt4QYGR6O8/aOK5ejMFRvXc7aQhklKuWe/ptA4=" [mod."github.com/gin-contrib/sse"] version = "v0.1.0" hash = "sha256-zYbMTao+1F+385Lvsba9roLmmt9eYqr57sUWo0LCVhw=" @@ -83,8 +83,8 @@ schema = 3 version = "v0.0.20" hash = "sha256-qhw9hWtU5wnyFyuMbKx+7RB8ckQaFQ8D+8GKPkN3HHQ=" [mod."github.com/mattn/go-sqlite3"] - version = "v2.0.3+incompatible" - hash = "sha256-+zdGqThUb7if9JAiO+6s6KXBeuT322JhfcityvdrxD4=" + version = "v1.14.22" + hash = "sha256-CWF2Hjg43658NhaePWbGzS19gHJXjuTroG5c0W3hgYQ=" [mod."github.com/modern-go/concurrent"] version = "v0.0.0-20180306012644-bacd9c7ef1dd" hash = "sha256-OTySieAgPWR4oJnlohaFTeK1tRaVp/b0d1rYY8xKMzo=" diff --git a/main.go b/main.go index 3b7a7b9..9b9b695 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "log" + "os" "time" envfuncs "github.com/joho/godotenv" @@ -78,8 +79,14 @@ func main() { log.Println("Could not add default team") } - log.Print("Running at http://localhost:8080") - router_err := router.Run() // listen and serve on 0.0.0.0:8080 + // Get port to run server on, or default to 8080 + port := os.Getenv("PORT") + if port == "" { + port = "8080" + } + + log.Print("Running at http://localhost:" + port) + router_err := router.Run(":" + port) if router_err != nil { log.Fatalf("main(): couldn't run server gg: %v", router_err) } diff --git a/router.go b/router.go index 55f4126..ff6ec7c 100644 --- a/router.go +++ b/router.go @@ -90,7 +90,8 @@ func getUserId(c *gin.Context) (string, error) { session := sessions.Default(c) userProfile, ok := session.Get("user_profile").(crud.User) if !ok { - return "", fmt.Errorf("couldn't get user id") + log.Printf("getUserId(): error retrieving user id") + return "", fmt.Errorf("error retrieving user id") } uid := userProfile.UserID @@ -98,10 +99,10 @@ func getUserId(c *gin.Context) (string, error) { } func getTeamTask(c *gin.Context) { - tid, err1 := strconv.Atoi(c.Param("id")) - if err1 != nil { + tid, err := strconv.Atoi(c.Param("id")) + if err != nil { log.Println("getTeamTaskId(): str2int error") - c.JSON(http.StatusBadRequest, gin.H{"error": "This is really bad"}) + c.JSON(http.StatusInternalServerError, gin.H{"error": "This is really bad"}) return } @@ -112,18 +113,18 @@ func getTeamTask(c *gin.Context) { task, err := crud.GetTeamTask(tid) if err != nil { - log.Println("getTaskById(): Problem in team tasks, probably DB related") + c.JSON(http.StatusInternalServerError, gin.H{"error": "could not retrieve team tasks"}) + return } c.JSON(http.StatusOK, gin.H{"tasks": task}) } func addUserTeam(c *gin.Context) { - log.Println("getting team") - tid, err1 := strconv.Atoi(c.Param("id")) + tid, err := strconv.Atoi(c.Param("id")) code := c.Param("code") - if err1 != nil { + if err != nil { log.Println("getTeamTaskId(): str2int error") - c.JSON(http.StatusBadRequest, gin.H{"error": "This is really bad"}) + c.JSON(http.StatusInternalServerError, gin.H{"error": "This is really bad"}) return } @@ -135,36 +136,32 @@ func addUserTeam(c *gin.Context) { succ, err := crud.AddUserToTeam(int64(tid), code) if succ && err == nil { c.JSON(http.StatusOK, gin.H{"message": "Success"}) - return } else { - c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create cadd memebr"}) - return + c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to add user to team"}) } - } func getUserTeams(c *gin.Context) { uid, err := getUserId(c) if err != nil { - log.Println(err) - c.JSON(http.StatusInternalServerError, gin.H{"error": "failed teams get"}) + c.JSON(http.StatusInternalServerError, gin.H{"error": "error authenticating user"}) return } + ret, err := crud.GetUserTeams(uid) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "failed teams get"}) return } - log.Println(ret) c.JSON(http.StatusOK, gin.H{"teams": ret}) } func getTeamUsers(c *gin.Context) { - tid, err1 := strconv.Atoi(c.Param("id")) - if err1 != nil { - log.Println("getteamusers): str2int error", err1) - c.JSON(http.StatusBadRequest, gin.H{"error": "This is really bad"}) + tid, err := strconv.Atoi(c.Param("id")) + if err != nil { + log.Println("getTeamUsers(): str2int error", err) + c.JSON(http.StatusInternalServerError, gin.H{"error": "This is really bad"}) return } @@ -182,13 +179,11 @@ func getTeamUsers(c *gin.Context) { } func deleteTeamUser(c *gin.Context) { - tid, err1 := strconv.Atoi(c.Param("tid")) + tid, err := strconv.Atoi(c.Param("tid")) code := c.Param("code") - log.Println(tid) - log.Println(code) - if err1 != nil { - log.Println("getRrs): str2int error", err1) - c.JSON(http.StatusBadRequest, gin.H{"error": "This is really bad"}) + if err != nil { + log.Println("deleteTeamUser(): str2int error", err) + c.JSON(http.StatusInternalServerError, gin.H{"error": "This is really bad"}) return } @@ -199,17 +194,17 @@ func deleteTeamUser(c *gin.Context) { ret, err := crud.RemoveUserFromTeam(int64(tid), code) if !ret || err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "This is really bad"}) + c.JSON(http.StatusInternalServerError, gin.H{"error": "error removing user from team"}) return } c.JSON(http.StatusOK, gin.H{"message": "Success"}) } func deleteTeam(c *gin.Context) { - tid, err1 := strconv.Atoi(c.Param("tid")) - if err1 != nil { - log.Println("delmusers): str2int error") - c.JSON(http.StatusBadRequest, gin.H{"error": "This is really bad"}) + tid, err := strconv.Atoi(c.Param("tid")) + if err != nil { + log.Println("deleteTeam(): str2int error") + c.JSON(http.StatusInternalServerError, gin.H{"error": "This is really bad"}) return } @@ -220,20 +215,23 @@ func deleteTeam(c *gin.Context) { ret, err := crud.DeleteTeam(int64(tid)) if !ret || err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "This is really bad"}) + c.JSON(http.StatusInternalServerError, gin.H{"error": "error deleting team"}) return } c.JSON(http.StatusOK, gin.H{"message": "Success"}) } func createTeam(c *gin.Context) { + uid, err := getUserId(c) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "error authenticating user"}) + return + } + name := c.Param("name") - session := sessions.Default(c) - userProfile, _ := session.Get("user_profile").(crud.User) - uid := userProfile.UserID ret, val, err := crud.CreateTeam(name, uid) if !ret || err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "This is really bad"}) + c.JSON(http.StatusInternalServerError, gin.H{"error": "could not create team"}) return } @@ -243,7 +241,7 @@ func createTeam(c *gin.Context) { func passRecurringTask(c *gin.Context) { uid, err := getUserId(c) if err != nil { - c.String(http.StatusInternalServerError, "Failure to retrieve user id") + c.JSON(http.StatusInternalServerError, gin.H{"error": "error authenticating user"}) return } @@ -266,7 +264,6 @@ func passRecurringTask(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": "Success", "bossId": bossId}) return } else { - log.Printf("passRecurringTask(): %v", err) c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to pass recurring task"}) return } @@ -275,7 +272,7 @@ func passRecurringTask(c *gin.Context) { func failRecurringTask(c *gin.Context) { uid, err := getUserId(c) if err != nil { - c.String(http.StatusInternalServerError, "Failure to retrieve user id") + c.JSON(http.StatusInternalServerError, gin.H{"error": "error authenticating user"}) return } @@ -298,7 +295,6 @@ func failRecurringTask(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": "Success"}) return } else { - log.Printf("failRecurringTask(): %v", err) c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fail recurring task"}) return } @@ -328,13 +324,13 @@ func getBossById(c *gin.Context) { func getCurrBossHealth(c *gin.Context) { uid, err := getUserId(c) if err != nil { - c.String(http.StatusInternalServerError, "Failure to retrieve user id") + c.JSON(http.StatusInternalServerError, gin.H{"error": "error authenticating user"}) return } currBossHealth, err := crud.GetCurrBossHealth(uid) if err != nil { - c.String(http.StatusInternalServerError, "Can't find boss health") + c.JSON(http.StatusInternalServerError, gin.H{"error": "error getting current boss health"}) return } c.JSON(http.StatusOK, gin.H{"curr_boss_health": currBossHealth}) @@ -343,7 +339,7 @@ func getCurrBossHealth(c *gin.Context) { func getCategory(c *gin.Context) { uid, err := getUserId(c) if err != nil { - c.String(http.StatusInternalServerError, "Failure to retrieve user id") + c.JSON(http.StatusInternalServerError, gin.H{"error": "error authenticating user"}) return } @@ -366,6 +362,7 @@ func getCategory(c *gin.Context) { } if cat.UserID != uid { + log.Printf("getCategory(): unauthorized access to category not owned by current user") c.JSON(http.StatusUnauthorized, gin.H{"error": "Category is not owned by user"}) return } @@ -376,7 +373,7 @@ func getCategory(c *gin.Context) { func putCat(c *gin.Context) { uid, err := getUserId(c) if err != nil { - c.String(http.StatusInternalServerError, "Failure to retrieve user id") + c.JSON(http.StatusInternalServerError, gin.H{"error": "error authenticating user"}) return } @@ -390,24 +387,21 @@ func putCat(c *gin.Context) { success, catID, err := crud.CreateCategory(json) if success { c.JSON(http.StatusOK, gin.H{"message": "Success", "catID": catID}) - return } else { - c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create cat", "details": err.Error()}) - return + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create category", "details": err.Error()}) } } func getUserPoints(c *gin.Context) { uid, err := getUserId(c) if err != nil { - c.String(http.StatusInternalServerError, "Failure to retrieve user id") + c.JSON(http.StatusInternalServerError, gin.H{"error": "error authenticating user"}) return } ret, fnd, err := crud.GetUserPoints(uid) - if !fnd { - log.Println("getTaskById(): Problem in getUserPoints, probably DB related", err) - c.JSON(http.StatusBadRequest, gin.H{"error": "This is really bad"}) + if !fnd || err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "error retrieving user points"}) return } c.JSON(http.StatusOK, gin.H{"points": ret}) @@ -417,25 +411,24 @@ func getUserPoints(c *gin.Context) { func createTask(c *gin.Context) { uid, err := getUserId(c) if err != nil { - c.String(http.StatusInternalServerError, "Failure to retrieve user id") + c.JSON(http.StatusInternalServerError, gin.H{"error": "error authenticating user"}) return } + // Try to bind JSON sent in request into our Task struct var json crud.Task if err := c.ShouldBindJSON(&json); err != nil { log.Printf("createTask(): could not bind request to Task") - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return - } //take any JSON sent in the BODY of the request and try to bind it to our Task struct + } json.UserID = uid - success, taskID, err := crud.CreateTask(json) //pass struct into function to add Task to db + success, taskID, err := crud.CreateTask(json) if success { c.JSON(http.StatusOK, gin.H{"message": "Success", "taskID": taskID}) - return } else { c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create task", "details": err.Error()}) - return } } @@ -443,7 +436,7 @@ func createTask(c *gin.Context) { func editTask(c *gin.Context) { uid, err := getUserId(c) if err != nil { - c.String(http.StatusInternalServerError, "Failure to retrieve user id") + c.JSON(http.StatusInternalServerError, gin.H{"error": "error authenticating user"}) return } @@ -456,21 +449,21 @@ func editTask(c *gin.Context) { } if err := c.ShouldBindJSON(&json); err != nil { + log.Printf("editTask(): could not bind request to Task") c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } json.UserID = uid - fmt.Println("we are checkin the field of the jsawn") - fmt.Println(json.Status) + // Hall of fame comment + // fmt.Println("we are checkin the field of the jsawn") + // fmt.Println(json.Status) success, err := crud.EditTask(json, tid) if success { c.JSON(http.StatusOK, gin.H{"message": "Success"}) - return } else { c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to edit task", "details": err.Error()}) - return } } @@ -478,7 +471,7 @@ func editTask(c *gin.Context) { func deleteTask(c *gin.Context) { uid, err := getUserId(c) if err != nil { - c.String(http.StatusInternalServerError, "Failure to retrieve user id") + c.JSON(http.StatusInternalServerError, gin.H{"error": "error authenticating user"}) return } @@ -490,7 +483,6 @@ func deleteTask(c *gin.Context) { } success, err := crud.DeleteTask(tid, uid) - if success { c.JSON(http.StatusOK, gin.H{"message": "Success"}) } else { @@ -502,7 +494,7 @@ func deleteTask(c *gin.Context) { func getAllUserTasks(c *gin.Context) { uid, err := getUserId(c) if err != nil { - c.String(http.StatusInternalServerError, "Failure to retrieve user id") + c.JSON(http.StatusInternalServerError, gin.H{"error": "error authenticating user"}) return } @@ -511,15 +503,13 @@ func getAllUserTasks(c *gin.Context) { c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve all user tasks"}) return } - log.Println("working") - log.Println(arr) c.JSON(http.StatusOK, gin.H{"list": arr}) } func passTheTask(c *gin.Context) { uid, err := getUserId(c) if err != nil { - c.String(http.StatusInternalServerError, "Failure to retrieve user id") + c.JSON(http.StatusInternalServerError, gin.H{"error": "error authenticating user"}) return } @@ -533,24 +523,20 @@ func passTheTask(c *gin.Context) { success, bossId, err := crud.PassTask(tid, uid) if success && err == nil { c.JSON(http.StatusOK, gin.H{"message": "Success", "bossId": bossId}) - return } else { - log.Printf("passTheTask(): %v", err) c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to pass task"}) - return } } func failTheTask(c *gin.Context) { uid, err := getUserId(c) if err != nil { - c.String(http.StatusInternalServerError, "Failure to retrieve user id") + c.JSON(http.StatusInternalServerError, gin.H{"error": "error authenticating user"}) return } tid, err := strconv.Atoi(c.Param("id")) if err != nil { - log.Println("editTask(): Invalid taskID") c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid TaskId"}) return @@ -559,11 +545,8 @@ func failTheTask(c *gin.Context) { success, err := crud.FailTask(tid, uid) if success && err == nil { c.JSON(http.StatusOK, gin.H{"message": "Success"}) - return } else { - log.Printf("failTheTask(): %v", err) c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fail task"}) - return } } @@ -571,7 +554,7 @@ func failTheTask(c *gin.Context) { func getTaskById(c *gin.Context) { uid, err := getUserId(c) if err != nil { - c.String(http.StatusInternalServerError, "Failure to retrieve user id") + c.JSON(http.StatusInternalServerError, gin.H{"error": "error authenticating user"}) return } @@ -589,12 +572,13 @@ func getTaskById(c *gin.Context) { return } if err != nil { - log.Printf("getTaskById(): %v", err) - c.JSON(http.StatusInternalServerError, gin.H{"error": "This is really bad"}) + c.JSON(http.StatusInternalServerError, gin.H{"error": "error retreiving task"}) return } if task.UserID != uid { - c.JSON(http.StatusUnauthorized, gin.H{"error": "task not owned by user"}) + log.Printf("getTaskById(): unauthorized access to task not owned by current user") + c.JSON(http.StatusUnauthorized, gin.H{"error": "task not owned by current user"}) + return } c.JSON(http.StatusOK, gin.H{"task": task}) @@ -604,30 +588,26 @@ func getTaskById(c *gin.Context) { func getuserTaskSpan(c *gin.Context) { uid, err := getUserId(c) if err != nil { - c.String(http.StatusInternalServerError, "Failure to retrieve user id") + c.JSON(http.StatusInternalServerError, gin.H{"error": "error authenticating user"}) return } - starttime, err1 := time.Parse(time.RFC3339, c.Param("start")) - if err1 != nil { - log.Println("startime", err) - c.String(http.StatusBadRequest, "Error: incorrect request time format") + starttime, err := time.Parse(time.RFC3339, c.Param("start")) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "incorrect request time format (start)"}) return } - log.Println(starttime) - endtime, err2 := time.Parse(time.RFC3339, c.Param("end")) - if err2 != nil { - log.Println("enime", err) - c.String(http.StatusBadRequest, "Error: incorrect request time format") + endtime, err := time.Parse(time.RFC3339, c.Param("end")) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "incorrect request time format (end)"}) return } - log.Println(endtime) + arr, err := crud.GetUserTaskDateTime(uid, starttime, endtime) - log.Println(arr) if err != nil { log.Println("getAllUserTasks(): Problem probably DB related") - c.JSON(http.StatusBadRequest, gin.H{"error": "This is really bad"}) + c.JSON(http.StatusBadRequest, gin.H{"error": "failed to retreive tasks in given span"}) return } @@ -640,8 +620,7 @@ func searchUsers(c *gin.Context) { if method == "name" { users, foundAny, err := crud.SearchUsername(query, false) if err != nil { - log.Printf("searchUsers(): error searching for users: %v", err) - c.JSON(http.StatusInternalServerError, gin.H{"error": "This is really bad"}) + c.JSON(http.StatusInternalServerError, gin.H{"error": "failed searching for users"}) return } @@ -667,24 +646,22 @@ func searchUsers(c *gin.Context) { res = append(res, user) c.JSON(http.StatusOK, gin.H{"num_results": 1, "users": res}) - } else { - c.String(http.StatusBadRequest, "Format error: accepted methods of user search are by \"name\" and \"code\".") + c.JSON(http.StatusBadRequest, gin.H{"error": "format error: accepted methods of user search are by \"name\" or \"code\""}) } } func addFriend(c *gin.Context) { uid, err := getUserId(c) if err != nil { - c.String(http.StatusInternalServerError, "Failure to retrieve user id") + c.JSON(http.StatusInternalServerError, gin.H{"error": "error authenticating user"}) return } their_code := c.Param("code") addSuccess, err := crud.AddFriend(uid, their_code) if !addSuccess || err != nil { - log.Printf("addFriend(): error adding friend: %v", err) - c.String(http.StatusInternalServerError, "Failure to add friend") + c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to add friend"}) return } @@ -694,15 +671,14 @@ func addFriend(c *gin.Context) { func removeFriend(c *gin.Context) { uid, err := getUserId(c) if err != nil { - c.String(http.StatusInternalServerError, "Failure to retrieve user id") + c.JSON(http.StatusInternalServerError, gin.H{"error": "error authenticating user"}) return } their_code := c.Param("code") delSuccess, err := crud.DeleteFriend(uid, their_code) if !delSuccess || err != nil { - log.Printf("deleteFriend(): error removing friend: %v", err) - c.String(http.StatusInternalServerError, "Failure to remove friend") + c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to remove friend"}) return } @@ -712,14 +688,13 @@ func removeFriend(c *gin.Context) { func getFriendList(c *gin.Context) { uid, err := getUserId(c) if err != nil { - c.String(http.StatusInternalServerError, "Failure to retrieve user id") + c.JSON(http.StatusInternalServerError, gin.H{"error": "error authenticating user"}) return } friends, err := crud.GetFriendList(uid, false) if err != nil { - log.Printf("getFriendList(): error retrieving friend list: %v", err) - c.String(http.StatusInternalServerError, "Failure to retrieve friend list") + c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to retrieve friend list"}) return } diff --git a/testdb b/testdb deleted file mode 100644 index 7c6dd99ebf8dec2518f408d423c347db62d4cd48..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 32768 zcmeI%(NEh(90zc_1yZtg^es;nC-ot=3a!}0b8C{LcnuCUrXA8G{EYmdJz{}fB*y_0D*Z4T>M9HiydfXR z@dvDEBonRMU6!ExJ!+m3s7|v2I@0mTHZM5A_ee3a{m7?3gFx z;=NW0tf&=HEsOI1iibE|k2nXNP9E4~NGhU<_T?6jcp~ktE6MsgIbr8L952Y z;c>>|ARq24BraYr)*7p;`sI5ivBlF=S(SyQibYv{^%2YKF9(smKNqrGG&|Jlcsu2i zsd}mD2L5rm4ytJD>)T<(y1|cB9~uY$=uvba#VFY*Et1yQ z*fdFNhqhiCVtCnLJH%-A_+S{Zj0MfLX5SYdpeT@L#sl6ouQmWXby;teBw>6^wz}O8 zwVWvpJGG*y?T>G1ZioGT*c(OJ_=bm>XjBix{B$gyWx#@PG?347;wOQ87Adgh_fAE^ zk$|(5^mzP^C8;#zeIoCBn&0(Q-a;}?hCGb3RAID4uF~^i!cuj^PGwD()mLl!rd2w*$yc*?Pg#5}wS!w4D6a@rqd;t|C4Q5ne5Mp=}nZ*{i6FTZsk%eeH70@p1Rwwb2tWV=5P$##AOHaf i%uN92|GCj)VhBJ00uX=z1Rwwb2tWV=5P-nU1pWr+oe^IE