Skip to content

Commit

Permalink
feat: join interested, liked table with pet table
Browse files Browse the repository at this point in the history
  • Loading branch information
cjtim committed Aug 30, 2022
1 parent 5528a64 commit acbb8bf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
13 changes: 6 additions & 7 deletions handlers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package handlers
import (
"github.com/cjtim/be-friends-api/configs"
"github.com/cjtim/be-friends-api/handlers/auth"
"github.com/cjtim/be-friends-api/handlers/interest"
"github.com/cjtim/be-friends-api/handlers/like"
interest "github.com/cjtim/be-friends-api/handlers/like"
"github.com/cjtim/be-friends-api/handlers/middlewares"
"github.com/cjtim/be-friends-api/handlers/pet"
pet_img "github.com/cjtim/be-friends-api/handlers/pet/img"
Expand Down Expand Up @@ -63,27 +63,26 @@ func Route(r *fiber.App) {
petRoute := v1.Group("/pet")
{
petRoute.Get("", pet.PetList) // list pet
petRoute.Get("/:pet_id", pet.PetDetails) // get pet by :pet_id
petRoute.Get("/:pet_id", middlewares.JWTMiddleware, pet.PetDetails) // get pet by :pet_id
petRoute.Post("", middlewares.JWTMiddleware, pet.PetCreate) // create pet
petRoute.Put("", middlewares.JWTMiddleware, pet.PetCreate) // create pet
petRoute.Post("/img", middlewares.JWTMiddleware, pet_img.PetFileUpload) // upload pet image
}

shelterRoute := v1.Group("/shelter")
{
shelterRoute.Get("", shelter.GetShelters) // shelter list
shelterRoute.Get("/:id", shelter.GetShelterById) // details
shelterRoute.Get("", shelter.GetShelters) // shelter list
shelterRoute.Get("/:id", middlewares.JWTMiddleware, shelter.GetShelterById) // details
}

// TODO: imprement all
likeRoute := v1.Group("/like")
likeRoute := v1.Group("/like", middlewares.JWTMiddleware)
{
likeRoute.Get("", like.List)
likeRoute.Post("/:pet_id", like.Add)
likeRoute.Delete("/:pet_id", like.Remove)
}

interestRoute := v1.Group("/interest")
interestRoute := v1.Group("/interest", middlewares.JWTMiddleware)
{
interestRoute.Get("", interest.List)
interestRoute.Post("/:pet_id", interest.Add)
Expand Down
6 changes: 5 additions & 1 deletion repository/interested.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ type InterestedImpl struct{}
type Interested struct {
PetID int `json:"pet_id" db:"pet_id"`
UserID uuid.UUID `json:"user_id" db:"user_id"`
Pet
}

func (i *InterestedImpl) ListByUserID(userID uuid.UUID) (output []Interested, err error) {
stm := `SELECT * FROM "interested" WHERE user_id = $1`
stm := `SELECT * FROM "interested" l
INNER JOIN "pet" p
ON l.pet_id = p.id
WHERE l.user_id = $1`
err = DB.Select(&output, stm, userID)
return
}
Expand Down
6 changes: 5 additions & 1 deletion repository/liked.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ type LikedImpl struct{}
type Liked struct {
PetID int `json:"pet_id" db:"pet_id"`
UserID uuid.UUID `json:"user_id" db:"user_id"`
Pet
}

func (i *LikedImpl) ListByUserID(userID uuid.UUID) (liked []Liked, err error) {
stm := `SELECT * FROM "liked" WHERE user_id = $1`
stm := `SELECT * FROM "liked" l
INNER JOIN "pet" p
ON l.pet_id = p.id
WHERE l.user_id = $1`
err = DB.Select(&liked, stm, userID)
return
}
Expand Down

0 comments on commit acbb8bf

Please sign in to comment.