示例#1
0
// addSkip adds skip parameter into sql query
func addSkip(query *gorm.DB, skip int) *gorm.DB {
	if skip > 0 {
		return query.Offset(skip)
	}

	return query
}
示例#2
0
func AllMediaForDate(db gorm.DB, page, year, month, day int) []Media {
	var media []Media
	offset := (page - 1) * ResultsPerRequest
	scope := db.Offset(offset).Limit(ResultsPerRequest)

	if year > 0 {
		scope = scope.Where("date_part('year', taken_at) = ?", year)

		if month > 0 {
			scope = scope.Where("date_part('month', taken_at) = ?", month)

			if day > 0 {
				scope = scope.Where("date_part('day', taken_at) = ?", day)
			}
		}
	}
	scope.Find(&media)
	return media
}
示例#3
0
func getJobsFromDB(db gorm.DB, start, limit int) (map[string]interface{}, int, error) {
	jobs := []application.Job{}
	jobsMap := make(map[string]interface{})
	var total int
	dbQuery := db.Offset(start)
	if limit > 0 {
		dbQuery = dbQuery.Limit(limit)
	}
	if err := dbQuery.Find(&jobs).Error; err != nil {
		return jobsMap, total, err
	}

	for _, job := range jobs {
		id := strconv.FormatUint(uint64(job.ID), 10)
		jobsMap[id] = job
	}

	if err := db.Find(&[]application.Job{}).Count(&total).Error; err != nil {
		return jobsMap, total, err
	}

	return jobsMap, total, nil
}
示例#4
0
func getFreelancersFromDB(db gorm.DB, start, limit int) (map[string]interface{}, int, error) {
	freelancersMap := make(map[string]interface{})
	freelancers := []application.Freelancer{}
	var total int
	dbQuery := db.Offset(start)
	if limit > 0 {
		dbQuery = dbQuery.Limit(limit)
	}
	if err := dbQuery.Find(&freelancers).Error; err != nil {
		return freelancersMap, total, err
	}

	for _, freelancer := range freelancers {
		id := strconv.FormatUint(uint64(freelancer.ID), 10)
		freelancersMap[id] = freelancer
	}

	if err := db.Find(&[]application.Freelancer{}).Count(&total).Error; err != nil {
		return freelancersMap, total, err
	}

	return freelancersMap, total, nil
}
示例#5
0
func (p *QueryParams) PrepareQuery(searchRelation *gorm.DB) *gorm.DB {
	if len(p.Categories) != 0 && !contains(p.Categories, allCat) {
		searchRelation = searchRelation.Where("category_slug IN (?)", p.Categories)
	}

	if p.Page > 1 {
		searchRelation = searchRelation.Offset((p.Page - 1) * perPage)
	}

	if p.Sort == "newest" {
		searchRelation = searchRelation.Order("date desc")
	} else {
		searchRelation = searchRelation.Order("(total_views / POW(((EXTRACT(EPOCH FROM (now()-date)) / 3600)::integer + 2), 1.5)) desc")
	}

	if !p.StartDate.IsZero() {
		searchRelation = searchRelation.Where("date > ?", p.StartDate)
	}
	if !p.EndDate.IsZero() {
		searchRelation = searchRelation.Where("date < ?", p.EndDate)
	}

	return searchRelation.Limit(perPage)
}
示例#6
0
func LoadPhotos(db *gorm.DB, start int, length int) []Photo {
	var photos []Photo
	db.Offset(start).Limit(length).Find(&photos)
	return photos
}
示例#7
0
func Unpaginate(db *gorm.DB) *gorm.DB {
	return db.Offset(-1).Limit(-1)
}