diff --git a/handlers/interest/list.go b/handlers/interest/list.go index ee99088..b2bff9a 100644 --- a/handlers/interest/list.go +++ b/handlers/interest/list.go @@ -1,6 +1,8 @@ package interest import ( + "strconv" + "github.com/cjtim/be-friends-api/internal/auth" "github.com/cjtim/be-friends-api/repository" "github.com/gofiber/fiber/v2" @@ -11,9 +13,24 @@ func List(c *fiber.Ctx) error { if err != nil { return err } - interested, err := repository.InterestedRepo.ListByUserID(claims.ID) + petIdStr := c.Query("pet_id") + if petIdStr == "" { + // List intested by user_id + interested, err := repository.InterestedRepo.ListByUserID(claims.ID) + if err != nil { + return err + } + return c.JSON(interested) + } + // If pet_id exist + // Fetch users that interested + petId, err := strconv.Atoi(petIdStr) + if err != nil { + return err + } + users, err := repository.InterestedRepo.ListByPetID(petId) if err != nil { return err } - return c.JSON(interested) + return c.JSON(users) } diff --git a/repository/interested.go b/repository/interested.go index 42fe7d0..962206d 100644 --- a/repository/interested.go +++ b/repository/interested.go @@ -19,6 +19,15 @@ func (i *InterestedImpl) ListByUserID(userID uuid.UUID) (output []Interested, er return } +func (i *InterestedImpl) ListByPetID(petId int) (output []User, err error) { + stm := `SELECT u.* FROM "interested" l + INNER JOIN "user" u + ON u.id = l.user_id + WHERE l.pet_id = $1` + err = DB.Select(&output, stm, petId) + return +} + func (i *InterestedImpl) Add(petID int, userID uuid.UUID) error { stm := `INSERT INTO "interested" (pet_id, user_id) VALUES ($1, $2)` _, err := DB.Exec(stm, petID, userID)