7
7
"github.com/neatplex/nightell-core/internal/services/container"
8
8
"github.com/neatplex/nightell-core/internal/utils"
9
9
"net/http"
10
- "strconv"
11
10
)
12
11
13
12
func UsersShow (ctr * container.Container ) echo.HandlerFunc {
@@ -30,10 +29,26 @@ func UsersShow(ctr *container.Container) echo.HandlerFunc {
30
29
return errors .WithStack (err )
31
30
}
32
31
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
+
33
46
return ctx .JSON (http .StatusOK , map [string ]interface {}{
34
47
"user" : user ,
35
48
"followers_count" : followersCount ,
36
49
"followings_count" : followingsCount ,
50
+ "follows_me" : followsMe ,
51
+ "followed_by_me" : followedByMe ,
37
52
})
38
53
}
39
54
}
@@ -98,50 +113,44 @@ func UsersFollowings(ctr *container.Container) echo.HandlerFunc {
98
113
}
99
114
}
100
115
101
- func UsersFollowingsStore (ctr * container.Container ) echo.HandlerFunc {
116
+ func UsersFollowersStore (ctr * container.Container ) echo.HandlerFunc {
102
117
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 )
104
120
105
- if strconv . FormatUint ( user . ID , 10 ) == ctx . Param ( "followeeId" ) {
121
+ if authUser . ID == userId {
106
122
return ctx .JSON (http .StatusForbidden , map [string ]interface {}{
107
123
"message" : "You cannot follow yourself!" ,
108
124
})
109
125
}
110
126
111
- followee , err := ctr .UserService .FindBy ("id" , utils . StringToID ( ctx . Param ( "followeeId" ), 0 ) )
127
+ followee , err := ctr .UserService .FindBy ("id" , userId )
112
128
if err != nil {
113
129
return errors .WithStack (err )
114
130
}
115
131
if followee == nil {
116
132
return ctx .NoContent (http .StatusNotFound )
117
133
}
118
134
119
- if _ , err = ctr .FollowshipService .Create (followee .ID , user .ID ); err != nil {
135
+ if _ , err = ctr .FollowshipService .Create (followee .ID , authUser .ID ); err != nil {
120
136
return errors .WithStack (err )
121
137
}
122
138
123
139
return ctx .NoContent (http .StatusCreated )
124
140
}
125
141
}
126
142
127
- func UsersFollowingsDelete (ctr * container.Container ) echo.HandlerFunc {
143
+ func UsersFollowersDelete (ctr * container.Container ) echo.HandlerFunc {
128
144
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 )
136
147
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 )
140
149
if err != nil {
141
150
return errors .WithStack (err )
142
151
}
143
152
if followship == nil {
144
- return ctx .NoContent (http .StatusNotFound )
153
+ return ctx .NoContent (http .StatusNoContent )
145
154
}
146
155
147
156
err = ctr .FollowshipService .Delete (followship .ID )
0 commit comments