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

Feature/create friendship #36

Merged
merged 6 commits into from
Oct 1, 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
28 changes: 28 additions & 0 deletions backend/internal/handlers/friendship/friendship.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package friendship

import (
"fmt"
"github.com/GenerateNU/nightlife/internal/models"
"github.com/gofiber/fiber/v2"
"log"
)

// POST endpoint to create friendship between two users in DB
func (s *Service) CreateFriendship(c *fiber.Ctx) error {
fmt.Println("Creating a friendship")

var req models.Friendship

if err := c.BodyParser(&req); err != nil {
log.Printf("Error parsing JSON: %v, Request: %+v", err, req)
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "Cannot parse JSON",
})
}

if err := s.store.CreateFriendship(c.Context(), req); err != nil {
return err
}

return c.Status(fiber.StatusCreated).JSON(req)
}
21 changes: 21 additions & 0 deletions backend/internal/handlers/friendship/routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package friendship

import (
//"github.com/GenerateNU/nightlife/internal/auth"
"github.com/GenerateNU/nightlife/internal/types"
"github.com/gofiber/fiber/v2"
)

func Routes(app *fiber.App, params types.Params) {

//create a service
service := newService(params.Store)

//create a grouping
protected := app.Group("/friendships")
//.Use(auth.Protected(&params.Supabase))

//create a route
protected.Post("/api/friendship", service.CreateFriendship)

}
11 changes: 11 additions & 0 deletions backend/internal/handlers/friendship/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package friendship

import "github.com/GenerateNU/nightlife/internal/storage"

type Service struct {
store storage.Storage
}

func newService(store storage.Storage) *Service {
return &Service{store: store}
}
28 changes: 28 additions & 0 deletions backend/internal/models/friendship.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package models

import (
"encoding/json"
"github.com/google/uuid"
"time"
)

type FriendshipStatus string

const (
Pending FriendshipStatus = "PENDING"
Declined FriendshipStatus = "DECLINED"
Blocked FriendshipStatus = "BLOCKED"
Accepted FriendshipStatus = "ACCEPTED"
)

// convert FriendshipStatus to a string when serializing to JSON
func (fs FriendshipStatus) MarshalJSON() ([]byte, error) {
return json.Marshal(string(fs))
}

type Friendship struct {
UserID1 uuid.UUID `json:"user_id1"`
UserID2 uuid.UUID `json:"user_id2"`
FriendshipStatus FriendshipStatus `json:"friendship_status"`
CreatedAt time.Time `json:"created_at"`
}
4 changes: 4 additions & 0 deletions backend/internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server
import (
"github.com/GenerateNU/nightlife/internal/errs"
"github.com/GenerateNU/nightlife/internal/handlers/auth"
"github.com/GenerateNU/nightlife/internal/handlers/friendship"
"github.com/GenerateNU/nightlife/internal/handlers/health"
"github.com/GenerateNU/nightlife/internal/handlers/hello"
"github.com/GenerateNU/nightlife/internal/handlers/profiles"
Expand Down Expand Up @@ -38,6 +39,9 @@ func New(params types.Params) *fiber.App {
// Venue Ratings route group
venueratings.Routes(app, params)

// Friendship route group
friendship.Routes(app, params)

return app
}

Expand Down
16 changes: 16 additions & 0 deletions backend/internal/storage/postgres/friendship.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) CreateFriendship(ctx context.Context, friendship models.Friendship) error {
var query = `
INSERT INTO "Friendship" (user_id1, user_id2, friendship_status, created_at)
VALUES ($1, $2, $3, $4);
`
_, err := db.conn.Exec(ctx, query, friendship.UserID1, friendship.UserID2, friendship.FriendshipStatus, friendship.CreatedAt)
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 @@ -13,6 +13,7 @@ type Storage interface {
UserRating
Venues
Profile
Friendship
}

type Test interface {
Expand All @@ -35,3 +36,7 @@ type Venues interface {
DeleteReviewForVenue(context.Context, int8) error
GetAllVenueRatings(context.Context, uuid.UUID) ([]models.VenueRatings, error)
}

type Friendship interface {
CreateFriendship(context.Context, models.Friendship) error
}
Loading