@@ -9,6 +9,14 @@ import (
99 "github.com/swaggest/usecase/status"
1010)
1111
12+ type aliasLister interface {
13+ ListAliases () []spellchecker.ListItem
14+ }
15+
16+ type aliasGetter interface {
17+ GetCodeByAlias (alias string ) (string , error )
18+ }
19+
1220type aliasSetter interface {
1321 SetAlias (alias string , code string ) error
1422}
@@ -17,15 +25,73 @@ type aliasDeleter interface {
1725 DeleteAlias (alias string ) error
1826}
1927
28+ type AliasListResponse struct {
29+ Items []ListItem `json:"items"`
30+ }
31+
32+ func aliasList (registry aliasLister ) usecase.Interactor {
33+ u := usecase .NewInteractor (func (ctx context.Context , input Empty , output * AliasListResponse ) error {
34+ items := registry .ListAliases ()
35+
36+ result := make ([]ListItem , 0 , len (items ))
37+
38+ for _ , item := range items {
39+ result = append (result , ListItem {
40+ Code : item .Code ,
41+ Aliases : item .Aliases ,
42+ })
43+ }
44+
45+ output .Items = result
46+
47+ return nil
48+ })
49+
50+ u .SetTitle ("List all aliases" )
51+ u .SetDescription ("With their aliasesdictionaries" )
52+ u .SetExpectedErrors (status .Internal , status .AlreadyExists , status .InvalidArgument )
53+
54+ return u
55+ }
56+
57+ type AliasGetRequest struct {
58+ Alias string `path:"alias" minLength:"1" description:"Alias to set to the dictionary"`
59+ }
60+
61+ type AliasGetResponse struct {
62+ Dictionary string `json:"dictionary"`
63+ }
64+
65+ func aliasGet (registry aliasGetter ) usecase.Interactor {
66+ u := usecase .NewInteractor (func (ctx context.Context , input AliasGetRequest , output * AliasGetResponse ) error {
67+ code , err := registry .GetCodeByAlias (input .Alias )
68+ if errors .Is (spellchecker .ErrNotFound , err ) {
69+ return status .Wrap (err , status .NotFound )
70+ } else if err != nil {
71+ return status .Wrap (err , status .Internal )
72+ }
73+
74+ output .Dictionary = code
75+
76+ return nil
77+ })
78+
79+ u .SetTitle ("Get dictionary alias" )
80+ u .SetDescription ("Returns dictionary code assigned to the provided alias" )
81+ u .SetExpectedErrors (status .Internal , status .NotFound )
82+
83+ return u
84+ }
85+
2086type AliasSetRequest struct {
2187 Alias string `path:"alias" minLength:"1" description:"Alias to set to the dictionary"`
2288 Dictionary string `json:"dictionary"`
2389}
2490
2591func aliasSet (registry aliasSetter ) usecase.Interactor {
26- u := usecase .NewInteractor (func (ctx context.Context , input AliasSetRequest , output * EmptyResponse ) error {
92+ u := usecase .NewInteractor (func (ctx context.Context , input AliasSetRequest , output * Empty ) error {
2793 err := registry .SetAlias (input .Alias , input .Dictionary )
28- if errors .Is (spellchecker .ErrNotFound , err ) {
94+ if errors .Is (spellchecker .ErrAliasNotFound , err ) {
2995 return status .Wrap (err , status .NotFound )
3096 } else if err != nil {
3197 return status .Wrap (err , status .Internal )
@@ -46,9 +112,9 @@ type AliasDeleteRequest struct {
46112}
47113
48114func aliasDelete (registry aliasDeleter ) usecase.Interactor {
49- u := usecase .NewInteractor (func (ctx context.Context , input AliasDeleteRequest , output * EmptyResponse ) error {
115+ u := usecase .NewInteractor (func (ctx context.Context , input AliasDeleteRequest , output * Empty ) error {
50116 err := registry .DeleteAlias (input .Alias )
51- if errors .Is (spellchecker .ErrNotFound , err ) {
117+ if errors .Is (spellchecker .ErrAliasNotFound , err ) {
52118 return status .Wrap (err , status .NotFound )
53119 } else if err != nil {
54120 return status .Wrap (err , status .Internal )
0 commit comments