add notgroup function

This commit is contained in:
rick 2022-10-29 12:20:32 +02:00
parent 083278deb0
commit 11c3592164
Signed by: Rick
GPG key ID: 4A6223D66294EB20
2 changed files with 47 additions and 0 deletions

View file

@ -103,3 +103,29 @@ func GetGroupsUser(c *fiber.Ctx) error {
} }
} }
} }
// @Summary Renvoie la liste des groupes où ne se trouve pas un utilisateur
// @Produce json
// @Success 200 {array} models.Group "Liste des ids des groupes"
// @Failure 403
// @Failure 404
// @Failure 500
// @Router /users/{id}/not/groups [get]
func GetNotGroupsUser(c *fiber.Ctx) error {
token := checkCookie(c)
if token == "" {
return c.SendStatus(fiber.StatusForbidden)
} else {
id := c.Params("id")
groups, err := dao.GetGroupNotByMember(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.Status(fiber.StatusOK).JSON(groups)
}
}
}

View file

@ -45,6 +45,27 @@ func GetGroupByMember(id string) (group []models.Group, err error) {
} }
} }
func GetGroupNotByMember(id string) (group []models.Group, err error) {
db, _ := get()
objectId, err := primitive.ObjectIDFromHex(id)
defer disconnect(db.Client())
coll := db.Collection("groups")
cursor, err := coll.Find(context.TODO(), bson.D{{"members", bson.D{{"$ne", objectId}}}})
defer cursor.Close(context.TODO())
if err != nil {
return nil, err
}
err = cursor.All(context.TODO(), &group)
if err != nil {
return nil, err
} else {
return group, nil
}
}
func CheckMemberInGroup(idUser string, idGroup string) (ret bool, err error) { func CheckMemberInGroup(idUser string, idGroup string) (ret bool, err error) {
db, _ := get() db, _ := get()
userObject, err := primitive.ObjectIDFromHex(idUser) userObject, err := primitive.ObjectIDFromHex(idUser)