51 lines
969 B
Go
51 lines
969 B
Go
package repository
|
|
|
|
//go:generate mockgen -source=$GOFILE -destination=mocks/$GOFILE
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"git.ostiwe.com/ostiwe-com/status/model"
|
|
"git.ostiwe.com/ostiwe-com/status/modules/db"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Service interface {
|
|
All(ctx context.Context, limit, offset int, publicOnly bool) ([]model.Service, error)
|
|
}
|
|
|
|
type service struct {
|
|
repository
|
|
}
|
|
|
|
func NewServiceRepository() Service {
|
|
return &service{
|
|
repository{db: db.Global},
|
|
}
|
|
}
|
|
|
|
func (s *service) All(ctx context.Context, limit, offset int, publicOnly bool) ([]model.Service, error) {
|
|
items := make([]model.Service, 0)
|
|
|
|
query := s.db.
|
|
WithContext(ctx).
|
|
Preload("Statuses", func(db *gorm.DB) *gorm.DB {
|
|
return db.
|
|
Where("created_at > ?", time.Now().Truncate(24*time.Hour)).
|
|
Order("created_at desc")
|
|
})
|
|
|
|
if publicOnly {
|
|
query = query.Where("public = ?", true)
|
|
}
|
|
|
|
query = query.
|
|
Limit(limit).
|
|
Offset(offset).
|
|
Find(&items)
|
|
|
|
return items, query.
|
|
Error
|
|
}
|