Skip to content

Commit

Permalink
Merge pull request #55 from GenerateNU/venue-id-endpoint
Browse files Browse the repository at this point in the history
Venue id endpoint
  • Loading branch information
wyattchris authored Oct 20, 2024
2 parents e9e6eea + c5d7349 commit 38d7bbf
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 3 deletions.
Binary file added backend/bin/nightlife
Binary file not shown.
4 changes: 4 additions & 0 deletions backend/internal/handlers/venues/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ func Routes(app *fiber.App, params types.Params) {
protected.Delete("/:venueId", service.DeleteVenue)

protected.Delete("/reviews/:reviewId", service.DeleteReviewForVenue)

protected.Get("/:venueId", service.GetVenueFromID)
protected.Get("/search", service.GetVenueFromName)
protected.Get("/getAll", service.GetAllVenues)
}
2 changes: 1 addition & 1 deletion backend/internal/handlers/venues/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ type Service struct {

func newService(store storage.Storage) *Service {
return &Service{store: store}
}
}
41 changes: 39 additions & 2 deletions backend/internal/handlers/venues/venues.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package venues

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

"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
)

func (s *Service) DeleteVenue(c *fiber.Ctx) error {
Expand Down Expand Up @@ -58,3 +59,39 @@ func (s *Service) DeleteReviewForVenue(c *fiber.Ctx) error {

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

func (s *Service) GetVenueFromID(c *fiber.Ctx) error {
venueID := c.Params("venueId")
if venueID == "" {
return fiber.NewError(fiber.StatusBadRequest, "Venue ID is required")
}
formattedID, err := uuid.Parse(venueID)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Couldn't parse venue id to uuid")
}
venue, err := s.store.GetVenueFromID(c.Context(), formattedID)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "Could not get venue")
}
return c.Status(fiber.StatusOK).JSON(venue)
}

func (s *Service) GetVenueFromName(c *fiber.Ctx) error {
name := c.Query("q")
if name == "" {
return fiber.NewError(fiber.StatusBadRequest, "Venue name is required")
}
venue, err := s.store.GetVenueFromName(c.Context(), name)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "Could not get venue")
}
return c.Status(fiber.StatusOK).JSON(venue)
}

func (s *Service) GetAllVenues(c *fiber.Ctx) error {
venues, err := s.store.GetAllVenues(c.Context())
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "Could not get venue")
}
return c.Status(fiber.StatusOK).JSON(venues)
}
30 changes: 30 additions & 0 deletions backend/internal/models/venues.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package models

import (
"time"

"github.com/google/uuid"
)

type Venue struct {

VenueID uuid.UUID `json:"venue_id"`

Name string `json:"name"`

Address string `json:"address"`

City string `json:"city"`

State string `json:"state"`

ZipCode string `json:"zip_code"`

// location : Geographical point?

// VenueType string `json:"venue_type"`

CreatedAt time.Time `json:"created_at"`

//UpdatedAt time.Time `json:"updated_at"`
}
38 changes: 38 additions & 0 deletions backend/internal/storage/postgres/venues.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"fmt"

"github.com/GenerateNU/nightlife/internal/models"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
)

func (db *DB) DeleteVenue(ctx context.Context, id uuid.UUID) error {
Expand All @@ -30,3 +32,39 @@ func (db *DB) DeleteReviewForVenue(ctx context.Context, reviewID int8) error {

return nil
}

func (db *DB) GetVenueFromID(ctx context.Context, id uuid.UUID) (models.Venue, error) {
var query = `SELECT venue_id, name, address, city, state, zip_code, created_at FROM "Venue" WHERE venue_id = $1`
rows, err := db.conn.Query(ctx, query, id.String())
if err != nil {
return models.Venue{}, err
}
defer rows.Close()
arr, err := pgx.CollectRows(rows, pgx.RowToStructByName[models.Venue])
return arr[0], err
}

func (db *DB) GetVenueFromName(ctx context.Context, name string) (models.Venue, error) {
query := `SELECT venue_id, name, address, city, state, zip_code, created_at FROM "Venue" WHERE name ilike $1`
rows, err := db.conn.Query(ctx, query, name)
if err != nil {
fmt.Println("HALLO " + err.Error())
return models.Venue{}, err
}
defer rows.Close()
arr, err := pgx.CollectRows(rows, pgx.RowToStructByName[models.Venue])
return arr[0], err
}

func (db *DB) GetAllVenues(ctx context.Context) ([]models.Venue, error) {
query := `SELECT venue_id, name, address, city, state, zip_code, created_at FROM Venue`
rows, err := db.conn.Query(ctx, query)
if err != nil {
fmt.Println("HALLO " + err.Error())
return []models.Venue{}, err
}
defer rows.Close()
return pgx.CollectRows(rows, pgx.RowToStructByName[models.Venue])
}


3 changes: 3 additions & 0 deletions backend/internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ type Venues interface {
DeleteVenue(context.Context, uuid.UUID) error
DeleteReviewForVenue(context.Context, int8) error
GetAllVenueRatings(context.Context, uuid.UUID) ([]models.VenueRatings, error)
GetVenueFromID(context.Context, uuid.UUID) (models.Venue, error)
GetVenueFromName(context.Context, string) (models.Venue, error)
GetAllVenues(ctx context.Context) ([]models.Venue, error)
}

type Friendship interface {
Expand Down

0 comments on commit 38d7bbf

Please sign in to comment.