From c01679630357925cbc4f32912c48f926e043a4dd Mon Sep 17 00:00:00 2001 From: Howl Date: Fri, 8 Apr 2016 19:23:52 +0200 Subject: [PATCH] Friend deletion --- app/start.go | 2 ++ app/v1/friend.go | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/app/start.go b/app/start.go index 9aca04a..6f66fee 100644 --- a/app/start.go +++ b/app/start.go @@ -42,6 +42,8 @@ func Start(conf common.Conf, db *sql.DB) *gin.Engine { // Write privilege required gv1.POST("/friends/add", Method(v1.FriendsAddPOST, db, common.PrivilegeWrite)) gv1.GET("/friends/add/:id", Method(v1.FriendsAddGET, db, common.PrivilegeWrite)) + gv1.POST("/friends/del", Method(v1.FriendsDelPOST, db, common.PrivilegeWrite)) + gv1.GET("/friends/del/:id", Method(v1.FriendsDelGET, db, common.PrivilegeWrite)) // M E T A // E T "wow thats so meta" diff --git a/app/v1/friend.go b/app/v1/friend.go index cd9cced..950fbae 100644 --- a/app/v1/friend.go +++ b/app/v1/friend.go @@ -203,7 +203,7 @@ func addFriend(md common.MethodData, u int) (r common.Response) { } r.Code = 200 r.Data = friendsWithData{ - Friends: relExists, + Friends: true, Mutual: isMutual, } return @@ -217,3 +217,43 @@ func userExists(md common.MethodData, u int) (r bool) { } return } + +// FriendsDelGET is the GET version of FriendDelPOST. +func FriendsDelGET(md common.MethodData) common.Response { + uidS := md.C.Param("id") + uid, err := strconv.Atoi(uidS) + if err != nil { + return common.Response{ + Code: 400, + Message: "Nope. That's not a number.", + } + } + return delFriend(md, uid) +} + +// FriendsDelPOST allows for deleting friends. +func FriendsDelPOST(md common.MethodData) (r common.Response) { + d := friendAddPOSTData{} + err := json.Unmarshal(md.RequestData, &d) + if err != nil { + md.Err(err) + r = Err500 + return + } + return delFriend(md, d.UserID) +} + +func delFriend(md common.MethodData, u int) common.Response { + _, err := md.DB.Exec("DELETE FROM users_relationships WHERE user1 = ? AND user2 = ?", md.ID(), u) + if err != nil { + md.Err(err) + return Err500 + } + return common.Response{ + Code: 200, + Data: friendsWithData{ + Friends: false, + Mutual: false, + }, + } +}