Friend deletion

This commit is contained in:
Howl 2016-04-08 19:23:52 +02:00
parent 44d12d2493
commit c016796303
2 changed files with 43 additions and 1 deletions

View File

@ -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"

View File

@ -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,
},
}
}