Exemplo 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
}
Exemplo n.º 2
0
func (pa *PersonAddress) JoinWith(handler gorm.JoinTableHandlerInterface, db *gorm.DB, source interface{}) *gorm.DB {
	table := pa.Table(db)
	return db.Joins("INNER JOIN person_addresses ON person_addresses.address_id = addresses.id").Where(fmt.Sprintf("%v.deleted_at IS NULL OR %v.deleted_at <= '0001-01-02'", table, table))
}