Golang Context.BindWith Examples

Golang Context.BindWith - 30 examples found. These are the top rated real world Golang examples of github.com/gin-gonic/gin.Context.BindWith extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
// EmailVerification verifies an email of user.
func EmailVerification(c *gin.Context) (int, error) {
	var user model.User
	var verifyEmailForm VerifyEmailForm
	c.BindWith(&verifyEmailForm, binding.Form)
	log.Debugf("verifyEmailForm.ActivationToken : %s", verifyEmailForm.ActivationToken)
	if db.ORM.Where(&model.User{ActivationToken: verifyEmailForm.ActivationToken}).First(&user).RecordNotFound() {
		return http.StatusNotFound, errors.New("User is not found.")
	}
	isExpired := timeHelper.IsExpired(user.ActivateUntil)
	log.Debugf("passwordResetUntil : %s", user.ActivateUntil.UTC())
	log.Debugf("expired : %t", isExpired)
	if isExpired {
		return http.StatusForbidden, errors.New("token not valid.")
	}
	user.ActivationToken = ""
	user.ActivateUntil = time.Now()
	user.ActivatedAt = time.Now()
	user.Activation = true
	status, err := UpdateUserCore(&user)
	if err != nil {
		return status, err
	}
	status, err = SetCookie(c, user.Token)
	return status, err
}
Example #2
0
File: file.go Project: tka/goyangi
// CreateFiles creates files.
func CreateFiles(c *gin.Context) (int, error) {
	var forms FilesForm
	start := time.Now()
	c.BindWith(&forms, binding.JSON)
	log.Debugf("CreateFiles c form : %v", forms)

	user, _ := userService.CurrentUser(c)
	sqlStrBuffer := new(bytes.Buffer)
	stringHelper.Concat(sqlStrBuffer, "INSERT INTO file(user_id, name, size, created_at) VALUES ")
	values := []interface{}{}
	for _, file := range forms.Files {
		stringHelper.Concat(sqlStrBuffer, "(?, ?, ?, ?),")
		values = append(values, user.Id, file.Name, file.Size, time.Now())

	}
	// sqlStrBuffer.Truncate(sqlStrBuffer.Len() - 1) is slower than slice.
	if len(values) > 0 {
		sqlStr := sqlStrBuffer.String()
		sqlStr = sqlStr[0 : len(sqlStr)-1]
		log.Debugf("sqlStr for File : %s", sqlStr)
		db.ORM.Exec(sqlStr, values...)
		elapsed := time.Since(start)
		log.Debugf("CreateFiles elapsed : %s", elapsed)
	}

	return http.StatusCreated, nil
}
Example #3
0
func (s *Server) CommentDeleteHandler(c *gin.Context) {
	var form struct {
		Entry   string `form:"entry" binding:"required"`
		Comment string `form:"comment" binding:"required"`
	}
	c.BindWith(&form, binding.Form)

	// TODO: check perm
	profile, _ := s.CurrentUser(c)
	graph, _ := s.CurrentGraph(c)
	req := &pb.CommentDeleteRequest{
		Entry:   form.Entry,
		Comment: form.Comment,
		User:    profile.Id,
	}

	ctx, cancel := DefaultTimeoutContext()
	defer cancel()

	entry, err := s.client.DeleteComment(ctx, req)
	if RequestError(c, err) {
		return
	}

	entry.FormatComments(int32(0))
	entry.RebuildCommentsCommand(profile, graph)
	c.JSON(200, entry.Comments)
}
Example #4
0
func addUser(c *gin.Context) {
	var (
		user     User
		err      error
		passHash []byte
	)
	err = c.BindWith(&user, binding.JSON)
	if err != nil {
		c.JSON(404, "Couldn't bind!")
	} else {
		if db.NewRecord(user) {
			user.CreatedAt = time.Now().UTC()
			user.UpdatedAt = time.Now().UTC()

			// Create password hash
			originalPass := []byte(user.PasswordHash)
			if passHash, err = bcrypt.GenerateFromPassword(originalPass, bcrypt.DefaultCost); err != nil {
				fmt.Printf("%v", err)
			}
			user.PasswordHash = string(passHash)

			db.Create(&user)
			c.JSON(201, user)
		} else {
			c.JSON(400, "This user already exist")
		}
	}
}
func (ac *AdminController) SaveBlogEditCtr(c *gin.Context) {
	session := sessions.Default(c)
	username := session.Get("username")
	if username == nil {
		(&umsg{"You have no permission", "/"}).ShowMessage(c)
		return
	}
	var BI EditBlogItem
	c.BindWith(&BI, binding.Form)
	if BI.Aid == "" {
		(&umsg{"Can not find the blog been edit", "/"}).ShowMessage(c)
		return
	}
	if BI.Title == "" {
		(&umsg{"Title can not empty", "/"}).ShowMessage(c)
		return
	}
	if BI.Content == "" {
		(&umsg{"Content can not empty", "/"}).ShowMessage(c)
		return
	}
	_, err := DB.Exec("update top_article set title=?, content=? where aid = ?", BI.Title, BI.Content, BI.Aid)
	if err == nil {
		Cache = lru.New(8192)
		(&umsg{"Success", "/"}).ShowMessage(c)
	} else {
		(&umsg{"Failed to save blog", "/"}).ShowMessage(c)
	}

}
Example #6
0
func commonDeploy(ginCtx *gin.Context) (DeployRequest, error) {
	ginCtx.Request.ParseForm()
	var givenDeploy DeployRequest
	ginCtx.BindWith(&givenDeploy, binding.JSON)
	glog.Infof("given %+v", givenDeploy)
	return givenDeploy, nil
}