func getUsersPost(uid string, allUsers bool, category int64, published string, db *gorm.DB) (posts []models.Post, err error) { db = db.Preload("Author").Order("created_at desc") if !allUsers { db = db.Where("author_id = ?", uid) } if category != 0 { db = db.Joins( "join post_categories as pc on posts.id = pc.post_id " + "join categories as c on c.id = pc.category_id") db = db.Where("c.id = ?", category) } published = strings.ToLower(published) if published != "all" && (published == "true" || published == "false") { pub := published == "true" db = db.Where("published = ?", pub) } // the Select call is needed because of a bug. // TODO: remove this call when the bug is fixed db = db.Select("posts.id, posts.title, posts.content, posts.published, posts.created_at, posts.updated_at, posts.author_id") if err = db.Find(&posts).Error; err != nil { return nil, err } return posts, nil }
func FacultiesDepartments(db *gorm.DB) ([]json_models.FacultiesDepartments, error) { var result []json_models.FacultiesDepartments err := db.Model(&models.Faculties{}).Select("id as faculty_id, short_name as faculty_name").Scan(&result).Error if err != nil { return nil, err } for i, faculty := range result { var records []models.Departments err := db.Preload("Faculty").Where("faculty_id = ?", faculty.FacultyId).Find(&records).Error if err != nil { return nil, err } if len(records) != 0 { for _, record := range records { val := json_models.Department{Id: record.Id, Name: record.Name} result[i].Departments = append(result[i].Departments, val) } } else { result[i].Departments = []json_models.Department{} } } return result, nil }
// find a service by id func FindServiceByObjectID(db *gorm.DB, id string) (Service, bool, error) { result := Service{} err := db.Preload("Identity").Where(&Service{ObjectID: id}).First(&result).Error if err != nil { if err == gorm.RecordNotFound { return result, false, nil } return result, false, err } return result, true, nil }
// find object by object id or by pin func FindObjectByObjectIDOrPin(db *gorm.DB, IDOrPin string) (Object, bool, error) { result := Object{} err := db.Preload("Service.Identity").Preload("Wallet.Identity").Or(&Object{ObjectID: IDOrPin}).Where(&Object{Pin: IDOrPin}).First(&result).Error if err != nil { if err == gorm.RecordNotFound { return result, false, nil } return result, false, err } return result, true, nil }
// find wallet by object id func FindWalletByObjectID(db *gorm.DB, objectID string) (Wallet, bool, error) { result := Wallet{} err := db.Preload("Identity").Where(&Wallet{ObjectID: objectID}).First(&result).Error if err != nil { if err == gorm.RecordNotFound { return result, false, nil } return result, false, err } return result, true, nil }
// find a service by it's client id func FindServiceByClientId(db *gorm.DB, clientId string) (Service, bool, error) { result := Service{} err := db.Preload("Identity").Where(&Service{ClientID: clientId}).First(&result).Error if err != nil { if err == gorm.RecordNotFound { return result, false, nil } else { return result, false, err } } return result, true, nil }
func FetchUserById(id string, DB *gorm.DB) *gorm.DB { __id, _ := utils.ParseId(id) return DB.Preload("Account").Where(gorm.Model{ID: __id}).First(&User{}) }
func Fetch(DB *gorm.DB) *gorm.DB { var u []User return DB.Preload("Account").Find(&u) }
// RefreshTokenPreloadWithPrefix sets up Gorm preloads for a refresh token object, // and prefixes with prefix for nested objects func RefreshTokenPreloadWithPrefix(db *gorm.DB, prefix string) *gorm.DB { return db. Preload(prefix + "Client").Preload(prefix + "User") }
// AuthorizationCodePreloadWithPrefix sets up Gorm preloads for an auth code object, // and prefixes with prefix for nested objects func AuthorizationCodePreloadWithPrefix(db *gorm.DB, prefix string) *gorm.DB { return db. Preload(prefix + "Client").Preload(prefix + "User") }
// find all objects contained in a list of object ids func FindAllObjectsByObjectID(db *gorm.DB, objects []string) ([]Object, error) { result := []Object{} return result, db.Preload("Service.Identity").Preload("Wallet.Identity").Where("object_id IN (?)", objects).Find(&result).Error }
// PasswordResetPreloadWithPrefix sets up Gorm preloads for a password reset object, // and prefixes with prefix for nested objects func PasswordResetPreloadWithPrefix(db *gorm.DB, prefix string) *gorm.DB { return db.Preload(prefix + "User.OauthUser") }
// InvitationPreloadWithPrefix sets up Gorm preloads for an invitation object, // and prefixes with prefix for nested objects func InvitationPreloadWithPrefix(db *gorm.DB, prefix string) *gorm.DB { return db.Preload(prefix + "InvitedUser.OauthUser"). Preload(prefix + "InvitedByUser.OauthUser") }
// ConfirmationPreloadWithPrefix sets up Gorm preloads for a confirmation object, // and prefixes with prefix for nested objects func ConfirmationPreloadWithPrefix(db *gorm.DB, prefix string) *gorm.DB { return db.Preload(prefix + "User.OauthUser") }
// UserPreloadWithPrefix sets up Gorm preloads for a user object, // and prefixes with prefix for nested objects func UserPreloadWithPrefix(db *gorm.DB, prefix string) *gorm.DB { return db.Preload(prefix + "Account.OauthClient"). Preload(prefix + "OauthUser") }
// AccountPreloadWithPrefix sets up Gorm preloads for an account object, and prefixes with prefix for nested objects func AccountPreloadWithPrefix(db *gorm.DB, prefix string) *gorm.DB { return db.Preload(prefix + "OauthClient") }