当前位置: 首页>>代码示例>>Golang>>正文


Golang DB.Create方法代码示例

本文整理汇总了Golang中github.com/jinzhu/gorm.DB.Create方法的典型用法代码示例。如果您正苦于以下问题:Golang DB.Create方法的具体用法?Golang DB.Create怎么用?Golang DB.Create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/jinzhu/gorm.DB的用法示例。


在下文中一共展示了DB.Create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: PostLike

func PostLike(db gorm.DB, router *gin.Engine) {
	// POST /like
	// POST new like to database
	router.POST("/like", func(c *gin.Context) {
		var likeData model.LikeData
		if err := c.BindJSON(&likeData); err == nil {
			like := &model.Like{
				LikeData: likeData,
			}

			var x_user uint64
			if resp := c.Request.Header.Get(userHeader); resp != "" {
				conv_user, _ := strconv.ParseUint(resp, 10, 64)
				x_user = conv_user
				like.Trace.CreatedBy = x_user
				like.Trace.UpdatedBy = x_user
				if err := checkDataLike(like.LikeData); err {
					if err := db.Create(&like).Error; err != nil {
						c.AbortWithError(http.StatusBadRequest, err)
					} else {
						c.JSON(http.StatusCreated, like)
					}
				} else {
					c.AbortWithStatus(http.StatusBadRequest)
				}
			} else {
				c.AbortWithStatus(http.StatusForbidden)
			}

		} else {
			log.Print(err)
			c.AbortWithError(http.StatusBadRequest, err)
		}
	})
}
开发者ID:ali-abdalla,项目名称:humhub-api,代码行数:35,代码来源:like.go

示例2: newAccountHandler

//Adds a new account to the db. Passwords are generated using bcrypt
func newAccountHandler(req *http.Request, db *gorm.DB, rendr render.Render) {
	username := req.FormValue("username")
	password := req.FormValue("password")

	if username == "" {
		log.Println(http.StatusBadRequest, "Username blank")
		rendr.Text(http.StatusBadRequest, "Supply valid username")
		return
	}
	if password == "" {
		log.Println(http.StatusBadRequest, "Password blank")
		rendr.Text(http.StatusBadRequest, "Supply valid password")
		return
	}

	hash, err := bcrypt.GenerateFromPassword([]byte(password), 0)
	if err != nil {
		log.Println(err.Error())
		rendr.Text(http.StatusBadRequest, err.Error())
		return
	}

	acc := &account{Username: username, Password: hash, Funds: 0}
	err = db.Create(acc).Error
	if err != nil {
		log.Println(err.Error())
		rendr.Text(http.StatusBadRequest, err.Error())
		return
	}
	rendr.Text(http.StatusOK, "Account created")
}
开发者ID:danihodovic,项目名称:lendico,代码行数:32,代码来源:routes.go

示例3: PostProfileFieldCategory

func PostProfileFieldCategory(db gorm.DB, router *gin.Engine) {
	// POST /profile_field_category
	// POST new profile_field_category to database
	router.POST("/profile_field_category", func(c *gin.Context) {
		var profile_field_categoryData model.ProfileFieldCategoryData
		if err := c.BindJSON(&profile_field_categoryData); err == nil {
			profile_field_category := &model.ProfileFieldCategory{
				ProfileFieldCategoryData: profile_field_categoryData,
			}

			var x_user uint64
			if resp := c.Request.Header.Get(userHeader); resp != "" {
				conv_user, _ := strconv.ParseUint(resp, 10, 64)
				x_user = conv_user
				profile_field_category.Trace.CreatedBy = x_user
				profile_field_category.Trace.UpdatedBy = x_user
				if err := checkDataProfileFieldCategory(profile_field_category.ProfileFieldCategoryData); err {
					if err := db.Create(&profile_field_category).Error; err != nil {
						c.AbortWithError(http.StatusBadRequest, err)
					} else {
						c.JSON(http.StatusCreated, profile_field_category)
					}
				} else {
					c.AbortWithStatus(http.StatusBadRequest)
				}
			} else {
				c.AbortWithStatus(http.StatusForbidden)
			}

		} else {
			log.Print(err)
			c.AbortWithError(http.StatusBadRequest, err)
		}
	})
}
开发者ID:ali-abdalla,项目名称:humhub-api,代码行数:35,代码来源:profile_field_category.go

示例4: PostSpaceSetting

func PostSpaceSetting(db gorm.DB, router *gin.Engine) {
	// POST /space_setting
	// POST new space_setting to database
	router.POST("/space_setting", func(c *gin.Context) {
		var space_settingData model.SpaceSettingData
		if err := c.BindJSON(&space_settingData); err == nil {
			space_setting := &model.SpaceSetting{
				SpaceSettingData: space_settingData,
			}

			var x_user uint64
			if resp := c.Request.Header.Get(userHeader); resp != "" {
				conv_user, _ := strconv.ParseUint(resp, 10, 64)
				x_user = conv_user
				space_setting.Trace.CreatedBy = x_user
				space_setting.Trace.UpdatedBy = x_user
				if err := checkDataSpaceSetting(space_setting.SpaceSettingData); err {
					if err := db.Create(&space_setting).Error; err != nil {
						c.AbortWithError(http.StatusBadRequest, err)
					} else {
						c.JSON(http.StatusCreated, space_setting)
					}
				} else {
					c.AbortWithStatus(http.StatusBadRequest)
				}
			} else {
				c.AbortWithStatus(http.StatusForbidden)
			}

		} else {
			log.Print(err)
			c.AbortWithError(http.StatusBadRequest, err)
		}
	})
}
开发者ID:ali-abdalla,项目名称:humhub-api,代码行数:35,代码来源:space_setting.go

示例5: PostActivity

func PostActivity(db gorm.DB, router *gin.Engine) {
	// POST /activity
	// POST new activity to database
	router.POST("/activity", func(c *gin.Context) {
		var activityData model.ActivityData
		if err := c.BindJSON(&activityData); err == nil {
			activity := &model.Activity{
				ActivityData: activityData,
			}

			var x_user uint64
			if resp := c.Request.Header.Get(userHeader); resp != "" {
				conv_user, _ := strconv.ParseUint(resp, 10, 64)
				x_user = conv_user
				activity.Trace.CreatedBy = x_user
				activity.Trace.UpdatedBy = x_user
				if err := checkDataActivity(activity.ActivityData); err {
					if err := db.Create(&activity).Error; err != nil {
						c.AbortWithError(http.StatusBadRequest, err)
					} else {
						c.JSON(http.StatusCreated, activity)
					}
				} else {
					c.AbortWithStatus(http.StatusBadRequest)
				}
			} else {
				c.AbortWithStatus(http.StatusForbidden)
			}

		} else {
			log.Print(err)
			c.AbortWithError(http.StatusBadRequest, err)
		}
	})
}
开发者ID:ali-abdalla,项目名称:humhub-api,代码行数:35,代码来源:activity.go

示例6: PostWallEntry

func PostWallEntry(db gorm.DB, router *gin.Engine) {
	// POST /wall_entry
	// POST new wall_entry to database
	router.POST("/wall_entry", func(c *gin.Context) {
		var wall_entryData model.WallEntryData
		if err := c.BindJSON(&wall_entryData); err == nil {
			wall_entry := &model.WallEntry{
				WallEntryData: wall_entryData,
			}

			var x_user uint64
			if resp := c.Request.Header.Get(userHeader); resp != "" {
				conv_user, _ := strconv.ParseUint(resp, 10, 64)
				x_user = conv_user
				wall_entry.Trace.CreatedBy = x_user
				wall_entry.Trace.UpdatedBy = x_user
				if err := checkDataWallEntry(wall_entry.WallEntryData); err {
					if err := db.Create(&wall_entry).Error; err != nil {
						c.AbortWithError(http.StatusBadRequest, err)
					} else {
						c.JSON(http.StatusCreated, wall_entry)
					}
				} else {
					c.AbortWithStatus(http.StatusBadRequest)
				}
			} else {
				c.AbortWithStatus(http.StatusForbidden)
			}

		} else {
			log.Print(err)
			c.AbortWithError(http.StatusBadRequest, err)
		}
	})
}
开发者ID:ali-abdalla,项目名称:humhub-api,代码行数:35,代码来源:wall_entry.go

示例7: PostUserPassword

func PostUserPassword(db gorm.DB, router *gin.Engine) {
	// POST /user_password
	// POST new user_password to database
	router.POST("/user_password", func(c *gin.Context) {
		var user_passwordData model.UserPasswordData
		if err := c.BindJSON(&user_passwordData); err == nil {
			user_password := &model.UserPassword{
				UserPasswordData: user_passwordData,
			}

			var x_user uint64
			if resp := c.Request.Header.Get(userHeader); resp != "" {
				conv_user, _ := strconv.ParseUint(resp, 10, 64)
				x_user = conv_user
				user_password.Trace.CreatedBy = x_user
				user_password.Trace.UpdatedBy = x_user
				if err := checkDataUserPassword(user_password.UserPasswordData); err {
					if err := db.Create(&user_password).Error; err != nil {
						c.AbortWithError(http.StatusBadRequest, err)
					} else {
						c.JSON(http.StatusCreated, user_password)
					}
				} else {
					c.AbortWithStatus(http.StatusBadRequest)
				}
			} else {
				c.AbortWithStatus(http.StatusForbidden)
			}

		} else {
			log.Print(err)
			c.AbortWithError(http.StatusBadRequest, err)
		}
	})
}
开发者ID:ali-abdalla,项目名称:humhub-api,代码行数:35,代码来源:user_password.go

示例8: PostPost

func PostPost(db gorm.DB, router *gin.Engine) {
	// POST /post
	// POST new post to database
	router.POST("/post", func(c *gin.Context) {
		var postData model.PostData
		if err := c.BindJSON(&postData); err == nil {
			post := &model.Post{
				PostData: postData,
			}

			var x_user uint64
			if resp := c.Request.Header.Get(userHeader); resp != "" {
				conv_user, _ := strconv.ParseUint(resp, 10, 64)
				x_user = conv_user
				post.Trace.CreatedBy = x_user
				post.Trace.UpdatedBy = x_user
				if err := checkDataPost(post.PostData); err {
					if err := db.Create(&post).Error; err != nil {
						c.AbortWithError(http.StatusBadRequest, err)
					} else {
						c.JSON(http.StatusCreated, post)
					}
				} else {
					c.AbortWithStatus(http.StatusBadRequest)
				}
			} else {
				c.AbortWithStatus(http.StatusForbidden)
			}

		} else {
			log.Print(err)
			c.AbortWithError(http.StatusBadRequest, err)
		}
	})
}
开发者ID:ali-abdalla,项目名称:humhub-api,代码行数:35,代码来源:post.go

示例9: PostGroupAdmin

func PostGroupAdmin(db gorm.DB, router *gin.Engine) {
	// POST /group_admin
	// POST new group_admin to database
	router.POST("/group_admin", func(c *gin.Context) {
		var group_adminData model.GroupAdminData
		if err := c.BindJSON(&group_adminData); err == nil {
			group_admin := &model.GroupAdmin{
				GroupAdminData: group_adminData,
			}

			var x_user uint64
			if resp := c.Request.Header.Get(userHeader); resp != "" {
				conv_user, _ := strconv.ParseUint(resp, 10, 64)
				x_user = conv_user
				group_admin.Trace.CreatedBy = x_user
				group_admin.Trace.UpdatedBy = x_user
				if err := checkDataGroupAdmin(group_admin.GroupAdminData); err {
					if err := db.Create(&group_admin).Error; err != nil {
						c.AbortWithError(http.StatusBadRequest, err)
					} else {
						c.JSON(http.StatusCreated, group_admin)
					}
				} else {
					c.AbortWithStatus(http.StatusBadRequest)
				}
			} else {
				c.AbortWithStatus(http.StatusForbidden)
			}

		} else {
			log.Print(err)
			c.AbortWithError(http.StatusBadRequest, err)
		}
	})
}
开发者ID:ali-abdalla,项目名称:humhub-api,代码行数:35,代码来源:group_admin.go

示例10: AddLinkHandler

func AddLinkHandler(cfg Config, w http.ResponseWriter, r *http.Request, db *gorm.DB, cachechan chan string) bool {

	/* Parse the form and check the URL arguments */
	r.ParseForm()

	title := r.FormValue("title")
	url := r.FormValue("url")
	description := r.FormValue("description")
	password := r.FormValue("password")

	if len(title) == 0 || len(url) == 0 || len(description) == 0 || len(password) == 0 {
		/* Return an error page if we're missing a field */
		return false
	}

	if password != cfg.Post.Password {
		/* Bad password, so we should show an error page */
		return false
	}

	link := Link{Title: title, URL: url, Description: description, Active: true}
	db.Create(&link)

	/* Let's queue the link for caching */
	cachechan <- url

	return true
}
开发者ID:cmalekpour,项目名称:links,代码行数:28,代码来源:add_link_handler.go

示例11: PostNotification

func PostNotification(db gorm.DB, router *gin.Engine) {
	// POST /notification
	// POST new notification to database
	router.POST("/notification", func(c *gin.Context) {
		var notificationData model.NotificationData
		if err := c.BindJSON(&notificationData); err == nil {
			notification := &model.Notification{
				NotificationData: notificationData,
			}

			var x_user uint64
			if resp := c.Request.Header.Get(userHeader); resp != "" {
				conv_user, _ := strconv.ParseUint(resp, 10, 64)
				x_user = conv_user
				notification.Trace.CreatedBy = x_user
				notification.Trace.UpdatedBy = x_user
				if err := checkDataNotification(notification.NotificationData); err {
					if err := db.Create(&notification).Error; err != nil {
						c.AbortWithError(http.StatusBadRequest, err)
					} else {
						c.JSON(http.StatusCreated, notification)
					}
				} else {
					c.AbortWithStatus(http.StatusBadRequest)
				}
			} else {
				c.AbortWithStatus(http.StatusForbidden)
			}

		} else {
			log.Print(err)
			c.AbortWithError(http.StatusBadRequest, err)
		}
	})
}
开发者ID:ali-abdalla,项目名称:humhub-api,代码行数:35,代码来源:notification.go

示例12: createUserCommon

func (s *Service) createUserCommon(db *gorm.DB, username, password string) (*User, error) {
	// Start with a user without a password
	user := &User{
		Username: username,
		Password: util.StringOrNull(""),
	}

	// If the password is being set already, create a bcrypt hash
	if password != "" {
		passwordHash, err := pass.HashPassword(password)
		if err != nil {
			return nil, err
		}
		user.Password = util.StringOrNull(string(passwordHash))
	}

	// Check the username is available
	if s.UserExists(user.Username) {
		return nil, ErrUsernameTaken
	}

	// Create the user
	if err := db.Create(user).Error; err != nil {
		return nil, err
	}
	return user, nil
}
开发者ID:chenhougen,项目名称:go-oauth2-server,代码行数:27,代码来源:user.go

示例13: Set

func (p *SettingDao) Set(db *gorm.DB, key string, val interface{}, enc bool) error {
	dt, err := web.ToBits(val)
	if err != nil {
		db.Rollback()
		return err
	}

	if enc {
		dt, err = p.Aes.Encode(dt)
		if err != nil {
			db.Rollback()
			return err
		}
	}

	st := Setting{ID: key}
	var cn int
	db.Model(st).Count(&cn)
	if cn == 0 {
		st.Val = dt
		db.Create(&st)
	} else {
		db.Model(&st).Updates(Setting{Val: dt})
	}
	return nil
}
开发者ID:itpkg,项目名称:base,代码行数:26,代码来源:dao.go

示例14: NewSpendingType

// NewSpendingType Function
func NewSpendingType(f forms.AddSpendingTypeForm, DB *gorm.DB) (spendingType SpendingType, err []helper.ErrorMessage) {

	v := f.Validate()

	if v.HasErrors() {
		for _, value := range v.Errors {
			err = append(err, helper.ErrorMessage{
				Code:    409,
				Source:  helper.SourceErrors{Pointer: value.Key},
				Title:   value.Message,
				Details: value.Message,
			})
		}

		return spendingType, err
	}

	spendingType.Name = f.Data.Name
	spendingType.Description = f.Data.Description

	_err := DB.Create(&spendingType).Error
	if _err != nil {
		err = append(err, helper.ErrorMessage{
			Code:    409,
			Source:  helper.SourceErrors{},
			Title:   "Failed Creating New Spending Type",
			Details: _err.Error(),
		})

		return spendingType, err
	}

	return spendingType, nil
}
开发者ID:BugisDev,项目名称:SpendingTracker,代码行数:35,代码来源:spending.go

示例15: Bootstrap

// Bootstrap creates "migrations" table
// to keep track of already run database migrations
func Bootstrap(db *gorm.DB) error {
	migrationName := "bootstrap_migrations"

	migration := new(Migration)
	// Using Error instead of RecordNotFound because we want to check
	// if the migrations table exists. This is different from later migrations
	// where we query the already create migrations table.
	exists := nil == db.Where("name = ?", migrationName).First(migration).Error

	if exists {
		logger.Infof("Skipping %s migration", migrationName)
		return nil
	}

	logger.Infof("Running %s migration", migrationName)

	// Create migrations table
	if err := db.CreateTable(new(Migration)).Error; err != nil {
		return fmt.Errorf("Error creating migrations table: %s", db.Error)
	}

	// Save a record to migrations table,
	// so we don't rerun this migration again
	migration.Name = migrationName
	if err := db.Create(migration).Error; err != nil {
		return fmt.Errorf("Error saving record to migrations table: %s", err)
	}

	return nil
}
开发者ID:RichardKnop,项目名称:example-api,代码行数:32,代码来源:bootstrap.go


注:本文中的github.com/jinzhu/gorm.DB.Create方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。