diff --git a/backend/internal/handlers/venue_ratings/routes.go b/backend/internal/handlers/venue_ratings/routes.go index 606149f..9786603 100644 --- a/backend/internal/handlers/venue_ratings/routes.go +++ b/backend/internal/handlers/venue_ratings/routes.go @@ -16,6 +16,6 @@ func Routes(app *fiber.App, params types.Params) { //.Use(auth.Protected(¶ms.Supabase)) //create a route - protected.Get("/api/venue/:venueid/ratings", service.GetAllVenueRatings) + protected.Get("/venue/:venueid/ratings", service.GetAllVenueRatings) } diff --git a/backend/internal/handlers/venues/routes.go b/backend/internal/handlers/venues/routes.go new file mode 100644 index 0000000..9cd3f1f --- /dev/null +++ b/backend/internal/handlers/venues/routes.go @@ -0,0 +1,23 @@ +package venues + +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(¶ms.Supabase)) + + //Endpoints + protected.Delete("/:venueId", service.DeleteVenue) + + protected.Delete("/reviews/:reviewId", service.DeleteReviewForVenue) +} diff --git a/backend/internal/handlers/venues/service.go b/backend/internal/handlers/venues/service.go new file mode 100644 index 0000000..e08c69c --- /dev/null +++ b/backend/internal/handlers/venues/service.go @@ -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} +} diff --git a/backend/internal/handlers/venues/venues.go b/backend/internal/handlers/venues/venues.go new file mode 100644 index 0000000..69ef2d0 --- /dev/null +++ b/backend/internal/handlers/venues/venues.go @@ -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"}) +} + diff --git a/backend/internal/server/server.go b/backend/internal/server/server.go index 1b6c2cc..6177158 100644 --- a/backend/internal/server/server.go +++ b/backend/internal/server/server.go @@ -8,6 +8,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" @@ -27,6 +28,7 @@ func New(params types.Params) *fiber.App { hello.Routes(app, params) test.Routes(app, params) auth.Routes(app, params) + venues.Routes(app, params) profiles.Routes(app, params) //User Ratings route group diff --git a/backend/internal/storage/postgres/venueratings.go b/backend/internal/storage/postgres/venueratings.go index f9c2edd..46f590b 100644 --- a/backend/internal/storage/postgres/venueratings.go +++ b/backend/internal/storage/postgres/venueratings.go @@ -36,5 +36,4 @@ WHERE return pgx.CollectRows(rows, pgx.RowToStructByName[models.VenueRatings]) - //return []models.VenueRatings{}, nil } diff --git a/backend/internal/storage/postgres/venues.go b/backend/internal/storage/postgres/venues.go new file mode 100644 index 0000000..ebf0bb2 --- /dev/null +++ b/backend/internal/storage/postgres/venues.go @@ -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 +} + diff --git a/backend/internal/storage/storage.go b/backend/internal/storage/storage.go index 86883f0..5f7d5f9 100644 --- a/backend/internal/storage/storage.go +++ b/backend/internal/storage/storage.go @@ -11,7 +11,7 @@ type Storage interface { Close(context.Context) error Test UserRating - VenueRatings + Venues Profile } @@ -30,6 +30,8 @@ type UserRating interface { GetAllUserRatings(context.Context, uuid.UUID) ([]models.UserRating, error) } -type VenueRatings interface { +type Venues interface { + DeleteVenue(context.Context, uuid.UUID) error + DeleteReviewForVenue(context.Context, int8) error GetAllVenueRatings(context.Context, uuid.UUID) ([]models.VenueRatings, error) }