1
1
package handlers
2
2
3
3
import (
4
- "context"
5
4
"errors"
6
5
"net/http"
6
+ "sandbox-go-api-sqlboiler-rest-auth/internal/middleware"
7
7
"sandbox-go-api-sqlboiler-rest-auth/models"
8
8
"time"
9
9
@@ -25,13 +25,14 @@ type UsersData struct {
25
25
Users * []PublicUser `json:"users"`
26
26
}
27
27
28
- func (h * Handlers ) GetUsers (c echo.Context ) error {
29
- ctx := context .Background ()
28
+ func GetUsers (c echo.Context ) error {
29
+ cc := c .(* middleware.CustomContext )
30
+ ctx := cc .Request ().Context ()
30
31
var users []PublicUser
31
32
res := SuccessResponse {
32
33
Data : UsersData {Users : & users },
33
34
}
34
- err := models .Users ().Bind (ctx , h . db , & users )
35
+ err := models .Users ().Bind (ctx , cc . DB , & users )
35
36
if err != nil {
36
37
c .Error (err )
37
38
return err
@@ -43,8 +44,9 @@ type UserData struct {
43
44
User * PublicUser `json:"user"`
44
45
}
45
46
46
- func (h * Handlers ) GetUser (c echo.Context ) error {
47
- ctx := context .Background ()
47
+ func GetUser (c echo.Context ) error {
48
+ cc := c .(* middleware.CustomContext )
49
+ ctx := cc .Request ().Context ()
48
50
res := SuccessResponse {
49
51
Data : UserData {},
50
52
}
@@ -56,7 +58,7 @@ func (h *Handlers) GetUser(c echo.Context) error {
56
58
return echo .NewHTTPError (http .StatusBadRequest , err .Error ())
57
59
}
58
60
59
- exists , err := models .UserExists (ctx , h . db , id )
61
+ exists , err := models .UserExists (ctx , cc . DB , id )
60
62
if ! exists {
61
63
return echo .NewHTTPError (http .StatusNotFound , http .StatusText (http .StatusNotFound ))
62
64
}
@@ -65,7 +67,7 @@ func (h *Handlers) GetUser(c echo.Context) error {
65
67
}
66
68
67
69
var users []* PublicUser
68
- err = models .Users (qm .Where ("id = ?" , id )).Bind (ctx , h . db , & users )
70
+ err = models .Users (qm .Where ("id = ?" , id )).Bind (ctx , cc . DB , & users )
69
71
if err != nil {
70
72
c .Error (err )
71
73
return err
@@ -80,8 +82,9 @@ type CreateUserRequest struct {
80
82
Password string `json:"password"`
81
83
}
82
84
83
- func (h * Handlers ) CreateUser (c echo.Context ) error {
84
- ctx := context .Background ()
85
+ func CreateUser (c echo.Context ) error {
86
+ cc := c .(* middleware.CustomContext )
87
+ ctx := cc .Request ().Context ()
85
88
res := SuccessResponse {
86
89
Data : UserData {},
87
90
}
@@ -93,7 +96,7 @@ func (h *Handlers) CreateUser(c echo.Context) error {
93
96
Email : req .Email ,
94
97
HashedPassword : req .Password ,
95
98
}
96
- err := u .Insert (ctx , h . db , boil .Infer ())
99
+ err := u .Insert (ctx , cc . DB , boil .Infer ())
97
100
if err != nil {
98
101
return echo .NewHTTPError (http .StatusInternalServerError , err .Error ())
99
102
}
@@ -110,32 +113,33 @@ func (h *Handlers) CreateUser(c echo.Context) error {
110
113
return c .JSON (http .StatusOK , res )
111
114
}
112
115
113
- func ( h * Handlers ) PatchUser (c echo.Context ) error {
116
+ func PatchUser (c echo.Context ) error {
114
117
return errors .New ("not implemented" )
115
118
}
116
119
117
- func (h * Handlers ) DeleteUser (c echo.Context ) error {
118
- ctx := context .Background ()
120
+ func DeleteUser (c echo.Context ) error {
121
+ cc := c .(* middleware.CustomContext )
122
+ ctx := cc .Request ().Context ()
119
123
var id int
120
124
err := echo .PathParamsBinder (c ).
121
125
Int ("id" , & id ).
122
126
BindError ()
123
127
if err != nil {
124
128
return echo .NewHTTPError (http .StatusBadRequest , err .Error ())
125
129
}
126
- exists , err := models .UserExists (ctx , h . db , id )
130
+ exists , err := models .UserExists (ctx , cc . DB , id )
127
131
if ! exists {
128
132
return echo .NewHTTPError (http .StatusNotFound , http .StatusText (http .StatusNotFound ))
129
133
}
130
134
if err != nil {
131
135
return echo .NewHTTPError (http .StatusInternalServerError , err .Error ())
132
136
}
133
- u , err := models .FindUser (ctx , h . db , id )
137
+ u , err := models .FindUser (ctx , cc . DB , id )
134
138
if err != nil {
135
139
return echo .NewHTTPError (http .StatusInternalServerError , err .Error ())
136
140
}
137
141
138
- _ , err = u .Delete (ctx , h . db )
142
+ _ , err = u .Delete (ctx , cc . DB )
139
143
if err != nil {
140
144
return echo .NewHTTPError (http .StatusInternalServerError , err .Error ())
141
145
}
0 commit comments