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 // @Failure 404
// @Router /groups/join [put] // @Router /groups/join [put]
func JoinGroup(c *fiber.Ctx) error { 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 // @Summary Quitte un groupe
@ -93,7 +108,22 @@ func JoinGroup(c *fiber.Ctx) error {
// @Failure 404 // @Failure 404
// @Router /groups/leave [put] // @Router /groups/leave [put]
func LeaveGroupe(c *fiber.Ctx) error { 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 // @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 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("/current", controllers.GetCurrentUser)
route.Get("/:id", controllers.GetUser) route.Get("/:id", controllers.GetUser)
route.Get("/:id/groups", controllers.GetGroupsUser) 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", controllers.GetInfos)
route.Get("/:id/events", controllers.GetEvents) route.Get("/:id/events", controllers.GetEvents)
route.Put("/create", controllers.CreateGroup) route.Put("/create", controllers.CreateGroup)
route.Put("/join", controllers.JoinGroup) route.Put("/:id/join", controllers.JoinGroup)
route.Put("/leave", controllers.LeaveGroupe) route.Put("/:id/leave", controllers.LeaveGroupe)
route.Put("/switch", controllers.ChangeLeader) route.Put("/switch", controllers.ChangeLeader)
route.Put("/events/plan", controllers.CreateEvent) route.Put("/events/plan", controllers.CreateEvent)
route.Put("/events/change", controllers.ChangeEvent) route.Put("/events/change", controllers.ChangeEvent)