init: first steps
This commit is contained in:
50
repository/service.go
Normal file
50
repository/service.go
Normal file
@@ -0,0 +1,50 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user