This repository has been archived by the owner on Jul 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
87 lines (75 loc) · 2.23 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package game
import (
"context"
"errors"
"fmt"
"log"
"time"
"github.com/IvanKyrylov/user-game-api/internal/apperror"
)
var _ Service = &service{}
type Service interface {
GetById(ctx context.Context, id string) (Game, error)
GetByPlayer(ctx context.Context, uuid string, limit, page int64) ([]Game, error)
GetAll(ctx context.Context, limit, page int64) ([]Game, error)
GetGamesStatistics(ctx context.Context, userId string, startDate, endDate time.Time) ([]GamesStatistics, error)
}
type service struct {
storage Storage
logger *log.Logger
}
func NewService(storage Storage, logger *log.Logger) (Service, error) {
return &service{
storage: storage,
logger: logger,
}, nil
}
func (s service) GetById(ctx context.Context, id string) (game Game, err error) {
game, err = s.storage.FindById(ctx, id)
if err != nil {
if errors.Is(err, apperror.ErrNotFound.Err) {
return game, err
}
return game, fmt.Errorf("failed to find game by id. error: %w", err)
}
return game, nil
}
func (s service) GetByPlayer(ctx context.Context, uuid string, limit, page int64) (games []Game, err error) {
games, err = s.storage.FindByPlayer(ctx, uuid, limit, page)
if err != nil {
if errors.Is(err, apperror.ErrNotFound) {
return games, err
}
return games, fmt.Errorf("failed to get games by player. error: %w", err)
}
if len(games) == 0 {
return games, apperror.ErrNotFound
}
return games, nil
}
func (s service) GetAll(ctx context.Context, limit, page int64) (games []Game, err error) {
games, err = s.storage.FindAll(ctx, limit, page)
if err != nil {
if errors.Is(err, apperror.ErrNotFound) {
return games, err
}
return games, fmt.Errorf("failed to get all games. error: %w", err)
}
if len(games) == 0 {
return games, apperror.ErrNotFound
}
return games, nil
}
func (s service) GetGamesStatistics(ctx context.Context, userId string, startDate, endDate time.Time) (data []GamesStatistics, err error) {
data, err = s.storage.AggregateGamesStatistics(ctx, userId, startDate, endDate)
if err != nil {
if errors.Is(err, apperror.ErrNotFound) {
return data, err
}
return data, fmt.Errorf("failed to get statistics games. error: %w", err)
}
if len(data) == 0 {
return data, apperror.ErrNotFound
}
return data, nil
}