Skip to content

Commit

Permalink
post profile prefs endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ihysi2024 committed Sep 25, 2024
1 parent dc17378 commit 967afc4
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 78 deletions.
38 changes: 4 additions & 34 deletions backend/internal/handlers/profile/preferences.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")


}
26 changes: 0 additions & 26 deletions backend/internal/schema/profile/routes/routes.go

This file was deleted.

18 changes: 0 additions & 18 deletions backend/internal/schema/profile/transactions/transactions.go

This file was deleted.

2 changes: 2 additions & 0 deletions backend/internal/storage/postgres/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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)`

Expand Down

0 comments on commit 967afc4

Please sign in to comment.