Ejemplo n.º 1
0
// Create inserts a new group into database
func Create(c *echo.Context) (int, interface{}) {
	digitsID, ok := c.Get("digitsID").(int64)
	if !ok {
		return msg.Forbidden("session required")
	}

	// Bind request body with group
	var group models.Group
	err := c.Bind(&group)

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

	// Set values
	group.ID = bson.NewObjectId()
	group.Admins = []int64{digitsID}
	group.Creator = digitsID
	group.Members = []int64{digitsID}

	// Create group
	err = mangos.Insert(constants.CGroups, group)
	if err != nil {
		return msg.InternalError(err)
	}

	return msg.Ok(group)
}
Ejemplo n.º 2
0
// Create inserts a new friendship in database
func Create(c *echo.Context) (int, interface{}) {
	digitsID, ok := c.Get("digitsID").(int64)
	if !ok {
		return msg.Forbidden("session required")
	}

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

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

	// Set internal fields
	friend.ID = bson.NewObjectId()
	friend.DigitsID = digitsID
	friend.CreatedAt = time.Now()

	// Insert into database
	err = mangos.Insert(constants.CFriends, friend)
	if err != nil {
		return msg.InternalError(err)
	}

	return msg.Ok(friend)
}
Ejemplo n.º 3
0
// ExchageDigits takes a digits auth and converts it into internal auth
func ExchageDigits(c *echo.Context) (int, interface{}) {
	// Get digits authoriation
	dauth, ok := c.Get("digitsAuth").(*models.DigitsAuth)
	if !ok {
		return msg.Forbidden("must include a digits authorization")
	}

	var device models.Device
	var err error

	// Bind body to device data
	err = c.Bind(&device)
	if err != nil {
		return msg.BadRequest(err)
	}

	// Valdate profile data
	err = validator.Validate(device)
	if err != nil {
		return msg.BadRequest(err)
	}

	// Create internal session
	device.CreatedAt = time.Now()
	toCreate := &models.HabloAuth{
		ID:       bson.NewObjectId(),
		DigitsID: dauth.ID,
		Token:    auth.GenerateToken(dauth),
		Enabled:  true,
		Expires:  false,
		Device:   device,
	}

	log.Printf("Awaiting for [HabloAuth = %s] inserction", toCreate.ID.Hex())
	err = mangos.Insert(collection, toCreate)
	if err != nil {
		log.Printf("Cannot insert [HabloAuth = %s]: %s", toCreate.ID.Hex(), err)
		return msg.InternalError(err)
	}

	log.Printf("Created and served HabloAuth ->\n%s", msg.Detail(toCreate.ID))
	return msg.Ok(toCreate)
}
Ejemplo n.º 4
0
// Create inserts a new valid profile into database.
func Create(c *echo.Context) (int, interface{}) {
	var profile models.Profile
	var err error

	// Get the digits authorization
	auth, ok := c.Get("digitsAuth").(*models.DigitsAuth)
	if !ok {
		return msg.Forbidden("invalid authorization")
	}

	// Bind body to profile data
	err = c.Bind(&profile)
	if err != nil {
		return msg.BadRequest(err)
	}

	// Valdate profile data
	err = validator.Validate(profile)
	if err != nil {
		return msg.BadRequest(err)
	}

	// New profiles always require confirmation
	profile.ID = auth.ID
	profile.RequiresConfirm = true
	profile.CreatedAt = time.Now()

	// Insert into database
	log.Printf("Awaiting for [Profile = %d] inserction", profile.ID)
	err = mangos.Insert(collection, &profile)
	if err != nil {
		log.Printf("Cannot insert [Profile = %d]: %s", profile.ID, err)
		return msg.InternalError(err)
	}

	// TODO: Schedule welcome message and stuff.
	// Done
	log.Printf("Created profile ->\n%s", msg.Detail(profile))
	return msg.Ok(profile)
}