Skip to content

Commit

Permalink
feat: shelter able to fetch users interested by pet_id
Browse files Browse the repository at this point in the history
  • Loading branch information
cjtim committed Oct 11, 2022
1 parent 3af4b30 commit a195e22
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
21 changes: 19 additions & 2 deletions handlers/interest/list.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
}
9 changes: 9 additions & 0 deletions repository/interested.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a195e22

Please sign in to comment.