diff --git a/backend/internal/handlers/profile/preferences.go b/backend/internal/handlers/profile/preferences.go new file mode 100644 index 0000000..2619ba5 --- /dev/null +++ b/backend/internal/handlers/profile/preferences.go @@ -0,0 +1,27 @@ +package profile + +import ( + "github.com/GenerateNU/nightlife/internal/errs" + "github.com/GenerateNU/nightlife/internal/models" + "github.com/gofiber/fiber/v2" +) + +// POST Endpoint -> allows users to add their preferences to the db +func (s *Service) CreatePreferences(c *fiber.Ctx) error { + var p models.Preferences + if err := c.BodyParser(&p); err != nil { + return errs.BadRequest(err) + } + + if verrs := p.Validate(); verrs != nil { + return errs.InvalidRequestData(verrs) + } + + if err := s.store.CreatePreferences(c.Context(), p); err != nil { + return err + } + + // close out with success status + return c.Status(fiber.StatusCreated).JSON(p) + +} diff --git a/backend/internal/handlers/profile/routes.go b/backend/internal/handlers/profile/routes.go new file mode 100644 index 0000000..16ebc1e --- /dev/null +++ b/backend/internal/handlers/profile/routes.go @@ -0,0 +1,21 @@ +package profile + +import ( + "github.com/GenerateNU/nightlife/internal/auth" + "github.com/GenerateNU/nightlife/internal/types" + "github.com/gofiber/fiber/v2" +) + +// Create Profile fiber route group (no unprotected routes) +func Routes(app *fiber.App, params types.Params) { + service := newService(params.Store) + + // Create Protected Grouping + protected := app.Group("/profile") + + // Register Middleware + protected.Use(auth.Protected(¶ms.Supabase)) + + //Endpoints + protected.Post("/preferences", service.CreatePreferences) +} diff --git a/backend/internal/handlers/profile/service.go b/backend/internal/handlers/profile/service.go new file mode 100644 index 0000000..92c0687 --- /dev/null +++ b/backend/internal/handlers/profile/service.go @@ -0,0 +1,11 @@ +package profile + +import "github.com/GenerateNU/nightlife/internal/storage" + +type Service struct { + store storage.Storage +} + +func newService(store storage.Storage) *Service { + return &Service{store: store} +} diff --git a/backend/internal/models/preferences.go b/backend/internal/models/preferences.go new file mode 100644 index 0000000..d7eb051 --- /dev/null +++ b/backend/internal/models/preferences.go @@ -0,0 +1,15 @@ +package models + +// Represents User Preferences +type Preferences struct { + UserID int `json:"user_id"` // 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? +} + +func (p *Preferences) Validate() map[string]string { + return nil +} diff --git a/backend/internal/storage/postgres/profile.go b/backend/internal/storage/postgres/profile.go new file mode 100644 index 0000000..462fd42 --- /dev/null +++ b/backend/internal/storage/postgres/profile.go @@ -0,0 +1,16 @@ +package postgres + +import ( + "context" + + "github.com/GenerateNU/nightlife/internal/models" +) + +func (db *DB) CreatePreferences(ctx context.Context, p models.Preferences) error { + // query to save user data to db + query := `INSERT INTO preferences (userID, location, age, music, ambiance, notifs) + VALUES ($1, $2, $3, $4, $5, $6)` + + _, err := db.conn.Query(ctx, query, p.UserID, p.Location, p.Age, p.Music, p.Ambiance, p.Notifs) + return err +} diff --git a/backend/internal/storage/storage.go b/backend/internal/storage/storage.go index 46c80a5..557b72e 100644 --- a/backend/internal/storage/storage.go +++ b/backend/internal/storage/storage.go @@ -11,12 +11,17 @@ type Storage interface { Close(context.Context) error Test UserRating + Profile } type Test interface { GetAllTests(context.Context) ([]models.Test, error) } +type Profile interface { + CreatePreferences(context.Context, models.Preferences) error +} + type UserRating interface { GetAllUserRatings(context.Context, uuid.UUID) ([]models.UserRating, error) }