Skip to content

Commit

Permalink
POST Profile Preferences Endpoint (#25)
Browse files Browse the repository at this point in the history
* 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
ihysi2024 authored Sep 30, 2024
1 parent 0887c85 commit 3d76397
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 0 deletions.
27 changes: 27 additions & 0 deletions backend/internal/handlers/profile/preferences.go
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)

}
21 changes: 21 additions & 0 deletions backend/internal/handlers/profile/routes.go
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(&params.Supabase))

//Endpoints
protected.Post("/preferences", service.CreatePreferences)
}
11 changes: 11 additions & 0 deletions backend/internal/handlers/profile/service.go
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}
}
15 changes: 15 additions & 0 deletions backend/internal/models/preferences.go
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
}
16 changes: 16 additions & 0 deletions backend/internal/storage/postgres/profile.go
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
}
5 changes: 5 additions & 0 deletions backend/internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 3d76397

Please sign in to comment.