Esempio n. 1
1
// Update inserts a new friendship in database
func Update(c *echo.Context) (int, interface{}) {
	digitsID, ok := c.Get("digitsID").(int64)
	if !ok {
		return msg.Forbidden("session required")
	}

	friendID, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		return msg.BadRequest(err)
	}

	// Bind body request to friend
	var friend models.FriendUpdate
	err = c.Bind(&friend)
	if err != nil {
		return msg.BadRequest(err)
	}

	// Validate input
	err = validator.Validate(friend)
	if err != nil {
		return msg.BadRequest(err)
	}

	// Insert into database
	err = mangos.Update(constants.CFriends, bson.M{"friend_id": int64(friendID), "digits_id": digitsID}, friend)
	if err != nil {
		return msg.InternalError(err)
	}

	return msg.Ok(friend)
}
Esempio n. 2
0
// DelAdmin removes a member from an existing group
func DelAdmin(c *echo.Context) (int, interface{}) {
	digitsID, ok := c.Get("digitsID").(int64)
	if !ok {
		return msg.Forbidden("session required")
	}

	// Get group id and convert from string to objectId
	rawMID := c.Param("mid")
	if !bson.IsObjectIdHex(rawMID) {
		return msg.BadRequest("bad id: not a ObjectId")
	}

	// find the group
	memberID := bson.ObjectIdHex(rawMID)

	// Get group id and convert from string to objectId
	rawGID := c.Param("gid")
	if !bson.IsObjectIdHex(rawGID) {
		return msg.BadRequest("bad id: not a ObjectId")
	}

	// find the group
	groupID := bson.ObjectIdHex(rawGID)
	err := mangos.Update(constants.CGroups, bson.M{
		"$and": []bson.M{
			bson.M{"_id": groupID},
			bson.M{"admins": digitsID},
		},
	}, bson.M{"$pull": bson.M{"admins": memberID}})
	if err != nil {
		return msg.InternalError(err)
	}

	return msg.Ok("deleted admin")
}
Esempio n. 3
0
// Update patches a group in database
func Update(c *echo.Context) (int, interface{}) {
	digitsID, ok := c.Get("digitsID").(int64)
	if !ok {
		return msg.Forbidden("session required")
	}

	// Bind request body to a group update
	var group models.GroupUpdate
	err := c.Bind(&group)
	if err != nil {
		return msg.BadRequest(err)
	}

	// Get group id and convert from string to objectId
	rawGID := c.Param("gid")
	if !bson.IsObjectIdHex(rawGID) {
		return msg.BadRequest("bad id: not a ObjectId")
	}

	// Update the object, only if its a admin
	groupID := bson.ObjectIdHex(rawGID)
	err = mangos.Update(constants.CGroups, bson.M{
		"$and": []bson.M{
			bson.M{"_id": groupID},
			bson.M{"admins": digitsID},
		},
	}, bson.M{"$set": group})
	if err != nil {
		return msg.InternalError(err)
	}

	return msg.Ok(group)
}
Esempio n. 4
0
// Delete resets the settings for the user
func Delete(c *echo.Context) (int, interface{}) {
	digitsID, ok := c.Get("digitsID").(int64)
	if !ok {
		return msg.Forbidden("not a digitsID")
	}

	enabled := true
	defaults := models.SettingsUpdate{
		GlobalPushEnabled: &enabled,
		PushSound:         "",
	}
	err := mangos.Update(constants.CSettings, bson.M{"_id": digitsID}, bson.M{"$set": defaults})
	if err != nil {
		return msg.InternalError(err)
	}

	return msg.Ok("done")
}
Esempio n. 5
0
// Update sets the settings for the user
func Update(c *echo.Context) (int, interface{}) {
	digitsID, ok := c.Get("digitsID").(int64)
	if !ok {
		return msg.Forbidden("not a digitsID")
	}

	var settings models.SettingsUpdate
	err := c.Bind(&settings)
	if err != nil {
		return msg.BadRequest(err)
	}

	log.Printf("Awaiting for Settings[ ID=%d ] get", digitsID)
	err = mangos.Update(constants.CSettings, bson.M{"_id": digitsID}, bson.M{"$set": &settings})
	if err != nil {
		return msg.InternalError(err)
	}

	return msg.Ok("done")
}