Skip to content

Commit

Permalink
fix: liked and interested
Browse files Browse the repository at this point in the history
  • Loading branch information
cjtim committed Sep 17, 2022
1 parent c95be68 commit fcccd19
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
2 changes: 1 addition & 1 deletion repository/interested.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (i *InterestedImpl) Add(petID int, userID uuid.UUID) error {
}

func (i *InterestedImpl) Remove(petID int, userID uuid.UUID) error {
stm := `DELETE "interested" WHERE pet_id = $1, user_id = $2`
stm := `DELETE FROM "interested" WHERE pet_id = $1 AND user_id = $2`
_, err := DB.Exec(stm, petID, userID)
return err
}
2 changes: 1 addition & 1 deletion repository/liked.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (i *LikedImpl) Add(petID int, userID uuid.UUID) error {
}

func (i *LikedImpl) Remove(petID int, userID uuid.UUID) error {
stm := `DELETE "liked" WHERE pet_id = $1, user_id = $2`
stm := `DELETE FROM "liked" WHERE pet_id = $1 AND user_id = $2`
_, err := DB.Exec(stm, petID, userID)
return err
}
56 changes: 53 additions & 3 deletions repository/pet.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type Pet struct {
type PetWithPic struct {
Pet
PictureURLs json.RawMessage `json:"picture_urls" db:"picture_urls"`
Liked json.RawMessage `json:"liked" db:"liked"`
Interested json.RawMessage `json:"interested" db:"interested"`
}

func (p *PetImpl) List() (pets []PetWithPic, err error) {
Expand All @@ -40,7 +42,23 @@ func (p *PetImpl) List() (pets []PetWithPic, err error) {
FROM "pic_pet" pp
WHERE pp.pet_id = p.id
) pic
) AS picture_urls
) AS picture_urls,
(
SELECT COALESCE(json_agg(user_id), '[]')
FROM (
SELECT user_id
FROM "liked" lk
WHERE lk.pet_id = p.id
) liked
) AS liked,
(
SELECT COALESCE(json_agg(user_id), '[]')
FROM (
SELECT user_id
FROM "interested" itrt
WHERE itrt.pet_id = p.id
) itrt
) AS interested
FROM pet p
`
err = DB.Select(&pets, stm)
Expand All @@ -58,7 +76,23 @@ func (p *PetImpl) ListByUserId(userId uuid.UUID) (pets []PetWithPic, err error)
FROM "pic_pet" pp
WHERE pp.pet_id = p.id
) pic
) AS picture_urls
) AS picture_urls,
(
SELECT COALESCE(json_agg(user_id), '[]')
FROM (
SELECT user_id
FROM "liked" lk
WHERE lk.pet_id = p.id
) liked
) AS liked,
(
SELECT COALESCE(json_agg(user_id), '[]')
FROM (
SELECT user_id
FROM "interested" itrt
WHERE itrt.pet_id = p.id
) itrt
) AS interested
FROM pet p
WHERE p.user_id = $1
`
Expand All @@ -77,7 +111,23 @@ func (p *PetImpl) GetById(id int) (pet PetWithPic, err error) {
FROM "pic_pet" pp
WHERE pp.pet_id = p.id
) pic
) AS picture_urls
) AS picture_urls,
(
SELECT COALESCE(json_agg(user_id), '[]')
FROM (
SELECT user_id
FROM "liked" lk
WHERE lk.pet_id = p.id
) liked
) AS liked,
(
SELECT COALESCE(json_agg(user_id), '[]')
FROM (
SELECT user_id
FROM "interested" itrt
WHERE itrt.pet_id = p.id
) itrt
) AS interested
FROM pet p
WHERE id = $1
`
Expand Down

0 comments on commit fcccd19

Please sign in to comment.