t2/services/capture.go

80 lines
1.7 KiB
Go

package services
import (
"context"
"gitea.henriburau.de/haw-lan/cod4watcher/models"
)
type CaptureService struct {
p models.Persistence
}
func NewCaptureService(persistence models.Persistence) *CaptureService {
return &CaptureService{
p: persistence,
}
}
func (cs *CaptureService) CreateCapture(ctx context.Context, capture *models.Capture) error {
return cs.p.CreateCapture(ctx, capture)
}
func (cs *CaptureService) DeleteCapture(ctx context.Context, id int64) error {
return cs.p.DeleteCapture(ctx, id)
}
func (cs *CaptureService) SetMapScoreConted(ctx context.Context, id int64, counted bool) error {
return cs.p.SetMapScoreConted(ctx, id, counted)
}
func (cs *CaptureService) GetActiveCapures(ctx context.Context) ([]models.Capture, error) {
captures, err := cs.p.GetCaptures(ctx)
if err != nil {
return nil, err
}
var result []models.Capture
for _, capture := range captures {
if capture.Active {
result = append(result, capture)
}
}
return result, nil
}
func (cs *CaptureService) GetCaptures(ctx context.Context) ([]models.Capture, error) {
return cs.p.GetCaptures(ctx)
}
func (cs *CaptureService) StartCapture(ctx context.Context, id int64) error {
capture, err := cs.p.GetCapturesByID(ctx, id)
if err != nil {
return err
}
capture.Active = true
return cs.p.UpdateCapture(ctx, capture)
}
func (cs *CaptureService) StopCapture(ctx context.Context, id int64) error {
capture, err := cs.p.GetCapturesByID(ctx, id)
if err != nil {
return err
}
capture.Active = false
return cs.p.UpdateCapture(ctx, capture)
}
func (cs *CaptureService) GetCaptureById(ctx context.Context, id int64) (*models.Capture, error) {
capture, err := cs.p.GetCapturesByID(ctx, id)
if err != nil {
return nil, err
}
return capture, nil
}