feat: paginate/search words and improve review flow
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user