cslms-api/internal/repositories/paper.go

102 lines
3.3 KiB
Go

package repositories
import (
"learnsteam/cslms-api/internal/models"
"gorm.io/gorm"
)
type paperRepository struct {
DB *gorm.DB
}
func NewPaperRepository(db *gorm.DB) PaperRepository {
return &paperRepository{
DB: db,
}
}
type PaperRepository interface {
List(int64, string, int, int) (*[]models.PaperResponse, error)
Total(int64, string) (int64, error)
Find(int64) (*models.PaperResponse, error)
Get(int64) (*models.Paper, error)
Update(*models.Paper) (*models.Paper, error)
}
func (r *paperRepository) List(user_id int64, q string, page int, limit int) (*[]models.PaperResponse, error) {
var rows *[]models.PaperResponse
var err error
offset := limit * (page - 1)
if q != "" {
err = r.DB.Offset(offset).Limit(limit).Order("UserQuizPapers.created_at DESC").
Table("UserQuizPapers").
Select("UserQuizPapers.*", "Users.first_name", "Users.last_name", "QuizPapers.title", "QuizPapers.category").
Joins("LEFT JOIN Users ON Users.id = UserQuizPapers.user_id").
Joins("LEFT JOIN QuizPapers ON QuizPapers.id = UserQuizPapers.quiz_paper_id").
Where("QuizPapers.title LIKE ? OR QuizPapers.category LIKE ? OR CONCAT(first_name, last_name) LIKE ?", "%"+q+"%", "%"+q+"%", "%"+q+"%").
Where("UserQuizPapers.user_id = ?", user_id).
Find(&rows).Error
} else {
err = r.DB.Offset(offset).Limit(limit).Order("UserQuizPapers.created_at DESC").
Table("UserQuizPapers").
Select("UserQuizPapers.*", "Users.first_name", "Users.last_name", "QuizPapers.title", "QuizPapers.category").
Joins("LEFT JOIN Users ON Users.id = UserQuizPapers.user_id").
Joins("LEFT JOIN QuizPapers ON QuizPapers.id = UserQuizPapers.quiz_paper_id").
Where("UserQuizPapers.user_id = ?", user_id).
Find(&rows).Error
}
return rows, err
}
func (r *paperRepository) Total(user_id int64, q string) (int64, error) {
var total int64
var err error
if q != "" {
err = r.DB.Model(&models.Paper{}).
Joins("LEFT JOIN Users ON Users.id = UserQuizPapers.user_id").
Joins("LEFT JOIN QuizPapers ON QuizPapers.id = UserQuizPapers.quiz_paper_id").
Where("QuizPapers.title LIKE ? OR QuizPapers.category LIKE ? OR CONCAT(first_name, last_name) LIKE ?", "%"+q+"%", "%"+q+"%", "%"+q+"%").
Where("UserQuizPapers.user_id = ?", user_id).
Count(&total).
Error
} else {
err = r.DB.Model(&models.Paper{}).
Where("user_id = ?", user_id).
Count(&total).
Error
}
return total, err
}
func (r *paperRepository) Find(id int64) (*models.PaperResponse, error) {
var row *models.PaperResponse
err := r.DB.
Table("UserQuizPapers").
Select("UserQuizPapers.*", "Users.first_name", "Users.last_name", "QuizPapers.title", "QuizPapers.category").
Joins("LEFT JOIN Users ON Users.id = UserQuizPapers.user_id").
Joins("LEFT JOIN QuizPapers ON QuizPapers.id = UserQuizPapers.quiz_paper_id").
Where("UserQuizPapers.id = ?", id).First(&row).Error
return row, err
}
func (r *paperRepository) Get(id int64) (*models.Paper, error) {
var row *models.Paper
if err := r.DB.Where("id=?", id).First(&row).Error; err != nil {
return nil, err
}
return row, nil
}
func (r *paperRepository) Update(paper *models.Paper) (*models.Paper, error) {
var row *models.Paper
if err := r.DB.Where("id=?", paper.ID).First(&row).Error; err != nil {
return nil, err
}
err := r.DB.Model(&row).Select("*").Updates(&paper).Error
return row, err
}