Golang Context.MustGet Examples

Golang Context.MustGet - 30 examples found. These are the top rated real world Golang examples of github.com/gin-gonic/gin.Context.MustGet extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
func (t *TypesController) GetTypeDataHandler(c *gin.Context) {
	appId := c.Param("appId")
	typeName := c.Param("typeName")

	app := c.MustGet("app").(JSON)
	types := app["types"].([]interface{})
	found := false

	for _, t := range types {
		if value, ok := t.(string); ok && value == typeName {
			found = true
			break
		}
	}

	if !found {
		RestErrorNotFound(c)
		return
	}

	d := db.NewTypeDbService(appId, typeName)

	typeData, err := d.Find(nil, nil)

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

	c.JSON(http.StatusOK, typeData)
}
Example #2
0
func EditReviewFormR(c *gin.Context) {
	// dependency injection
	var (
		reviewStore = c.MustGet(ReviewStoreKey).(ReviewStore)
		user        = GetUser(c)
	)

	// only authenticated users can perform this action
	if !user.Authenticated() {
		c.AbortWithStatus(http.StatusUnauthorized)
		return
	}

	// fetch review
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		c.AbortWithError(http.StatusBadRequest, err)
		return
	}

	review, err := reviewStore.GetReview(id)
	if err != nil {
		c.AbortWithError(http.StatusInternalServerError, err)
		return
	}

	c.HTML(http.StatusOK, "review-form.html", gin.H{
		"Action": "Edit",
		"User":   user,
		"Form":   review,
		"Errors": map[string]string{},
	})
}
Example #3
0
func DeleteUserInstance(c *gin.Context) {
	db := c.MustGet("db").(gorm.DB)
	consumer := c.MustGet("consumer").(models.User)

	id := c.Params.ByName("id")

	var user models.User

	db.First(&user, id)

	if user.Username == "" {
		response := make(map[string]string)
		response["error"] = "Resource not found."
		c.JSON(404, response)
	} else {
		if (consumer.Admin) || (user.Id == consumer.Id) {
			db.Delete(&user)
			if user.Id != consumer.Id {
				if user.Gravatar == "" {
					user.Gravatar = user.Email
				}
				user.Email = ""
			}
			user.Password = ""
			c.JSON(200, user)
		} else {
			response := make(map[string]string)
			response["error"] = "Invalid credentials."
			c.JSON(401, response)
		}
	}
}
Example #4
0
func WhiskyListR(c *gin.Context) {
	// dependency injection
	var (
		whiskyStore = c.MustGet(WhiskyStoreKey).(WhiskyStore)
		user        = GetUser(c)
	)

	// get whiskies
	list, err := whiskyStore.GetAllWhiskies()
	if err != nil {
		c.AbortWithError(http.StatusInternalServerError, err)
		return
	}

	// transform list into alphabet map
	whiskies := map[string][]Whisky{}
	for _, whisky := range list {
		group := strings.ToUpper(whisky.Distillery[0:1])
		whiskies[group] = append(whiskies[group], whisky)
	}

	c.HTML(http.StatusOK, "list.html", gin.H{
		"User":     user,
		"Whiskies": whiskies,
	})
}
Example #5
0
func UserListR(c *gin.Context) {
	// dependency injection
	var (
		userStore = c.MustGet(UserStoreKey).(UserStore)
		user      = GetUser(c)
	)

	// get users
	list, err := userStore.GetAllUser()
	if err != nil {
		c.AbortWithError(http.StatusInternalServerError, err)
		return
	}

	// transform list into alphabet map
	users := map[string][]User{}
	for _, user := range list {
		group := strings.ToUpper(user.Name[0:1])
		users[group] = append(users[group], user)
	}

	c.HTML(http.StatusOK, "users.html", gin.H{
		"User":  user,
		"Users": users,
	})
}
Example #6
0
func UpdateTemplate(c *gin.Context) {
	oldTemplate := c.MustGet("template").(models.Template)

	var template models.Template
	if err := c.Bind(&template); err != nil {
		return
	}

	if _, err := database.Mysql.Exec("update project__template set ssh_key_id=?, inventory_id=?, repository_id=?, environment_id=?, playbook=?, arguments=?, override_args=? where id=?", template.SshKeyID, template.InventoryID, template.RepositoryID, template.EnvironmentID, template.Playbook, template.Arguments, template.OverrideArguments, oldTemplate.ID); err != nil {
		panic(err)
	}

	desc := "Template ID " + strconv.Itoa(template.ID) + " updated"
	objType := "template"
	if err := (models.Event{
		ProjectID:   &oldTemplate.ProjectID,
		Description: &desc,
		ObjectID:    &oldTemplate.ID,
		ObjectType:  &objType,
	}.Insert()); err != nil {
		panic(err)
	}

	c.AbortWithStatus(204)
}