func PutProfileFieldCategory(db gorm.DB, router *gin.Engine) { // PUT /profile_field_category // Update profile_field_category data by id router.PUT("/profile_field_category/:id", func(c *gin.Context) { _id := c.Param("id") if id, err := strconv.ParseUint(_id, 10, 64); err == nil { var profile_field_categoryData model.ProfileFieldCategoryData if err := c.BindJSON(&profile_field_categoryData); err == nil { profile_field_category := &model.ProfileFieldCategory{ ProfileFieldCategoryData: profile_field_categoryData, ProfileFieldCategoryId: model.ProfileFieldCategoryId{Id: id}, } if err := checkDataProfileFieldCategory(profile_field_category.ProfileFieldCategoryData); err { checkProfileFieldCategory := &model.ProfileFieldCategory{ ProfileFieldCategoryData: profile_field_categoryData, ProfileFieldCategoryId: model.ProfileFieldCategoryId{Id: id}, } if err := db.First(checkProfileFieldCategory).Error; err == nil { db.Save(&profile_field_category) c.JSON(http.StatusOK, profile_field_category) } else { c.AbortWithStatus(http.StatusNotFound) } } else { c.AbortWithStatus(http.StatusBadRequest) } } } else { log.Print(err) c.AbortWithError(http.StatusBadRequest, err) } }) }
func PutNotification(db gorm.DB, router *gin.Engine) { // PUT /notification // Update notification data by id router.PUT("/notification/:id", func(c *gin.Context) { _id := c.Param("id") if id, err := strconv.ParseUint(_id, 10, 64); err == nil { var notificationData model.NotificationData if err := c.BindJSON(¬ificationData); err == nil { notification := &model.Notification{ NotificationData: notificationData, NotificationId: model.NotificationId{Id: id}, } if err := checkDataNotification(notification.NotificationData); err { checkNotification := &model.Notification{ NotificationData: notificationData, NotificationId: model.NotificationId{Id: id}, } if err := db.First(checkNotification).Error; err == nil { db.Save(¬ification) c.JSON(http.StatusOK, notification) } else { c.AbortWithStatus(http.StatusNotFound) } } else { c.AbortWithStatus(http.StatusBadRequest) } } } else { log.Print(err) c.AbortWithError(http.StatusBadRequest, err) } }) }
func PutUserHttpSession(db gorm.DB, router *gin.Engine) { // PUT /user_http_session // Update user_http_session data by id router.PUT("/user_http_session/:id", func(c *gin.Context) { _id := c.Param("id") if id, err := strconv.ParseUint(_id, 10, 64); err == nil { var user_http_sessionData model.UserHttpSessionData if err := c.BindJSON(&user_http_sessionData); err == nil { user_http_session := &model.UserHttpSession{ UserHttpSessionData: user_http_sessionData, UserHttpSessionId: model.UserHttpSessionId{Id: id}, } if err := checkDataUserHttpSession(user_http_session.UserHttpSessionData); err { checkUserHttpSession := &model.UserHttpSession{ UserHttpSessionData: user_http_sessionData, UserHttpSessionId: model.UserHttpSessionId{Id: id}, } if err := db.First(checkUserHttpSession).Error; err == nil { db.Save(&user_http_session) c.JSON(http.StatusOK, user_http_session) } else { c.AbortWithStatus(http.StatusNotFound) } } else { c.AbortWithStatus(http.StatusBadRequest) } } } else { log.Print(err) c.AbortWithError(http.StatusBadRequest, err) } }) }
func PutLike(db gorm.DB, router *gin.Engine) { // PUT /like // Update like data by id router.PUT("/like/:id", func(c *gin.Context) { _id := c.Param("id") if id, err := strconv.ParseUint(_id, 10, 64); err == nil { var likeData model.LikeData if err := c.BindJSON(&likeData); err == nil { like := &model.Like{ LikeData: likeData, LikeId: model.LikeId{Id: id}, } if err := checkDataLike(like.LikeData); err { checkLike := &model.Like{ LikeData: likeData, LikeId: model.LikeId{Id: id}, } if err := db.First(checkLike).Error; err == nil { db.Save(&like) c.JSON(http.StatusOK, like) } else { c.AbortWithStatus(http.StatusNotFound) } } else { c.AbortWithStatus(http.StatusBadRequest) } } } else { log.Print(err) c.AbortWithError(http.StatusBadRequest, err) } }) }
func PutContent(db gorm.DB, router *gin.Engine) { // PUT /content // Update content data by id router.PUT("/content/:id", func(c *gin.Context) { _id := c.Param("id") if id, err := strconv.ParseUint(_id, 10, 64); err == nil { var contentData model.ContentData if err := c.BindJSON(&contentData); err == nil { content := &model.Content{ ContentData: contentData, ContentId: model.ContentId{Id: id}, } if err := checkDataContent(content.ContentData); err { checkContent := &model.Content{ ContentData: contentData, ContentId: model.ContentId{Id: id}, } if err := db.First(checkContent).Error; err == nil { db.Save(&content) c.JSON(http.StatusOK, content) } else { c.AbortWithStatus(http.StatusNotFound) } } else { c.AbortWithStatus(http.StatusBadRequest) } } } else { log.Print(err) c.AbortWithError(http.StatusBadRequest, err) } }) }
func PutModuleEnabled(db gorm.DB, router *gin.Engine) { // PUT /module_enabled // Update module_enabled data by id router.PUT("/module_enabled/:id", func(c *gin.Context) { _id := c.Param("id") if id, err := strconv.ParseUint(_id, 10, 64); err == nil { var module_enabledData model.ModuleEnabledData if err := c.BindJSON(&module_enabledData); err == nil { module_enabled := &model.ModuleEnabled{ ModuleEnabledData: module_enabledData, ModuleEnabledId: model.ModuleEnabledId{Id: id}, } if err := checkDataModuleEnabled(module_enabled.ModuleEnabledData); err { checkModuleEnabled := &model.ModuleEnabled{ ModuleEnabledData: module_enabledData, ModuleEnabledId: model.ModuleEnabledId{Id: id}, } if err := db.First(checkModuleEnabled).Error; err == nil { db.Save(&module_enabled) c.JSON(http.StatusOK, module_enabled) } else { c.AbortWithStatus(http.StatusNotFound) } } else { c.AbortWithStatus(http.StatusBadRequest) } } } else { log.Print(err) c.AbortWithError(http.StatusBadRequest, err) } }) }
func PutWallEntry(db gorm.DB, router *gin.Engine) { // PUT /wall_entry // Update wall_entry data by id router.PUT("/wall_entry/:id", func(c *gin.Context) { _id := c.Param("id") if id, err := strconv.ParseUint(_id, 10, 64); err == nil { var wall_entryData model.WallEntryData if err := c.BindJSON(&wall_entryData); err == nil { wall_entry := &model.WallEntry{ WallEntryData: wall_entryData, WallEntryId: model.WallEntryId{Id: id}, } if err := checkDataWallEntry(wall_entry.WallEntryData); err { checkWallEntry := &model.WallEntry{ WallEntryData: wall_entryData, WallEntryId: model.WallEntryId{Id: id}, } if err := db.First(checkWallEntry).Error; err == nil { db.Save(&wall_entry) c.JSON(http.StatusOK, wall_entry) } else { c.AbortWithStatus(http.StatusNotFound) } } else { c.AbortWithStatus(http.StatusBadRequest) } } } else { log.Print(err) c.AbortWithError(http.StatusBadRequest, err) } }) }
func PutPost(db gorm.DB, router *gin.Engine) { // PUT /post // Update post data by id router.PUT("/post/:id", func(c *gin.Context) { _id := c.Param("id") if id, err := strconv.ParseUint(_id, 10, 64); err == nil { var postData model.PostData if err := c.BindJSON(&postData); err == nil { post := &model.Post{ PostData: postData, PostId: model.PostId{Id: id}, } if err := checkDataPost(post.PostData); err { checkPost := &model.Post{ PostData: postData, PostId: model.PostId{Id: id}, } if err := db.First(checkPost).Error; err == nil { db.Save(&post) c.JSON(http.StatusOK, post) } else { c.AbortWithStatus(http.StatusNotFound) } } else { c.AbortWithStatus(http.StatusBadRequest) } } } else { log.Print(err) c.AbortWithError(http.StatusBadRequest, err) } }) }
func PutActivity(db gorm.DB, router *gin.Engine) { // PUT /activity // Update activity data by id router.PUT("/activity/:id", func(c *gin.Context) { _id := c.Param("id") if id, err := strconv.ParseUint(_id, 10, 64); err == nil { var activityData model.ActivityData if err := c.BindJSON(&activityData); err == nil { activity := &model.Activity{ ActivityData: activityData, ActivityId: model.ActivityId{Id: id}, } if err := checkDataActivity(activity.ActivityData); err { checkActivity := &model.Activity{ ActivityData: activityData, ActivityId: model.ActivityId{Id: id}, } if err := db.First(checkActivity).Error; err == nil { db.Save(&activity) c.JSON(http.StatusOK, activity) } else { c.AbortWithStatus(http.StatusNotFound) } } else { c.AbortWithStatus(http.StatusBadRequest) } } } else { log.Print(err) c.AbortWithError(http.StatusBadRequest, err) } }) }
func PutGroupAdmin(db gorm.DB, router *gin.Engine) { // PUT /group_admin // Update group_admin data by id router.PUT("/group_admin/:id", func(c *gin.Context) { _id := c.Param("id") if id, err := strconv.ParseUint(_id, 10, 64); err == nil { var group_adminData model.GroupAdminData if err := c.BindJSON(&group_adminData); err == nil { group_admin := &model.GroupAdmin{ GroupAdminData: group_adminData, GroupAdminId: model.GroupAdminId{Id: id}, } if err := checkDataGroupAdmin(group_admin.GroupAdminData); err { checkGroupAdmin := &model.GroupAdmin{ GroupAdminData: group_adminData, GroupAdminId: model.GroupAdminId{Id: id}, } if err := db.First(checkGroupAdmin).Error; err == nil { db.Save(&group_admin) c.JSON(http.StatusOK, group_admin) } else { c.AbortWithStatus(http.StatusNotFound) } } else { c.AbortWithStatus(http.StatusBadRequest) } } } else { log.Print(err) c.AbortWithError(http.StatusBadRequest, err) } }) }
func PutUrlOembed(db gorm.DB, router *gin.Engine) { // PUT /url_oembed // Update url_oembed data by id router.PUT("/url_oembed/:id", func(c *gin.Context) { _id := c.Param("id") if id, err := strconv.ParseUint(_id, 10, 64); err == nil { var url_oembedData model.UrlOembedData if err := c.BindJSON(&url_oembedData); err == nil { url_oembed := &model.UrlOembed{ UrlOembedData: url_oembedData, UrlOembedId: model.UrlOembedId{Id: id}, } if err := checkDataUrlOembed(url_oembed.UrlOembedData); err { checkUrlOembed := &model.UrlOembed{ UrlOembedData: url_oembedData, UrlOembedId: model.UrlOembedId{Id: id}, } if err := db.First(checkUrlOembed).Error; err == nil { db.Save(&url_oembed) c.JSON(http.StatusOK, url_oembed) } else { c.AbortWithStatus(http.StatusNotFound) } } else { c.AbortWithStatus(http.StatusBadRequest) } } } else { log.Print(err) c.AbortWithError(http.StatusBadRequest, err) } }) }
func PutSpaceSetting(db gorm.DB, router *gin.Engine) { // PUT /space_setting // Update space_setting data by id router.PUT("/space_setting/:id", func(c *gin.Context) { _id := c.Param("id") if id, err := strconv.ParseUint(_id, 10, 64); err == nil { var space_settingData model.SpaceSettingData if err := c.BindJSON(&space_settingData); err == nil { space_setting := &model.SpaceSetting{ SpaceSettingData: space_settingData, SpaceSettingId: model.SpaceSettingId{Id: id}, } if err := checkDataSpaceSetting(space_setting.SpaceSettingData); err { checkSpaceSetting := &model.SpaceSetting{ SpaceSettingData: space_settingData, SpaceSettingId: model.SpaceSettingId{Id: id}, } if err := db.First(checkSpaceSetting).Error; err == nil { db.Save(&space_setting) c.JSON(http.StatusOK, space_setting) } else { c.AbortWithStatus(http.StatusNotFound) } } else { c.AbortWithStatus(http.StatusBadRequest) } } } else { log.Print(err) c.AbortWithError(http.StatusBadRequest, err) } }) }
func PutFile(db gorm.DB, router *gin.Engine) { // PUT /file // Update file data by id router.PUT("/file/:id", func(c *gin.Context) { _id := c.Param("id") if id, err := strconv.ParseUint(_id, 10, 64); err == nil { var fileData model.FileData if err := c.BindJSON(&fileData); err == nil { file := &model.File{ FileData: fileData, FileId: model.FileId{Id: id}, } if err := checkDataFile(file.FileData); err { checkFile := &model.File{ FileData: fileData, FileId: model.FileId{Id: id}, } if err := db.First(checkFile).Error; err == nil { db.Save(&file) c.JSON(http.StatusOK, file) } else { c.AbortWithStatus(http.StatusNotFound) } } else { c.AbortWithStatus(http.StatusBadRequest) } } } else { log.Print(err) c.AbortWithError(http.StatusBadRequest, err) } }) }
func ProjectGet(db gorm.DB, r render.Render, params martini.Params) { var project models.Project if err := db.First(&project, params["id"]).Error; err != nil { r.JSON(http.StatusNotFound, map[string]interface{}{"error": "Project not found"}) return } r.JSON(http.StatusOK, project) }
func findTodo(db *gorm.DB, predicate *todo) (*todo, error) { t := new(todo) err := db.First(t, predicate).Error if err != nil { return nil, err } return t, nil }
func MediaDelete(db gorm.DB, r render.Render, params martini.Params) { var media models.Media if err := db.First(&media, params["id"]).Error; err != nil { r.JSON(http.StatusNotFound, map[string]interface{}{"error": "Media not found"}) return } db.Delete(media) r.JSON(http.StatusNoContent, nil) }
func MediaHead(db gorm.DB, r render.Render, params martini.Params) { media := models.Media{} id, _ := strconv.Atoi(params["id"]) if err := db.First(&media, id).Error; err != nil { r.Error(http.StatusNotFound) return } r.Redirect(media.Url) }
func GroupDelete(db gorm.DB, r render.Render, params martini.Params) { var group models.Group if err := db.First(&group, params["id"]).Error; err != nil { r.JSON(http.StatusNotFound, map[string]interface{}{"error": "Group not found"}) return } db.Delete(group) r.JSON(http.StatusNoContent, nil) }
func naiveMethod(db *gorm.DB, num int, done chan bool) { for i := 0; i < num; i++ { var tt TestTable panicOnError(db.First(&tt, 1).Error) tt.Counter -= 1 panicOnError(db.Save(&tt).Error) } done <- true }
func ormTest(db gorm.DB) []model.User { user := model.User{Name: "Jinzhu", Age: 18, Birthday: time.Now()} db.NewRecord(user) // => returns `true` if primary key is blank db.Create(&user) db.NewRecord(user) // => return `false` after `user` created users := []model.User{} db.First(&users) return users }
func GroupUpdate(db gorm.DB, r render.Render, params martini.Params, updatedGroup models.Group) { var group models.Group if err := db.First(&group, params["id"]).Error; err != nil { r.JSON(http.StatusNotFound, map[string]interface{}{"error": "Group not found"}) return } db.Model(&group).Update(&updatedGroup) r.JSON(http.StatusOK, group) }
func MediaUpdate(db gorm.DB, r render.Render, params martini.Params, updatedMedia models.Media) { var media models.Media if err := db.First(&media, params["id"]).Error; err != nil { r.JSON(http.StatusNotFound, map[string]interface{}{"error": "Media not found"}) return } db.Model(&media).Update(&updatedMedia) r.JSON(http.StatusOK, media) }
func MediaPlay(db gorm.DB, r render.Render, params martini.Params) { media := models.Media{} id, _ := strconv.Atoi(params["id"]) if err := db.First(&media, id).Error; err != nil { r.Error(http.StatusNotFound) return } db.Model(&media).Update(&models.Media{Played: media.Played + 1}) r.Redirect(media.Url) }
// BeforeUpdate ensures that friendship balance is kept in sync func (t *Transaction) BeforeUpdate(db *gorm.DB) (err error) { if t.RelatedObjectType != "Friendship" { return } var curTransaction Transaction db.First(&curTransaction, t.ID) ReverseTransaction(&curTransaction, db) // Now the AfterSave callback will use the new updated transaction // and update the balance accordingly return }
// GetComment returns one comment func GetComment(params martini.Params, r render.Render, db *gorm.DB) { id := params["id"] var comment Comment db.First(&comment, id) if comment.ID == 0 { r.JSON(404, Error{404, "Comment was not found."}) } r.JSON(200, comment) }
// GetSelection returns one selection func GetSelection(params martini.Params, r render.Render, db *gorm.DB) { id := params["id"] var selection Selection db.First(&selection, id) if selection.ID == 0 { r.JSON(404, Error{404, "Selection was not found."}) } r.JSON(200, selection) }
// GetSummariesByBranch retrieves the the Summary by repo and branch func GetSummariesByBranch(db *gorm.DB, repoID string, branchID string) []Summary { var repo repo.Repository db.First(&repo, repoID) var sums []Summary // Latest summary db.Where(&Summary{ BranchID: branchID, Repository: repo, }).Order("summary_id").Find(&sums) return sums }
// UpdateTorrentStats Handles updating statistics relevant to our tracker. func UpdateTorrentStats(dbConn *gorm.DB, seederDelta int64, leecherDelta int64) { dbConn = assertOpenConnection(dbConn) t := &schemas.Torrent{} dbConn.First(&t) dbConn.Model(&t).Updates( schemas.TrackerStats{ Uploaded: t.Seeders + seederDelta, Downloaded: t.Leechers + leecherDelta, }) return }
// UpdateStats Handles updating statistics relevant to our tracker. func UpdateStats(dbConn *gorm.DB, uploaded uint64, downloaded uint64) { dbConn = assertOpenConnection(dbConn) ts := &schemas.TrackerStats{} dbConn.First(&ts) dbConn.Model(&ts).Updates( schemas.TrackerStats{ Uploaded: ts.Uploaded + int64(uploaded), Downloaded: ts.Downloaded + int64(downloaded), }) return }
// UpdatePeerStats handles updating peer info like hits per ip, downloaded // amount, uploaded amounts. func UpdatePeerStats(dbConn *gorm.DB, uploaded uint64, downloaded uint64, ip string) { dbConn = assertOpenConnection(dbConn) ps := &schemas.PeerStats{Ip: ip} dbConn.First(&ps) fmt.Printf("%v", *ps) dbConn.Model(&ps).UpdateColumn(map[string]interface{}{ "Uploaded": ps.Uploaded + int64(uploaded), "Downloaded": ps.Downloaded + int64(downloaded), }) return }