-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
POST Profile Preferences Endpoint (#25)
* post profile preferences endpoint * post profile prefs endpoint * post prefs profile endpoint pr fix * preferences endpoint * Small fixes to resolve merge conflict --------- authored by: Ira Hysi Co-authored-by: Cam Plume <[email protected]>
- Loading branch information
Showing
6 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters