Esempio n. 1
0
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
}
Esempio n. 2
0
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
}
Esempio n. 3
0
// 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
}
Esempio n. 4
0
// 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
}
Esempio n. 5
0
// 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
}
Esempio n. 6
0
// 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
}
Esempio n. 7
0
func FetchUserById(id string, DB *gorm.DB) *gorm.DB {
	__id, _ := utils.ParseId(id)
	return DB.Preload("Account").Where(gorm.Model{ID: __id}).First(&User{})
}
Esempio n. 8
0
func Fetch(DB *gorm.DB) *gorm.DB {
	var u []User
	return DB.Preload("Account").Find(&u)
}
Esempio n. 9
0
// 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")
}
Esempio n. 10
0
// 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")
}
Esempio n. 11
0
// 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
}
Esempio n. 12
0
// 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")
}
Esempio n. 13
0
// 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")
}
Esempio n. 14
0
// 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")
}
Esempio n. 15
0
// 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")
}
Esempio n. 16
0
// 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")
}