Skip to content

Commit cc6083b

Browse files
leo-aa88cursoragent
andcommitted
refactor(api): use Handler naming for HTTP layer (#116)
- Rename BookRepository/UserRepository to BookHandler/UserHandler and NewBook* constructors accordingly; receivers use h *bookHandler. - Router registers routes via books/users handler variables. - Rename middleware authenticateJWT*.go to jwt_auth*.go (idiomatic file names). - Update README project tree (jwt_auth, database doc.go, drop stale db_mock). Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent c6822e8 commit cc6083b

8 files changed

Lines changed: 117 additions & 116 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ golang-rest-api-template/
6565
│ │ └── redis_env_test.go
6666
│ ├── database
6767
│ │ ├── db.go
68-
│ │ ├── db_mock.go
6968
│ │ ├── db_test.go
69+
│ │ ├── doc.go
7070
│ │ ├── mongo.go
7171
│ │ └── mongo_test.go
7272
│ ├── middleware
7373
│ │ ├── api_key.go
7474
│ │ ├── api_key_test.go
75-
│ │ ├── authenticateJWT.go
76-
│ │ ├── authenticateJWT_test.go
75+
│ │ ├── jwt_auth.go
76+
│ │ ├── jwt_auth_test.go
7777
│ │ ├── cors.go
7878
│ │ ├── logger.go
7979
│ │ ├── max_body.go

pkg/api/books.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ const (
1919
findBooksMaxLimit = 100
2020
)
2121

22-
// BookRepository is the HTTP surface for book routes (Gin handlers).
23-
type BookRepository interface {
22+
// BookHandler defines Gin handlers for book routes (HTTP layer only; persistence
23+
// lives in pkg/repository).
24+
type BookHandler interface {
2425
Healthcheck(c *gin.Context)
2526
FindBooks(c *gin.Context)
2627
CreateBook(c *gin.Context)
@@ -29,13 +30,13 @@ type BookRepository interface {
2930
DeleteBook(c *gin.Context)
3031
}
3132

32-
type bookRepository struct {
33+
type bookHandler struct {
3334
svc *service.BookService
3435
}
3536

36-
// NewBookRepository wires persistence and cache into book HTTP handlers.
37-
func NewBookRepository(store repository.BookPersistence, redisClient cache.Cache) *bookRepository {
38-
return &bookRepository{svc: service.NewBookService(store, redisClient)}
37+
// NewBookHandler wires persistence and cache into book HTTP handlers.
38+
func NewBookHandler(store repository.BookPersistence, redisClient cache.Cache) *bookHandler {
39+
return &bookHandler{svc: service.NewBookService(store, redisClient)}
3940
}
4041

4142
func parseIDParam(c *gin.Context) (uint, bool) {
@@ -90,7 +91,7 @@ func parseOffsetLimit(c *gin.Context) (offset, limit int, ok bool) {
9091
// @Produce json
9192
// @Success 200 {string} ok
9293
// @Router / [get]
93-
func (r *bookRepository) Healthcheck(c *gin.Context) {
94+
func (h *bookHandler) Healthcheck(c *gin.Context) {
9495
c.JSON(http.StatusOK, "ok")
9596
}
9697

@@ -106,12 +107,12 @@ func (r *bookRepository) Healthcheck(c *gin.Context) {
106107
// @Failure 400 {string} string "Bad Request"
107108
// @Failure 500 {string} string "Internal Server Error"
108109
// @Router /books [get]
109-
func (r *bookRepository) FindBooks(c *gin.Context) {
110+
func (h *bookHandler) FindBooks(c *gin.Context) {
110111
offset, limit, ok := parseOffsetLimit(c)
111112
if !ok {
112113
return
113114
}
114-
books, err := r.svc.ListBooks(c.Request.Context(), offset, limit)
115+
books, err := h.svc.ListBooks(c.Request.Context(), offset, limit)
115116
if err != nil {
116117
switch {
117118
case errors.Is(err, service.ErrListBooksDB):
@@ -144,13 +145,13 @@ func (r *bookRepository) FindBooks(c *gin.Context) {
144145
// @Failure 401 {string} string "Unauthorized"
145146
// @Failure 500 {string} string "Internal Server Error"
146147
// @Router /books [post]
147-
func (r *bookRepository) CreateBook(c *gin.Context) {
148+
func (h *bookHandler) CreateBook(c *gin.Context) {
148149
var input models.CreateBook
149150
if err := c.ShouldBindJSON(&input); err != nil {
150151
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
151152
return
152153
}
153-
book, err := r.svc.CreateBook(c.Request.Context(), input.Title, input.Author)
154+
book, err := h.svc.CreateBook(c.Request.Context(), input.Title, input.Author)
154155
if err != nil {
155156
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create book"})
156157
return
@@ -168,12 +169,12 @@ func (r *bookRepository) CreateBook(c *gin.Context) {
168169
// @Success 200 {object} models.Book "Successfully retrieved book"
169170
// @Failure 404 {string} string "Book not found"
170171
// @Router /books/{id} [get]
171-
func (r *bookRepository) FindBook(c *gin.Context) {
172+
func (h *bookHandler) FindBook(c *gin.Context) {
172173
id, ok := parseIDParam(c)
173174
if !ok {
174175
return
175176
}
176-
book, err := r.svc.GetBook(c.Request.Context(), id)
177+
book, err := h.svc.GetBook(c.Request.Context(), id)
177178
if err != nil {
178179
if repository.IsBookNotFound(err) {
179180
c.JSON(http.StatusNotFound, gin.H{"error": "book not found"})
@@ -201,7 +202,7 @@ func (r *bookRepository) FindBook(c *gin.Context) {
201202
// @Failure 404 {string} string "book not found"
202203
// @Failure 500 {string} string "Internal Server Error"
203204
// @Router /books/{id} [put]
204-
func (r *bookRepository) UpdateBook(c *gin.Context) {
205+
func (h *bookHandler) UpdateBook(c *gin.Context) {
205206
var input models.UpdateBook
206207
id, ok := parseIDParam(c)
207208
if !ok {
@@ -211,7 +212,7 @@ func (r *bookRepository) UpdateBook(c *gin.Context) {
211212
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
212213
return
213214
}
214-
book, err := r.svc.UpdateBook(c.Request.Context(), id, input.Title, input.Author)
215+
book, err := h.svc.UpdateBook(c.Request.Context(), id, input.Title, input.Author)
215216
if err != nil {
216217
if repository.IsBookNotFound(err) {
217218
c.JSON(http.StatusNotFound, gin.H{"error": "book not found"})
@@ -236,12 +237,12 @@ func (r *bookRepository) UpdateBook(c *gin.Context) {
236237
// @Failure 404 {string} string "book not found"
237238
// @Failure 500 {string} string "Internal Server Error"
238239
// @Router /books/{id} [delete]
239-
func (r *bookRepository) DeleteBook(c *gin.Context) {
240+
func (h *bookHandler) DeleteBook(c *gin.Context) {
240241
id, ok := parseIDParam(c)
241242
if !ok {
242243
return
243244
}
244-
if err := r.svc.DeleteBook(c.Request.Context(), id); err != nil {
245+
if err := h.svc.DeleteBook(c.Request.Context(), id); err != nil {
245246
if repository.IsBookNotFound(err) {
246247
c.JSON(http.StatusNotFound, gin.H{"error": "book not found"})
247248
return

0 commit comments

Comments
 (0)