Example #1
0
func (a *ApplicationController) CreateApplicationHandler(c *gin.Context) {
	body := &ApplicationModel{}

	if err := c.Bind(body); err != nil {
		RestError(c, err)
		return
	}

	if body.Name == "" {
		RestErrorInvalidBody(c)
		return
	}

	d := db.NewAppsDbService(c.MustGet("user").(string))

	username := c.MustGet("user").(string)
	doc := JSON{
		"name":      body.Name,
		"owner":     username,
		"types":     []string{"users"},
		"createdAt": time.Now(),
		"masterKey": utils.GetCleanUUID(),
	}

	if err := d.Insert(doc); err != nil {
		RestError(c, err)
		return
	}

	RespondId(doc["_id"], c)
}
Example #2
0
func (t *TypesController) DeleteType(c *gin.Context) {
	appId := c.Param("appId")
	typeName := c.Param("typeName")

	app := c.MustGet("app").(JSON)

	d := db.NewAppsDbService(c.MustGet("user").(string))
	d.UpdateId(app["_id"],
		JSON{
			"$pull": JSON{
				"types": typeName,
			},
		},
	)

	database := db.NewTypeDbService(appId, typeName)
	session, collection := database.GetCollection()
	defer session.Close()

	dropError := collection.DropCollection()

	if dropError != nil {
		RestError(c, dropError)
		return
	}
}
Example #3
0
func (a *ApplicationController) DeleteApplicationHandler(c *gin.Context) {
	appId := c.MustGet("app").(JSON)["_id"]

	d := db.NewAppsDbService(c.MustGet("user").(string))
	err := d.RemoveId(appId)

	if err != nil {
		RestError(c, err)
		return
	}
}
Example #4
0
func (t *TypesController) CreateTypeHandler(c *gin.Context) {
	body := utils.GetBody(c)
	typeName := body["name"]

	app := c.MustGet("app").(JSON)

	d := db.NewAppsDbService(c.MustGet("user").(string))
	d.UpdateId(app["_id"],
		JSON{
			"$push": JSON{
				"types": typeName,
			},
		},
	)
}
Example #5
0
func (a *ApplicationController) UpdateApplicationHandler(c *gin.Context) {
	appId := c.MustGet("app").(JSON)["_id"]
	d := db.NewAppsDbService(c.MustGet("user").(string))
	doc := utils.WhitelistFields([]string{"name"}, utils.GetBody(c))

	err := d.Update(JSON{
		"_id": appId,
	}, JSON{
		"$set": doc,
	})

	if err != nil {
		RestError(c, err)
		return
	}
}
Example #6
0
func (a *ApplicationController) GetApplicationsHandler(c *gin.Context) {
	user := c.MustGet("user").(string)
	d := db.NewAppsDbService(user)

	res, err := d.Find(
		JSON{
			"owner": user,
		},
		JSON{
			"name": 1,
		},
	)

	if err != nil {
		RestError(c, err)
		return
	}

	c.JSON(http.StatusOK, res)
}
Example #7
0
func injectAppMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		appId := c.Param("appId")

		if appId != "" {
			//TODO: cache this
			d := db.NewAppsDbService(c.MustGet("user").(string))
			app, err := d.FindId(appId, nil)
			if err != nil {
				RestErrorAppNotFound(c)
				return
			}

			c.Set("app", JSON{}.FromMap(app))

		} else {
			RestError(c, "Invalid app id.")
		}
	}
}