Skip to content

Commit 296ba02

Browse files
committed
Split routes into different files
1 parent 6a5e8a5 commit 296ba02

12 files changed

Lines changed: 332 additions & 252 deletions

internal/routes/alias.go

Lines changed: 0 additions & 131 deletions
This file was deleted.

internal/routes/alias_delete.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package routes
2+
3+
import (
4+
"context"
5+
"errors"
6+
7+
"github.com/f1monkey/spellchecker-web/internal/spellchecker"
8+
"github.com/swaggest/usecase"
9+
"github.com/swaggest/usecase/status"
10+
)
11+
12+
type aliasDeleter interface {
13+
DeleteAlias(alias string) error
14+
}
15+
16+
type AliasDeleteRequest struct {
17+
Alias string `path:"alias" minLength:"1" description:"Alias to delete"`
18+
}
19+
20+
func aliasDelete(registry aliasDeleter) usecase.Interactor {
21+
u := usecase.NewInteractor(func(ctx context.Context, input AliasDeleteRequest, output *Empty) error {
22+
err := registry.DeleteAlias(input.Alias)
23+
if errors.Is(spellchecker.ErrAliasNotFound, err) {
24+
return status.Wrap(err, status.NotFound)
25+
} else if err != nil {
26+
return status.Wrap(err, status.Internal)
27+
}
28+
29+
return nil
30+
})
31+
32+
u.SetTitle("Delete alias from a dictionary")
33+
u.SetDescription("Removes an alias from a dictionary.")
34+
u.SetExpectedErrors(status.Internal, status.NotFound)
35+
36+
return u
37+
}

internal/routes/alias_get.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package routes
2+
3+
import (
4+
"context"
5+
"errors"
6+
7+
"github.com/f1monkey/spellchecker-web/internal/spellchecker"
8+
"github.com/swaggest/usecase"
9+
"github.com/swaggest/usecase/status"
10+
)
11+
12+
type aliasGetter interface {
13+
GetCodeByAlias(alias string) (string, error)
14+
}
15+
16+
type AliasGetRequest struct {
17+
Alias string `path:"alias" minLength:"1" description:"Alias to set to the dictionary"`
18+
}
19+
20+
type AliasGetResponse struct {
21+
Dictionary string `json:"dictionary"`
22+
}
23+
24+
func aliasGet(registry aliasGetter) usecase.Interactor {
25+
u := usecase.NewInteractor(func(ctx context.Context, input AliasGetRequest, output *AliasGetResponse) error {
26+
code, err := registry.GetCodeByAlias(input.Alias)
27+
if errors.Is(spellchecker.ErrNotFound, err) {
28+
return status.Wrap(err, status.NotFound)
29+
} else if err != nil {
30+
return status.Wrap(err, status.Internal)
31+
}
32+
33+
output.Dictionary = code
34+
35+
return nil
36+
})
37+
38+
u.SetTitle("Get dictionary alias")
39+
u.SetDescription("Returns dictionary code assigned to the provided alias")
40+
u.SetExpectedErrors(status.Internal, status.NotFound)
41+
42+
return u
43+
}

internal/routes/alias_list.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package routes
2+
3+
import (
4+
"context"
5+
6+
"github.com/f1monkey/spellchecker-web/internal/spellchecker"
7+
"github.com/swaggest/usecase"
8+
"github.com/swaggest/usecase/status"
9+
)
10+
11+
type aliasLister interface {
12+
ListAliases() []spellchecker.ListItem
13+
}
14+
15+
type AliasListResponse struct {
16+
Items []ListItem `json:"items"`
17+
}
18+
19+
func aliasList(registry aliasLister) usecase.Interactor {
20+
u := usecase.NewInteractor(func(ctx context.Context, input Empty, output *AliasListResponse) error {
21+
items := registry.ListAliases()
22+
23+
result := make([]ListItem, 0, len(items))
24+
25+
for _, item := range items {
26+
result = append(result, ListItem{
27+
Code: item.Code,
28+
Aliases: item.Aliases,
29+
})
30+
}
31+
32+
output.Items = result
33+
34+
return nil
35+
})
36+
37+
u.SetTitle("List all aliases")
38+
u.SetDescription("With their aliasesdictionaries")
39+
u.SetExpectedErrors(status.Internal, status.AlreadyExists, status.InvalidArgument)
40+
41+
return u
42+
}

internal/routes/alias_set.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package routes
2+
3+
import (
4+
"context"
5+
"errors"
6+
7+
"github.com/f1monkey/spellchecker-web/internal/spellchecker"
8+
"github.com/swaggest/usecase"
9+
"github.com/swaggest/usecase/status"
10+
)
11+
12+
type aliasSetter interface {
13+
SetAlias(alias string, code string) error
14+
}
15+
16+
type AliasSetRequest struct {
17+
Alias string `path:"alias" minLength:"1" description:"Alias to set to the dictionary"`
18+
Dictionary string `json:"dictionary"`
19+
}
20+
21+
func aliasSet(registry aliasSetter) usecase.Interactor {
22+
u := usecase.NewInteractor(func(ctx context.Context, input AliasSetRequest, output *Empty) error {
23+
err := registry.SetAlias(input.Alias, input.Dictionary)
24+
if errors.Is(spellchecker.ErrAliasNotFound, err) {
25+
return status.Wrap(err, status.NotFound)
26+
} else if err != nil {
27+
return status.Wrap(err, status.Internal)
28+
}
29+
30+
return nil
31+
})
32+
33+
u.SetTitle("Set dictionary alias")
34+
u.SetDescription("Assigns an alias to a dictionary. If the alias is already used by another dictionary, it will be reassigned to the current one. This route can be used, for example, to manage dictionary versioning.")
35+
u.SetExpectedErrors(status.Internal, status.NotFound)
36+
37+
return u
38+
}

0 commit comments

Comments
 (0)