91 lines
2.6 KiB
Go
91 lines
2.6 KiB
Go
package repositories
|
|
|
|
import (
|
|
"fmt"
|
|
"learnsteam/cslms-api/internal/models"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type quizPaperRepository struct {
|
|
DB *gorm.DB
|
|
}
|
|
|
|
func NewQuizPaperRepository(db *gorm.DB) QuizPaperRepository {
|
|
return &quizPaperRepository{
|
|
DB: db,
|
|
}
|
|
}
|
|
|
|
type QuizPaperRepository interface {
|
|
List(string, string, int, int) (*[]models.QuizPaper, error)
|
|
Total(string, string) (int64, error)
|
|
|
|
Find(int64) (*models.QuizPaper, error)
|
|
Create(*models.QuizPaper) (*models.QuizPaper, error)
|
|
Update(*models.QuizPaper) (*models.QuizPaper, error)
|
|
Delete(string) error
|
|
}
|
|
|
|
func (r *quizPaperRepository) List(q string, tag string, page int, limit int) (*[]models.QuizPaper, error) {
|
|
var quizPapers *[]models.QuizPaper
|
|
var err error
|
|
offset := limit * (page - 1)
|
|
fmt.Println("q", q)
|
|
if q != "" {
|
|
fmt.Println(" 1 q", q)
|
|
err = r.DB.Offset(offset).Limit(limit).Order("created_at DESC").Where("title LIKE ? OR category LIKE ? OR tag LIKE ?", "%"+q+"%", "%"+q+"%", "%"+q+"%").Find(&quizPapers).Error
|
|
} else if tag != "" {
|
|
fmt.Println(" tag", tag)
|
|
err = r.DB.Offset(offset).Limit(limit).Order("created_at DESC").Where("tag LIKE ?", "%"+tag+"%").Find(&quizPapers).Error
|
|
} else {
|
|
fmt.Println(" query")
|
|
err = r.DB.Offset(offset).Limit(limit).Order("created_at DESC").Find(&quizPapers).Error
|
|
}
|
|
return quizPapers, err
|
|
}
|
|
|
|
func (r *quizPaperRepository) Total(q string, tag string) (int64, error) {
|
|
var total int64
|
|
var err error
|
|
if q != "" {
|
|
err = r.DB.Model(&models.QuizPaper{}).Where("title LIKE ? OR category LIKE ? OR tag LIKE ?", "%"+q+"%", "%"+q+"%", "%"+q+"%").Count(&total).Error
|
|
} else if tag != "" {
|
|
err = r.DB.Model(&models.QuizPaper{}).Where("tag LIKE ?", "%"+tag+"%").Count(&total).Error
|
|
} else {
|
|
err = r.DB.Model(&models.QuizPaper{}).Count(&total).Error
|
|
}
|
|
|
|
return total, err
|
|
}
|
|
|
|
func (r *quizPaperRepository) Find(id int64) (*models.QuizPaper, error) {
|
|
var quizPaper *models.QuizPaper
|
|
err := r.DB.Where("id = ?", id).First(&quizPaper).Error
|
|
return quizPaper, err
|
|
}
|
|
|
|
func (r *quizPaperRepository) Create(quizPaper *models.QuizPaper) (*models.QuizPaper, error) {
|
|
err := r.DB.Create(&quizPaper).Error
|
|
return quizPaper, err
|
|
}
|
|
|
|
func (r *quizPaperRepository) Update(quizPaper *models.QuizPaper) (*models.QuizPaper, error) {
|
|
var row *models.QuizPaper
|
|
if err := r.DB.Where("id=?", quizPaper.ID).First(&row).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err := r.DB.Model(&row).Select("*").Updates(&quizPaper).Error
|
|
return row, err
|
|
}
|
|
|
|
func (r *quizPaperRepository) Delete(id string) error {
|
|
var quizPaper *models.QuizPaper
|
|
if err := r.DB.Where("id=?", id).First(&quizPaper).Error; err != nil {
|
|
return err
|
|
}
|
|
err := r.DB.Delete(&quizPaper).Error
|
|
return err
|
|
}
|