func AuthenticateUser(db *gorm.DB) martini.Handler { return func(res http.ResponseWriter, req *http.Request, c martini.Context) { token := req.Header.Get(ApiTokenHeaderKey) var authUser models.AuthUser if token != "" { var err error authUser.User, err = models.GetUserByToken(db, token) if err == nil { authUser.SetAuthenticated(true) } else if err != gorm.RecordNotFound { utils.HttpError(res, http.StatusInternalServerError) } } c.Map(authUser) } }
func (this *Syncs) Create(req *http.Request, authUser models.AuthUser, render render.Render) { syncs := []models.Sync{} if decode(req, render, &syncs) != nil { return } tx := this.db.Begin() userIds := utils.Set{} for _, sync := range syncs { sync.UserId = authUser.Id if q := tx.Save(&sync); q.Error != nil { tx.Rollback() render.Error(500) return } for _, syncUser := range sync.SyncUsers { syncUser.SyncId = sync.Id if syncUser.UserId != authUser.Id { userIds.Insert(syncUser.UserId) } if q := tx.Save(&syncUser); q.Error != nil { tx.Rollback() render.Error(500) return } } } if q := tx.Commit(); q.Error != nil { tx.Rollback() render.Error(500) return } go this.sendPushNotifications(userIds.ToSlice()) if authUser.FetchSyncs(this.db) != nil { render.Error(500) return } render.JSON(http.StatusOK, map[string]interface{}{"results": authUser.Syncs}) }
func (this *Syncs) Index(authUser models.AuthUser, render render.Render) { if authUser.FetchSyncs(this.db) != nil { render.Error(500) } render.JSON(200, map[string]interface{}{"results": authUser.Syncs}) }