From 4266307727d51d737ba5db21b2fa27d62f22e501 Mon Sep 17 00:00:00 2001
From: Sercan Yesildal <sercan.yesildal@gmail.com>
Date: Mon, 10 Jul 2023 12:48:02 +0200
Subject: [PATCH] relationships updates

---
 service/planner/db/client.go     | 13 +++++++++++--
 service/planner/handler/group.go |  9 +++++++--
 service/planner/main.go          |  4 ++--
 service/planner/service/group.go |  4 ++--
 4 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/service/planner/db/client.go b/service/planner/db/client.go
index bbc2275..6dc7b5e 100644
--- a/service/planner/db/client.go
+++ b/service/planner/db/client.go
@@ -479,6 +479,15 @@ func CreateUserGroupRelation(userId int, groupId int) error {
 	}
 	defer udb.Close()
 
+	queryCheck, err := udb.Query("SELECT * FROM userGroupRelation WHERE userId = ? AND groupId = ?;", userId, groupId)
+	if err != nil {
+		return err
+	}
+	defer queryCheck.Close()
+	if queryCheck.Next() {
+		return errors.New("account already in group")
+	}
+
 	query, err := udb.Query("INSERT INTO userGroupRelation (userId, groupId) VALUES (?, ?);", userId, groupId)
 	if err != nil {
 		return err
@@ -488,14 +497,14 @@ func CreateUserGroupRelation(userId int, groupId int) error {
 	return nil
 }
 
-func DeleteUserGroupRelation(id int) error {
+func DeleteUserGroupRelation(userId int, groupId int) error {
 	udb, err := sql.Open(driverName, cfg.FormatDSN())
 	if err != nil {
 		return err
 	}
 	defer udb.Close()
 
-	query, err := udb.Query("DELETE FROM userGroupRelation WHERE id = ?;", id)
+	query, err := udb.Query("DELETE FROM userGroupRelation WHERE userId = ? AND groupId = ?;", userId, groupId)
 	if err != nil {
 		return err
 	}
diff --git a/service/planner/handler/group.go b/service/planner/handler/group.go
index 8ccc943..89ee73d 100644
--- a/service/planner/handler/group.go
+++ b/service/planner/handler/group.go
@@ -88,12 +88,17 @@ func CreateUserGroupRelation(w http.ResponseWriter, r *http.Request) {
 }
 
 func DeleteUserGroupRelation(w http.ResponseWriter, r *http.Request) {
-	id, err := getId(r)
+	userId, err := getUserId(r)
+	if err != nil {
+		http.Error(w, err.Error(), http.StatusBadRequest)
+		return
+	}
+	groupId, err := getGroupId(r)
 	if err != nil {
 		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
-	err = service.DeleteUserGroupRelation(id)
+	err = service.DeleteUserGroupRelation(userId, groupId)
 	if err != nil {
 		http.Error(w, err.Error(), http.StatusInternalServerError)
 		return
diff --git a/service/planner/main.go b/service/planner/main.go
index 30f0199..3a429a5 100644
--- a/service/planner/main.go
+++ b/service/planner/main.go
@@ -42,10 +42,10 @@ func main() {
 	router.HandleFunc("/user/{id}", handler.DeleteUser).Methods(http.MethodDelete)
 	router.HandleFunc("/user", handler.GetUsers).Methods(http.MethodGet)
 	router.HandleFunc("/user", handler.CreateUser).Methods(http.MethodPost)
-	router.HandleFunc("/group/{groupId}/{userId}", handler.CreateUserGroupRelation).Methods(http.MethodPost)
-	router.HandleFunc("/group/{groupId}/{userId}", handler.DeleteUserGroupRelation).Methods(http.MethodDelete)
 	router.HandleFunc("/group/meal/{id}", handler.DeleteGroupMeal).Methods(http.MethodDelete)
 	router.HandleFunc("/group/meal", handler.CreateGroupMeal).Methods(http.MethodPost)
+	router.HandleFunc("/group/{groupId}/{userId}", handler.CreateUserGroupRelation).Methods(http.MethodPost)
+	router.HandleFunc("/group/{groupId}/{userId}", handler.DeleteUserGroupRelation).Methods(http.MethodDelete)
 	router.HandleFunc("/group/{id}", handler.GetGroup).Methods(http.MethodGet)
 	router.HandleFunc("/group/{id}", handler.DeleteGroup).Methods(http.MethodDelete)
 	router.HandleFunc("/group", handler.GetGroups).Methods(http.MethodGet)
diff --git a/service/planner/service/group.go b/service/planner/service/group.go
index f84b542..6046f33 100644
--- a/service/planner/service/group.go
+++ b/service/planner/service/group.go
@@ -50,8 +50,8 @@ func CreateUserGroupRelation(userId int, groupId int) error {
 	return nil
 }
 
-func DeleteUserGroupRelation(id int) error {
-	err := db.DeleteUserGroupRelation(id)
+func DeleteUserGroupRelation(userId int, groupId int) error {
+	err := db.DeleteUserGroupRelation(userId, groupId)
 	if err != nil {
 		return err
 	}
-- 
GitLab