-
Notifications
You must be signed in to change notification settings - Fork 4
/
console_search.go
56 lines (46 loc) · 1.37 KB
/
console_search.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
package usecase
import (
"context"
"fmt"
"time"
"github.com/gandarez/video-game-api/internal/entity"
"github.com/gandarez/video-game-api/internal/model"
"github.com/google/uuid"
)
//go:generate mockery --name ConsoleRepositoryFinder --structname MockConsoleRepositoryFinder --inpackage --case snake
type (
// ConsoleRepositoryFinder is an interface for finding a console from the repository.
ConsoleRepositoryFinder interface {
FindByID(ctx context.Context, id string) (*entity.Console, error)
}
// ConsoleSearch is a use case for searching a console.
ConsoleSearch struct {
repo ConsoleRepositoryFinder
}
)
// NewConsoleSearch creates a new console search use case.
func NewConsoleSearch(repo ConsoleRepositoryFinder) *ConsoleSearch {
return &ConsoleSearch{
repo: repo,
}
}
// Search searches a console.
func (c *ConsoleSearch) Search(ctx context.Context, id string) (*model.Console, error) {
uid, err := uuid.Parse(id)
if err != nil {
return nil, fmt.Errorf("invalid id: %s", id)
}
console, err := c.repo.FindByID(ctx, uid.String())
if err != nil {
return nil, err
}
return parseConsoleEntity(console), nil
}
func parseConsoleEntity(input *entity.Console) *model.Console {
return &model.Console{
ID: input.ID.String(),
Name: input.Name,
Manufacturer: input.Manufacturer,
ReleaseDate: input.ReleaseDate.Format(time.DateOnly),
}
}