feat(API,DAO): leave and join group
This commit is contained in:
parent
87483470b9
commit
0e7dde9a9f
4 changed files with 71 additions and 4 deletions
|
@ -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
|
||||
|
|
36
dao/group.go
36
dao/group.go
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue