Skip to content

Commit

Permalink
feat: unfollow user
Browse files Browse the repository at this point in the history
  • Loading branch information
Woynert committed Oct 25, 2023
1 parent 9a2db22 commit 84f9066
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
23 changes: 23 additions & 0 deletions controller/friends.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,26 @@ func (ctrl *Controller) FollowUser(req model.ReqFollowUser, userId uuid.UUID) er

return nil
}

func (ctrl *Controller) UnFollowUser(req model.ReqFollowUser, userId uuid.UUID) error {

// write

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

query := `
DELETE FROM followers WHERE follower_user_uuid = $1 AND followed_user_uuid = $2
`
_, err := ctrl.DB.ExecContext(
ctx,
query,
userId,
req.OtherUserId,
)
if err != nil {
return err
}

return nil
}
18 changes: 18 additions & 0 deletions docs/bruno-users/friends/unfollow.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
meta {
name: unfollow
type: http
seq: 3
}

post {
url: {{BASE_URL}}/{{API_PATH}}/user/follow
body: json
auth: none
}

body:json {
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOiIyMDIzLTEwLTI1VDE3OjA2OjUyLjg0MDc1MjMyMi0wNTowMCIsIm5iZiI6IjIwMjMtMTAtMjVUMTY6NTY6NTIuODQwNzUwMDk0LTA1OjAwIiwidXNlcmlkIjoiMGVhNWRiNTAtYWJlYS00YzNiLTg0NDQtZGMxZTg5MjFkMWU2In0.I2P5NfVd9EYQwjHk45BfyHO7iI5tL2lshL8p6wKVXCg",
"otheruserid" : "0818f7a7-e8df-44c3-a981-52dd6150f2ab"
}
}
33 changes: 33 additions & 0 deletions router/friends.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,36 @@ func (r *Router) FollowUser(ctx *gin.Context) {

ctx.JSON(http.StatusOK, gin.H{"msg": "Following user"})
}

func (r *Router) UnFollowUser(ctx *gin.Context) {
// validate

var req model.ReqFollowUser
if err := ctx.BindJSON(&req); err != nil {
log.Println(err)
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.MsgBadRequest())
return
}
if err := r.validator.Struct(req); err != nil {
ctx.JSON(http.StatusBadRequest, model.MsgValidationErr(err.Error()))
return
}

// validate access token

err, claims := ValidateToken(req.Token, []byte(config.Cfg.Secret))
if err != nil {
log.Println(err)
ctx.AbortWithStatusJSON(http.StatusUnauthorized,
gin.H{"message": "Invalid access token"})
return
}
userId := claims.UserID

if err := r.ctrl.UnFollowUser(req, userId); err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.MsgIntServerErr())
return
}

ctx.JSON(http.StatusOK, gin.H{"msg": "User unfollowed"})
}
1 change: 1 addition & 0 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func SetupRouter(ctrl *controller.Controller) Router {
user.POST("/register_teacher", r.RegisterTeacher)
user.POST("/search", r.SearchUser)
user.POST("/follow", r.FollowUser)
user.POST("/unfollow", r.UnFollowUser)

sess := base.Group("/session")
sess.POST("/login", r.Login)
Expand Down

0 comments on commit 84f9066

Please sign in to comment.