39 lines
845 B
Go
39 lines
845 B
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.ostiwe.com/ostiwe-com/status/model"
|
|
"git.ostiwe.com/ostiwe-com/status/modules/db"
|
|
)
|
|
|
|
type Status interface {
|
|
Add(ctx context.Context, status model.Status) error
|
|
LastNStatuses(ctx context.Context, serviceID uint64, n uint) ([]model.Status, error)
|
|
}
|
|
|
|
type status struct {
|
|
repository
|
|
}
|
|
|
|
func NewStatusRepository() Status {
|
|
return &status{
|
|
repository: repository{db: db.Global},
|
|
}
|
|
}
|
|
|
|
func (s status) Add(ctx context.Context, status model.Status) error {
|
|
return s.db.WithContext(ctx).Create(&status).Error
|
|
}
|
|
|
|
func (s status) LastNStatuses(ctx context.Context, serviceID uint64, n uint) ([]model.Status, error) {
|
|
var items = make([]model.Status, 0, n)
|
|
|
|
return items, s.db.
|
|
WithContext(ctx).
|
|
Limit(int(n)).
|
|
Order("created_at DESC").
|
|
Find(&items, "service_id = ?", serviceID).
|
|
Error
|
|
}
|