Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POST Profile Preferences Endpoint #25

Merged
merged 6 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is a stub - Are you planning on adding form validation?

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)
}
Loading