From 967afc4d7512815eae2bdc4ed97dd2c08f443fa1 Mon Sep 17 00:00:00 2001 From: ihysi2024 <138328084+ihysi2024@users.noreply.github.com> Date: Tue, 24 Sep 2024 22:49:54 -0400 Subject: [PATCH] post profile prefs endpoint --- .../internal/handlers/profile/preferences.go | 38 ++----------------- .../internal/schema/profile/routes/routes.go | 26 ------------- .../profile/transactions/transactions.go | 18 --------- backend/internal/storage/postgres/profile.go | 2 + 4 files changed, 6 insertions(+), 78 deletions(-) delete mode 100644 backend/internal/schema/profile/routes/routes.go delete mode 100644 backend/internal/schema/profile/transactions/transactions.go diff --git a/backend/internal/handlers/profile/preferences.go b/backend/internal/handlers/profile/preferences.go index 94e1074..ed2e66e 100644 --- a/backend/internal/handlers/profile/preferences.go +++ b/backend/internal/handlers/profile/preferences.go @@ -4,46 +4,16 @@ import ( "github.com/gofiber/fiber/v2" ) -type Preferences struct { - userID int `json: "userID"` // assuming 1:1/1:* relationship - location string `json: "location"`//city/state combo? not sure how this is getting defined - age int `json: "age"` - music string `json: "music"` - ambiance string `json: "ambiance"` - notifs bool `json: "notifs"`// is this part of the preferences? -} // POST Endpoint -> allows users to add their preferences to the db -func (p *Preferences) RetPreferences(c *fiber.Ctx, db *pgx.Conn) error { - - // json body with respective attributes - // fill the struct -> 400 status if not in correct format - if err := c.BodyParser(&p); err != nil { - return c.Status(400).SendString("Invalid body format") - } - - // split preference attributes to insert to db - userID := p.userID - loc := p.location - age := p.age - music := p.music - amb := p.ambiance - notifs := p.notifs - - // insert pref attributes to prefs table - query := `INSERT INTO preferences (userID, location, age, music, ambiance, notifs) - VALUES ($1, $2, $3, $4, $5, $6)` +func (s *Service) RetPreferences(c *fiber.Ctx) error { - // execute query to save preferences to db - _, err := db.Exec(context.Background(), query, userID, loc, age, music, amb, notifs) - - // handle case where db connection bad + tests, err := s.store.StorePreferences(c.Context()) if err != nil { - return c.Status(500).SendString("Database Error") - } + return err + } // close out with success status return c.Status(fiber.StatusOK).SendString("Preferences saved successfully") - } diff --git a/backend/internal/schema/profile/routes/routes.go b/backend/internal/schema/profile/routes/routes.go deleted file mode 100644 index 6ff4689..0000000 --- a/backend/internal/schema/profile/routes/routes.go +++ /dev/null @@ -1,26 +0,0 @@ -package profile - -//NOTE: This is an example usage for auth demonstration purposes. In real configurations (beyond login) all route groups should be protected - -import ( - "github.com/GenerateNU/nightlife/internal/auth" - "github.com/GenerateNU/nightlife/internal/config" - profile "github.com/GenerateNU/nightlife/internal/schema/profile/transactions" - "github.com/gofiber/fiber/v2" -) - -// Create ProfileGroup fiber route group -// No unprotected routes -> all go through user auth -func RouteProfileGroup(app *fiber.App, config *config.Config) { - - // Create Protected Grouping -> allow user to *only* access - // their personal info - protected := app.Group("/profile_protected") - - // Register Middleware - protected.Use(auth.Protected(config)) - - //Endpoints - protected.Get("/profile", hello.RetPreferences) - -} diff --git a/backend/internal/schema/profile/transactions/transactions.go b/backend/internal/schema/profile/transactions/transactions.go deleted file mode 100644 index 7701493..0000000 --- a/backend/internal/schema/profile/transactions/transactions.go +++ /dev/null @@ -1,18 +0,0 @@ -package profile - -import ( - "github.com/gofiber/fiber/v2" -) - -type Preferences struct { - Location string `json:"location"` - Age int `json:"age"` - Music string `json:"music"` - Ambiance string `json:"ambiance"` - Notifs bool `json:"notif_prefs` -} - - -func RetPreferences(ctx *fiber.Ctx) error { - -} diff --git a/backend/internal/storage/postgres/profile.go b/backend/internal/storage/postgres/profile.go index 3a3d9b2..ec11771 100644 --- a/backend/internal/storage/postgres/profile.go +++ b/backend/internal/storage/postgres/profile.go @@ -19,6 +19,7 @@ type Preferences struct { func (db *DB) StorePreferences(ctx context.Context) ([]models.Test, error) { var p Preferences + // save user response to Preferences instance if err := c.BodyParser(&p); err != nil { return c.Status(400).SendString("Invalid body format") } @@ -31,6 +32,7 @@ func (db *DB) StorePreferences(ctx context.Context) ([]models.Test, error) { amb := p.ambiance notifs := p.notifs + // query to save user data to db query := `INSERT INTO preferences (userID, location, age, music, ambiance, notifs) VALUES ($1, $2, $3, $4, $5, $6)`