feat: paginate/search words and improve review flow
This commit is contained in:
@@ -100,8 +100,9 @@ func (h *WordHandler) SubmitReview(c *gin.Context) {
|
||||
func (h *WordHandler) GetWords(c *gin.Context) {
|
||||
limit, _ := strconv.Atoi(c.DefaultQuery("limit", "20"))
|
||||
offset, _ := strconv.Atoi(c.DefaultQuery("offset", "0"))
|
||||
query := strings.TrimSpace(c.DefaultQuery("q", ""))
|
||||
|
||||
words, total, err := h.wordService.GetAllWords(limit, offset)
|
||||
words, total, err := h.wordService.GetAllWords(limit, offset, query)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
|
||||
@@ -26,11 +26,20 @@ func (r *WordRepository) Create(word *model.Word) error {
|
||||
return r.db.Create(word).Error
|
||||
}
|
||||
|
||||
func (r *WordRepository) List(limit, offset int) ([]model.Word, int64, error) {
|
||||
func (r *WordRepository) List(limit, offset int, query string) ([]model.Word, int64, error) {
|
||||
var words []model.Word
|
||||
var total int64
|
||||
r.db.Model(&model.Word{}).Count(&total)
|
||||
if err := r.db.Limit(limit).Offset(offset).Order("created_at DESC").Find(&words).Error; err != nil {
|
||||
|
||||
db := r.db.Model(&model.Word{})
|
||||
if query != "" {
|
||||
like := "%" + query + "%"
|
||||
db = db.Where("word LIKE ? OR definition LIKE ?", like, like)
|
||||
}
|
||||
|
||||
if err := db.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if err := db.Limit(limit).Offset(offset).Order("created_at DESC").Find(&words).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return words, total, nil
|
||||
|
||||
@@ -412,8 +412,8 @@ func (s *WordService) SubmitReviewAnswer(userID int64, req request.ReviewAnswerR
|
||||
}
|
||||
|
||||
// 获取所有单词
|
||||
func (s *WordService) GetAllWords(limit, offset int) ([]model.Word, int64, error) {
|
||||
return s.wordRepo.List(limit, offset)
|
||||
func (s *WordService) GetAllWords(limit, offset int, query string) ([]model.Word, int64, error) {
|
||||
return s.wordRepo.List(limit, offset, strings.TrimSpace(query))
|
||||
}
|
||||
|
||||
// 获取记忆统计
|
||||
|
||||
Reference in New Issue
Block a user