feat(API,DAO): leave and join group

This commit is contained in:
rick 2022-10-29 23:05:16 +02:00
parent 87483470b9
commit 0e7dde9a9f
Signed by: Rick
GPG Key ID: 4A6223D66294EB20
4 changed files with 71 additions and 4 deletions

View File

@ -81,7 +81,22 @@ func CreateGroup(c *fiber.Ctx) error {
// @Failure 404
// @Router /groups/join [put]
func JoinGroup(c *fiber.Ctx) error {
return nil
token := checkCookie(c)
if token == "" {
return c.SendStatus(fiber.StatusForbidden)
} else {
id := c.Params("id")
err := dao.AddMemberInGroup(token, id)
if err == mongo.ErrNoDocuments {
return c.SendStatus(fiber.StatusNotFound)
} else if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": fmt.Sprint(err),
})
} else {
return c.SendStatus(fiber.StatusOK)
}
}
}
// @Summary Quitte un groupe
@ -93,7 +108,22 @@ func JoinGroup(c *fiber.Ctx) error {
// @Failure 404
// @Router /groups/leave [put]
func LeaveGroupe(c *fiber.Ctx) error {
return nil
token := checkCookie(c)
if token == "" {
return c.SendStatus(fiber.StatusForbidden)
} else {
id := c.Params("id")
err := dao.RemoveMemberFromGroup(token, id)
if err == mongo.ErrNoDocuments {
return c.SendStatus(fiber.StatusNotFound)
} else if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": fmt.Sprint(err),
})
} else {
return c.SendStatus(fiber.StatusOK)
}
}
}
// @Summary Change le chef d'un groupe

View File

@ -86,3 +86,39 @@ func CheckMemberInGroup(idUser string, idGroup string) (ret bool, err error) {
return true, nil
}
}
func AddMemberInGroup(idUser string, idGroup string) error {
db, _ := get()
user, err := primitive.ObjectIDFromHex(idUser)
group, err := primitive.ObjectIDFromHex(idGroup)
defer disconnect(db.Client())
coll := db.Collection("groups")
update := bson.D{{"$push", bson.D{{"members", user}}}}
err = coll.FindOneAndUpdate(
context.TODO(),
bson.D{{"_id", group}},
update,
).Err()
return err
}
func RemoveMemberFromGroup(idUser string, idGroup string) error {
db, _ := get()
user, err := primitive.ObjectIDFromHex(idUser)
group, err := primitive.ObjectIDFromHex(idGroup)
defer disconnect(db.Client())
coll := db.Collection("groups")
update := bson.D{{"$pull", bson.D{{"members", user}}}}
err = coll.FindOneAndUpdate(
context.TODO(),
bson.D{{"_id", group}},
update,
).Err()
return err
}

View File

@ -10,4 +10,5 @@ func UserRoute(route fiber.Router) {
route.Get("/current", controllers.GetCurrentUser)
route.Get("/:id", controllers.GetUser)
route.Get("/:id/groups", controllers.GetGroupsUser)
route.Get("/:id/not/groups", controllers.GetNotGroupsUser)
}

View File

@ -10,8 +10,8 @@ func GroupRoute(route fiber.Router) {
route.Get("/:id", controllers.GetInfos)
route.Get("/:id/events", controllers.GetEvents)
route.Put("/create", controllers.CreateGroup)
route.Put("/join", controllers.JoinGroup)
route.Put("/leave", controllers.LeaveGroupe)
route.Put("/:id/join", controllers.JoinGroup)
route.Put("/:id/leave", controllers.LeaveGroupe)
route.Put("/switch", controllers.ChangeLeader)
route.Put("/events/plan", controllers.CreateEvent)
route.Put("/events/change", controllers.ChangeEvent)