44package testapi
55
66import (
7- "context"
8- "encoding/json"
9- "net/http"
7+ "context"
8+ "encoding/json"
9+ "net/http"
1010
11- "github.com/larsartmann/go-composable-business-types/id "
12- "github.com/larsartmann/go-composable-business-types/types "
13- "github.com/larsartmann/go-composable-business-types/enums "
11+ "github.com/larsartmann/go-composable-business-types/enums "
12+ "github.com/larsartmann/go-composable-business-types/id "
13+ "github.com/larsartmann/go-composable-business-types/types "
1414)
1515
1616// Strong ID type aliases for type safety
17- type IDID = id.ID [UserBrand , string ]
18- type IdID = id.ID [GetUserHandlerBrand , string ]
17+ type (
18+ IDID = id.ID [UserBrand , string ]
19+ IdID = id.ID [GetUserHandlerBrand , string ]
20+ )
1921
2022// Brand types for strong ID generics
21- type UserBrand struct {}
22- type GetUserHandlerBrand struct {}
23+ type (
24+ UserBrand struct {}
25+ GetUserHandlerBrand struct {}
26+ )
2327
2428// Domain type aliases (phantom types for type safety)
25- type Age = types.Age
26- type TotalInt = types.TotalInt
27- type ActiveStatus = enums.ActiveStatus
29+ type (
30+ Age = types.Age
31+ TotalInt = types.TotalInt
32+ ActiveStatus = enums.ActiveStatus
33+ )
2834
2935// Type: User from TypeSpec
3036type User struct {
31- ID IDID `json:"id"`
32- Name string `json:"name"`
33- Email types.Email `json:"email,omitempty"`
34- Age Age `json:"age"`
35- Active ActiveStatus `json:"active"`
37+ ID IDID `json:"id"`
38+ Name string `json:"name"`
39+ Email types.Email `json:"email,omitempty"`
40+ Age Age `json:"age"`
41+ Active ActiveStatus `json:"active"`
3642}
3743
3844// Type: CreateUserRequest from TypeSpec
3945type CreateUserRequest struct {
40- Name string `json:"name"`
41- Email types.Email `json:"email"`
42- Age Age `json:"age"`
46+ Name string `json:"name"`
47+ Email types.Email `json:"email"`
48+ Age Age `json:"age"`
4349}
4450
4551// Type: UserList from TypeSpec
4652type UserList struct {
47- Users []User `json:"users"`
48- Total TotalInt `json:"total"`
53+ Users []User `json:"users"`
54+ Total TotalInt `json:"total"`
4955}
5056
5157// Service: TestAPI from TypeSpec
5258type TestAPIService struct {
53- service TestAPIServiceInterface
59+ service TestAPIServiceInterface
5460}
5561
5662// Interface: Generated from TypeSpec operations
5763type TestAPIServiceInterface interface {
58- GetUser (ctx context.Context , id IdID ) (User , error )
59- CreateUser (ctx context.Context , user CreateUserRequest ) (User , error )
60- ListUsers (ctx context.Context , limit * int32 , offset * int32 ) (UserList , error )
61- UpdateUser (ctx context.Context , id IdID , user User ) (User , error )
62- DeleteUser (ctx context.Context , id IdID ) error
64+ GetUser (ctx context.Context , id IdID ) (User , error )
65+ CreateUser (ctx context.Context , user CreateUserRequest ) (User , error )
66+ ListUsers (ctx context.Context , limit , offset * int32 ) (UserList , error )
67+ UpdateUser (ctx context.Context , id IdID , user User ) (User , error )
68+ DeleteUser (ctx context.Context , id IdID ) error
6369}
6470
6571// Helper: handleError handles error response and returns from caller if error occurred
6672func (s * TestAPIService ) handleError (w http.ResponseWriter , err error ) {
67- if err != nil {
68- s .writeError (w , err )
69- return
70- }
73+ if err != nil {
74+ s .writeError (w , err )
75+ return
76+ }
7177}
7278
7379// Helper: writeJsonResponse writes a JSON response with the given status code
7480func (s * TestAPIService ) writeJsonResponse (w http.ResponseWriter , statusCode int , result any ) {
75- w .Header ().Set ("Content-Type" , "application/json" )
76- w .WriteHeader (statusCode )
77- json .NewEncoder (w ).Encode (result )
81+ w .Header ().Set ("Content-Type" , "application/json" )
82+ w .WriteHeader (statusCode )
83+ json .NewEncoder (w ).Encode (result )
7884}
7985
8086// Handler: GetUser from TypeSpec operation
8187func (s * TestAPIService ) GetUserHandler (ctx context.Context , w http.ResponseWriter , r * http.Request , id IdID ) {
82- // TODO: Implement GetUser handler
83- // Route: GET /users/{id}
88+ // TODO: Implement GetUser handler
89+ // Route: GET /users/{id}
8490
85- result , err := s .service .GetUser (ctx , id )
86- s .handleError (w , err )
87- s .writeJsonResponse (w , http .StatusOK , result )
91+ result , err := s .service .GetUser (ctx , id )
92+ s .handleError (w , err )
93+ s .writeJsonResponse (w , http .StatusOK , result )
8894}
8995
9096// Handler: CreateUser from TypeSpec operation
9197func (s * TestAPIService ) CreateUserHandler (ctx context.Context , w http.ResponseWriter , r * http.Request ) {
92- // TODO: Implement CreateUser handler
93- // Route: POST /users
94-
95- var input CreateUserRequest
96- if err := json .NewDecoder (r .Body ).Decode (& input ); err != nil {
97- http .Error (w , "Invalid JSON" , http .StatusBadRequest )
98- return
99- }
100-
101- result , err := s .service .CreateUser (ctx , input )
102- s .handleError (w , err )
103- s .writeJsonResponse (w , http .StatusCreated , result )
98+ // TODO: Implement CreateUser handler
99+ // Route: POST /users
100+
101+ var input CreateUserRequest
102+ if err := json .NewDecoder (r .Body ).Decode (& input ); err != nil {
103+ http .Error (w , "Invalid JSON" , http .StatusBadRequest )
104+ return
105+ }
106+
107+ result , err := s .service .CreateUser (ctx , input )
108+ s .handleError (w , err )
109+ s .writeJsonResponse (w , http .StatusCreated , result )
104110}
105111
106112// Route Registration: Generated from TypeSpec operations
107113func (s * TestAPIService ) RegisterRoutes (mux * http.ServeMux ) {
108- mux .HandleFunc ("/users/{id}" , s .GetUserHandler )
109- mux .HandleFunc ("/users" , s .CreateUserHandler )
110- mux .HandleFunc ("/users" , s .ListUsersHandler )
111- mux .HandleFunc ("/users/{id}" , s .UpdateUserHandler )
112- mux .HandleFunc ("/users/{id}" , s .DeleteUserHandler )
113- }
114+ mux .HandleFunc ("/users/{id}" , s .GetUserHandler )
115+ mux .HandleFunc ("/users" , s .CreateUserHandler )
116+ mux .HandleFunc ("/users" , s .ListUsersHandler )
117+ mux .HandleFunc ("/users/{id}" , s .UpdateUserHandler )
118+ mux .HandleFunc ("/users/{id}" , s .DeleteUserHandler )
119+ }
0 commit comments