init: first steps
This commit is contained in:
57
repository/mocks/service.go
Normal file
57
repository/mocks/service.go
Normal file
@@ -0,0 +1,57 @@
|
||||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: service.go
|
||||
//
|
||||
// Generated by this command:
|
||||
//
|
||||
// mockgen -source=service.go -destination=mocks/service.go
|
||||
//
|
||||
|
||||
// Package mock_repository is a generated GoMock package.
|
||||
package mock_repository
|
||||
|
||||
import (
|
||||
context "context"
|
||||
reflect "reflect"
|
||||
|
||||
model "git.ostiwe.com/ostiwe-com/status/model"
|
||||
gomock "go.uber.org/mock/gomock"
|
||||
)
|
||||
|
||||
// MockService is a mock of Service interface.
|
||||
type MockService struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockServiceMockRecorder
|
||||
isgomock struct{}
|
||||
}
|
||||
|
||||
// MockServiceMockRecorder is the mock recorder for MockService.
|
||||
type MockServiceMockRecorder struct {
|
||||
mock *MockService
|
||||
}
|
||||
|
||||
// NewMockService creates a new mock instance.
|
||||
func NewMockService(ctrl *gomock.Controller) *MockService {
|
||||
mock := &MockService{ctrl: ctrl}
|
||||
mock.recorder = &MockServiceMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use.
|
||||
func (m *MockService) EXPECT() *MockServiceMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// All mocks base method.
|
||||
func (m *MockService) All(ctx context.Context, limit, offset int) ([]model.Service, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "All", ctx, limit, offset)
|
||||
ret0, _ := ret[0].([]model.Service)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// All indicates an expected call of All.
|
||||
func (mr *MockServiceMockRecorder) All(ctx, limit, offset any) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "All", reflect.TypeOf((*MockService)(nil).All), ctx, limit, offset)
|
||||
}
|
||||
7
repository/repository.go
Normal file
7
repository/repository.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package repository
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type repository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
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