Beispiel #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 := models.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)
}
Beispiel #2
0
func (t *TypesController) DeleteType(c *gin.Context) {
	appId := c.Param("appId")
	typeName := c.Param("typeName")

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

	d := db.NewAppsDbService(c.MustGet("user").(string))
	d.UpdateId(app["_id"],
		models.JSON{
			"$pull": models.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
	}
}
Beispiel #3
0
func (a *ApplicationController) DeleteApplicationHandler(c *gin.Context) {
	appId := c.MustGet("app").(models.JSON)["_id"]

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

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

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

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

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

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

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

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

	c.JSON(http.StatusOK, res)
}
Beispiel #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", models.JSON{}.FromMap(app))

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