Skip to content

Commit 586735b

Browse files
add get progress enpoint
1 parent 255a569 commit 586735b

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

internal/handlers/reading.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,28 @@ func (h *ReadingHandler) UpdateProgress(c *gin.Context) {
8888
c.JSON(http.StatusOK, resp)
8989
}
9090

91+
// @Summary Get Reading Progress
92+
// @Description Retrieve the current reading progress of a book.
93+
// @Tags Reading
94+
// @Produce json
95+
// @Param id path int true "User ID"
96+
// @Param bookID path int true "Book ID"
97+
// @Success 200 {object} models.UserBookProgressResponse
98+
// @Failure 400 {object} models.ErrorResponse
99+
// @Router /users/{id}/readings/{bookID} [get]
100+
// @Security Bearer
101+
func (h *ReadingHandler) GetProgress(c *gin.Context) {
102+
userID, _ := strconv.ParseUint(c.Param("id"), 10, 64)
103+
bookID, _ := strconv.ParseUint(c.Param("bookID"), 10, 64)
104+
105+
resp, err := h.readingService.GetUserBookProgress(uint(userID), uint(bookID))
106+
if err != nil {
107+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
108+
return
109+
}
110+
c.JSON(http.StatusOK, resp)
111+
}
112+
91113
// @Summary Complete Reading a Book
92114
// @Description Mark a book as completed and optionally add a note.
93115
// @Tags Reading

internal/handlers/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ func (s *Server) setupRoutes() {
204204
protected.POST("/users/:id/reading/start", middleware.AuthorizeSelf(), readingHandler.StartReading)
205205
protected.PATCH("/users/:id/reading/:bookID/progress", middleware.AuthorizeSelf(), readingHandler.UpdateProgress)
206206
protected.POST("/users/:id/reading/:bookID/complete", middleware.AuthorizeSelf(), readingHandler.CompleteReading)
207+
protected.GET("/users/:id/readings/:bookID/progress", middleware.AuthorizeSelf(), readingHandler.GetProgress)
207208
protected.GET("/users/:id/reading", middleware.AuthorizeSelf(), readingHandler.ListUserProgress)
208209
protected.GET("/users/:id/reading/history", readingHandler.UserReadingHistory)
209210

internal/services/reading.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,23 @@ func (s *ReadingService) UpdateProgress(userID, bookID uint, req *models.UpdateR
103103
return &resp, nil
104104
}
105105

106+
func (s *ReadingService) GetUserBookProgress(userID, bookID uint) (*models.UserBookProgressResponse, error) {
107+
book, err := s.bookRepo.GetByID(bookID)
108+
if err != nil {
109+
return nil, err
110+
}
111+
112+
p, err := s.readRepo.GetUserBookProgress(userID, bookID)
113+
if errors.Is(err, gorm.ErrRecordNotFound) {
114+
return nil, errors.New("reading not started")
115+
} else if err != nil {
116+
return nil, err
117+
}
118+
119+
resp := p.ToResponse(book)
120+
return &resp, nil
121+
}
122+
106123
func (s *ReadingService) CompleteReading(userID, bookID uint, note *string) (*models.UserBookProgressResponse, error) {
107124
book, err := s.bookRepo.GetByID(bookID)
108125
if err != nil {

0 commit comments

Comments
 (0)