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/delete/venue review #34

Merged
merged 9 commits into from
Oct 1, 2024
25 changes: 25 additions & 0 deletions backend/internal/handlers/venues/routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package venues

//NOTE: This is an example usage for auth demonstration purposes. In real configurations (beyond login) all route groups should be protected
wyattchris marked this conversation as resolved.
Show resolved Hide resolved

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

// Create HelloGroup fiber route group
func Routes(app *fiber.App, params types.Params) {
service := newService(params.Store)

// Create Protected Grouping
protected := app.Group("/venues")

// Register Middleware
protected.Use(auth.Protected(&params.Supabase))

//Endpoints
protected.Delete("/:venueId", service.DeleteVenue)

protected.Delete("/reviews/:reviewId", service.DeleteReviewForVenue)
}
11 changes: 11 additions & 0 deletions backend/internal/handlers/venues/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package venues

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

type Service struct {
store storage.Storage
}

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

import (
"fmt"
"net/http"
"strconv"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
)

func (s *Service) DeleteVenue(c *fiber.Ctx) error {
fmt.Println("Pinging correct service")

venueID := c.Params("venueId")
if venueID == "" {
c.Status(http.StatusBadRequest)
return fiber.NewError(400)
}
venueIDFormatted, err := uuid.Parse(venueID)

if err != nil {
c.Status(http.StatusBadRequest)
return fiber.NewError(400, "Delete failed due to malformed venue ID")
}

venueDelete := s.store.DeleteVenue(c.Context(), venueIDFormatted)

if venueDelete != nil {
c.Status(http.StatusInternalServerError)
return fiber.NewError(500, "Delete failed")
}

return c.Status(http.StatusOK).JSON("deleted venue")

}

/*
Deletes a review for a venue.
*/
func (s *Service) DeleteReviewForVenue(c *fiber.Ctx) error {

reviewID := c.Params("reviewId")
if reviewID == "" {
return fiber.NewError(fiber.StatusBadRequest, "Review ID is required")
}

parsedID, err := strconv.ParseInt(reviewID, 10, 8)
reviewIDFormatted := int8(parsedID)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Delete failed due to malformed review ID.")
}

// Call the store's DeleteReviewForVenue function with reviewId
err = s.store.DeleteReviewForVenue(c.Context(), reviewIDFormatted)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "Delete failed")
}

return c.Status(fiber.StatusOK).JSON(fiber.Map{"message": "Deleted review for venue"})
}

2 changes: 2 additions & 0 deletions backend/internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/GenerateNU/nightlife/internal/handlers/test"
userrating "github.com/GenerateNU/nightlife/internal/handlers/user_ratings"
venueratings "github.com/GenerateNU/nightlife/internal/handlers/venue_ratings"
"github.com/GenerateNU/nightlife/internal/handlers/venues"
"github.com/GenerateNU/nightlife/internal/middleware"
"github.com/GenerateNU/nightlife/internal/types"
go_json "github.com/goccy/go-json"
Expand All @@ -26,6 +27,7 @@ func New(params types.Params) *fiber.App {
hello.Routes(app, params)
test.Routes(app, params)
auth.Routes(app, params)
venues.Routes(app, params)

//User Ratings route group
userrating.Routes(app, params)
Expand Down
33 changes: 33 additions & 0 deletions backend/internal/storage/postgres/venues.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package postgres

import (
"context"
"fmt"

"github.com/google/uuid"
)

func (db *DB) DeleteVenue(ctx context.Context, id uuid.UUID) error {
_, err := db.conn.Exec(ctx, `DELETE FROM "Venue" v WHERE venue_id = $1`, id)
if err != nil {
return err
}
return nil
}

/*
Deletes a review for a venue.
*/
func (db *DB) DeleteReviewForVenue(ctx context.Context, reviewID int8) error {
result, err := db.conn.Exec(ctx, `DELETE FROM "Review" WHERE review_id = $1`, reviewID)
if err != nil {
return err
}

if result.RowsAffected() == 0 {
return fmt.Errorf("delete failed: review does not exist")
}

return nil
}

6 changes: 6 additions & 0 deletions backend/internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Storage interface {
Close(context.Context) error
Test
UserRating
Venues
VenueRatings
Profile
}
Expand All @@ -27,6 +28,11 @@ type UserRating interface {
GetAllUserRatings(context.Context, uuid.UUID) ([]models.UserRating, error)
}

type Venues interface {
wyattchris marked this conversation as resolved.
Show resolved Hide resolved
wyattchris marked this conversation as resolved.
Show resolved Hide resolved
DeleteVenue(context.Context, uuid.UUID) error
DeleteReviewForVenue(context.Context, int8) error
}

type VenueRatings interface {
GetAllVenueRatings(context.Context, uuid.UUID) ([]models.VenueRatings, error)
}
Loading