Skip to content

Commit 66ae43b

Browse files
committed
update follower/following apis
1 parent 3f0b1df commit 66ae43b

File tree

4 files changed

+37
-24
lines changed

4 files changed

+37
-24
lines changed

internal/http/server/handlers/v1/users.go

+27-18
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/neatplex/nightell-core/internal/services/container"
88
"github.com/neatplex/nightell-core/internal/utils"
99
"net/http"
10-
"strconv"
1110
)
1211

1312
func UsersShow(ctr *container.Container) echo.HandlerFunc {
@@ -30,10 +29,26 @@ func UsersShow(ctr *container.Container) echo.HandlerFunc {
3029
return errors.WithStack(err)
3130
}
3231

32+
authUser := ctx.Get("user").(*models.User)
33+
34+
relation, err := ctr.FollowshipService.FindByIds(user.ID, authUser.ID)
35+
if err != nil {
36+
return errors.WithStack(err)
37+
}
38+
followsMe := relation != nil
39+
40+
relation, err = ctr.FollowshipService.FindByIds(authUser.ID, user.ID)
41+
if err != nil {
42+
return errors.WithStack(err)
43+
}
44+
followedByMe := relation != nil
45+
3346
return ctx.JSON(http.StatusOK, map[string]interface{}{
3447
"user": user,
3548
"followers_count": followersCount,
3649
"followings_count": followingsCount,
50+
"follows_me": followsMe,
51+
"followed_by_me": followedByMe,
3752
})
3853
}
3954
}
@@ -98,50 +113,44 @@ func UsersFollowings(ctr *container.Container) echo.HandlerFunc {
98113
}
99114
}
100115

101-
func UsersFollowingsStore(ctr *container.Container) echo.HandlerFunc {
116+
func UsersFollowersStore(ctr *container.Container) echo.HandlerFunc {
102117
return func(ctx echo.Context) error {
103-
user := ctx.Get("user").(*models.User)
118+
userId := utils.StringToID(ctx.Param("userId"), 0)
119+
authUser := ctx.Get("user").(*models.User)
104120

105-
if strconv.FormatUint(user.ID, 10) == ctx.Param("followeeId") {
121+
if authUser.ID == userId {
106122
return ctx.JSON(http.StatusForbidden, map[string]interface{}{
107123
"message": "You cannot follow yourself!",
108124
})
109125
}
110126

111-
followee, err := ctr.UserService.FindBy("id", utils.StringToID(ctx.Param("followeeId"), 0))
127+
followee, err := ctr.UserService.FindBy("id", userId)
112128
if err != nil {
113129
return errors.WithStack(err)
114130
}
115131
if followee == nil {
116132
return ctx.NoContent(http.StatusNotFound)
117133
}
118134

119-
if _, err = ctr.FollowshipService.Create(followee.ID, user.ID); err != nil {
135+
if _, err = ctr.FollowshipService.Create(followee.ID, authUser.ID); err != nil {
120136
return errors.WithStack(err)
121137
}
122138

123139
return ctx.NoContent(http.StatusCreated)
124140
}
125141
}
126142

127-
func UsersFollowingsDelete(ctr *container.Container) echo.HandlerFunc {
143+
func UsersFollowersDelete(ctr *container.Container) echo.HandlerFunc {
128144
return func(ctx echo.Context) error {
129-
user := ctx.Get("user").(*models.User)
130-
131-
if strconv.FormatUint(user.ID, 10) == ctx.Param("followeeId") {
132-
return ctx.JSON(http.StatusForbidden, map[string]interface{}{
133-
"message": "You cannot follow yourself!",
134-
})
135-
}
145+
userId := utils.StringToID(ctx.Param("userId"), 0)
146+
authUser := ctx.Get("user").(*models.User)
136147

137-
followship, err := ctr.FollowshipService.FindByIds(
138-
user.ID, utils.StringToID(ctx.Param("followeeId"), 0),
139-
)
148+
followship, err := ctr.FollowshipService.FindByIds(authUser.ID, userId)
140149
if err != nil {
141150
return errors.WithStack(err)
142151
}
143152
if followship == nil {
144-
return ctx.NoContent(http.StatusNotFound)
153+
return ctx.NoContent(http.StatusNoContent)
145154
}
146155

147156
err = ctr.FollowshipService.Delete(followship.ID)

internal/http/server/routes.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ func (s *Server) registerRoutes() {
3838
private.DELETE("profile", v1.ProfileDelete(s.container))
3939
// users
4040
private.GET("users/:userId", v1.UsersShow(s.container))
41-
private.GET("users/:userId/followers", v1.UsersFollowers(s.container))
4241
private.GET("users/:userId/followings", v1.UsersFollowings(s.container))
43-
private.POST("users/:userId/followings/:followeeId", v1.UsersFollowingsStore(s.container))
44-
private.DELETE("users/:userId/followings/:followeeId", v1.UsersFollowingsDelete(s.container))
42+
private.GET("users/:userId/followers", v1.UsersFollowers(s.container))
43+
private.POST("users/:userId/followers", v1.UsersFollowersStore(s.container))
44+
private.DELETE("users/:userId/followers", v1.UsersFollowersDelete(s.container))
4545
// posts
4646
private.GET("users/:userId/posts", v1.PostsIndex(s.container))
4747
private.POST("posts", v1.PostsStore(s.container))

internal/models/followship.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import "time"
44

55
type Followship struct {
66
ID uint64 `gorm:"primaryKey" json:"id"`
7-
FollowerID uint64 `json:"follower_id"`
7+
FollowerID uint64 `gorm:"index:idx_follower_id_followee_id,priority:1" json:"follower_id"`
88
Follower *User `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"follower"`
9-
FolloweeID uint64 `json:"followee_id"`
9+
FolloweeID uint64 `gorm:"index:idx_follower_id_followee_id,priority:2" json:"followee_id"`
1010
Followee *User `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"followee"`
1111
CreatedAt time.Time `json:"created_at"`
1212
}

internal/services/followship/service.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/cockroachdb/errors"
55
"github.com/neatplex/nightell-core/internal/database"
66
"github.com/neatplex/nightell-core/internal/models"
7+
"gorm.io/gorm"
78
)
89

910
type Service struct {
@@ -15,7 +16,10 @@ func (s *Service) FindByIds(followerID, followeeID uint64) (*models.Followship,
1516
r := s.database.Handler().
1617
Where("follower_id = ?", followerID).
1718
Where("followee_id = ?", followeeID).
18-
Find(&followship)
19+
First(&followship)
20+
if r.Error != nil && errors.Is(r.Error, gorm.ErrRecordNotFound) {
21+
return nil, nil
22+
}
1923
return &followship, errors.Wrapf(r.Error, "followerId: %v, followeeId: %v", followerID, followeeID)
2024
}
2125

0 commit comments

Comments
 (0)