Skip to content

Commit

Permalink
feat: list pet by shelterId
Browse files Browse the repository at this point in the history
  • Loading branch information
cjtim committed Sep 11, 2022
1 parent acbb8bf commit d829cf1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
13 changes: 13 additions & 0 deletions handlers/pet/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/cjtim/be-friends-api/repository"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
)

// PetList - list all pets from database
Expand All @@ -17,6 +18,18 @@ import (
// @Failure 500 {string} string
// @Router /api/v1/pet [get]
func PetList(c *fiber.Ctx) error {
userId := c.Query("user_id")
if userId != "" {
_, err := uuid.Parse(userId)
if err != nil {
return err
}
pets, err := repository.PetRepo.List()
if err != nil {
return err
}
return c.JSON(pets)
}
pets, err := repository.PetRepo.List()
if err != nil {
return err
Expand Down
19 changes: 19 additions & 0 deletions repository/pet.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@ func (p *PetImpl) List() (pets []PetWithPic, err error) {
return pets, err
}

func (p *PetImpl) ListByUserId(userId uuid.UUID) (pets []PetWithPic, err error) {
stm := `
SELECT
p.*,
(
SELECT COALESCE(json_agg(pic), '[]')
FROM (
SELECT picture_url
FROM "pic_pet" pp
WHERE pp.pet_id = p.id
) pic
) AS picture_urls
FROM pet p
WHERE p.user_id = $1
`
err = DB.Select(&pets, stm, userId)
return pets, err
}

func (p *PetImpl) GetById(id int) (pet PetWithPic, err error) {
stm := `
SELECT
Expand Down
14 changes: 7 additions & 7 deletions tools/db/5-data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ INSERT INTO "status" (name)
VALUES ('new'), ('pending'), ('adoped');


-- -- Test credential
-- -- [email protected]
-- -- test
-- INSERT INTO "user" (id, name, email, password, is_org, line_uid)
-- VALUES
-- ('097b2b93-0a66-4de2-a5a6-4f0b15acc54c', 'Test', '[email protected]', '$2a$04$x1jU9wX5Ab7fzyL.qG5CO.4CHB/t3lq0obSjdXkJ5.tmlwjVJZyRO', TRUE, NULL),
-- ('e15f64ec-d87e-4bc3-8680-3fb7b47d438d', 'Line User Test', NULL, NULL, FALSE, 'aaaaaaaaaaaaaaaaaaaa');
-- Test credential
-- [email protected]
-- test
INSERT INTO "user" (id, name, email, password, is_org, line_uid)
VALUES
('097b2b93-0a66-4de2-a5a6-4f0b15acc54c', 'Test', '[email protected]', '$2a$04$x1jU9wX5Ab7fzyL.qG5CO.4CHB/t3lq0obSjdXkJ5.tmlwjVJZyRO', TRUE, NULL),
('e15f64ec-d87e-4bc3-8680-3fb7b47d438d', 'Line User Test', NULL, NULL, FALSE, 'aaaaaaaaaaaaaaaaaaaa');

-- INSERT INTO "pet" (name, description, lat, lng)
-- VALUES
Expand Down

0 comments on commit d829cf1

Please sign in to comment.