Skip to content

Commit

Permalink
Merge pull request #118 from traP-jp/fix/favoritecount
Browse files Browse the repository at this point in the history
favorite count in user handler
  • Loading branch information
kavos113 authored Sep 4, 2024
2 parents 9222037 + 36e7608 commit aefc31d
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/handler/userHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ func (h *Handler) GetUserWikiHandelr(c echo.Context) error {
tagsRes = append(tagsRes, tag.TagName)
}

var favorites int
err = h.db.Get(&favorites, "SELECT COUNT(*) FROM favorites WHERE wiki_id = ?", wiki.ID)
if err != nil {
log.Printf("failed to get favorites: %v", err)
return c.JSON(http.StatusInternalServerError, err)
}

res = append(res, model.WikiContentResponse{
ID: wiki.ID,
Type: wiki.Type,
Expand All @@ -56,6 +63,7 @@ func (h *Handler) GetUserWikiHandelr(c echo.Context) error {
UpdatedAt: wiki.UpdatedAt,
OwnerTraqID: wiki.OwnerTraqID,
Tags: tagsRes,
Favorites: favorites,
})
}

Expand Down Expand Up @@ -90,6 +98,13 @@ func (h *Handler) GetUserFavoriteWikiHandler(c echo.Context) error {
tagsRes = append(tagsRes, tag.TagName)
}

var favorites int
err = h.db.Get(&favorites, "SELECT COUNT(*) FROM favorites WHERE wiki_id = ?", wiki.ID)
if err != nil {
log.Printf("failed to get favorites: %v", err)
return c.JSON(http.StatusInternalServerError, err)
}

res = append(res, model.WikiContentResponse{
ID: wiki.ID,
Type: wiki.Type,
Expand All @@ -99,6 +114,7 @@ func (h *Handler) GetUserFavoriteWikiHandler(c echo.Context) error {
UpdatedAt: wiki.UpdatedAt,
OwnerTraqID: wiki.OwnerTraqID,
Tags: tagsRes,
Favorites: favorites,
})
}

Expand Down Expand Up @@ -172,6 +188,7 @@ func (h *Handler) PostUserFavoriteWikiHandler(c echo.Context) error {
UpdatedAt: wiki.UpdatedAt,
OwnerTraqID: wiki.OwnerTraqID,
Tags: tagsRes,
Favorites: 0,
}

return c.JSON(http.StatusOK, res)
Expand Down Expand Up @@ -228,6 +245,13 @@ func (h *Handler) DeleteUserFavoriteWikiHandler(c echo.Context) error {
tagsRes = append(tagsRes, tag.TagName)
}

var favorites int
err = h.db.Get(&favorites, "SELECT COUNT(*) FROM favorites WHERE wiki_id = ?", wiki.ID)
if err != nil {
log.Printf("failed to get favorites: %v", err)
return c.JSON(http.StatusInternalServerError, err)
}

res := model.WikiContentResponse{
ID: wiki.ID,
Type: wiki.Type,
Expand All @@ -237,6 +261,7 @@ func (h *Handler) DeleteUserFavoriteWikiHandler(c echo.Context) error {
UpdatedAt: wiki.UpdatedAt,
OwnerTraqID: wiki.OwnerTraqID,
Tags: tagsRes,
Favorites: favorites,
}

_, err = h.db.Exec("DELETE FROM favorites WHERE user_traq_id = ? AND wiki_id = ?", user.TraqID, wikiID)
Expand Down

0 comments on commit aefc31d

Please sign in to comment.