2023-12-27 17:31:49 +09:00
package repositories
import (
"learnsteam/cslms-api/internal/models"
"gorm.io/gorm"
2023-12-29 00:27:12 +09:00
"gorm.io/gorm/clause"
2023-12-27 17:31:49 +09:00
)
type userQuizPaperRepository struct {
DB * gorm . DB
}
func NewUserQuizPaperRepository ( db * gorm . DB ) UserQuizPaperRepository {
return & userQuizPaperRepository {
DB : db ,
}
}
type UserQuizPaperRepository interface {
2024-01-20 17:40:22 +09:00
List ( string , int , int ) ( * [ ] models . UserQuizPaperResponse , error )
2023-12-27 17:31:49 +09:00
Total ( string ) ( int64 , error )
2024-01-20 17:40:22 +09:00
Find ( int64 ) ( * models . UserQuizPaperResponse , error )
Get ( int64 ) ( * models . UserQuizPaper , error )
2023-12-27 17:31:49 +09:00
Create ( * models . UserQuizPaper ) ( * models . UserQuizPaper , error )
Insert ( [ ] models . UserQuizPaper ) ( [ ] models . UserQuizPaper , error )
Update ( * models . UserQuizPaper ) ( * models . UserQuizPaper , error )
Delete ( string ) error
}
2024-01-20 17:40:22 +09:00
func ( r * userQuizPaperRepository ) List ( q string , page int , limit int ) ( * [ ] models . UserQuizPaperResponse , error ) {
var rows * [ ] models . UserQuizPaperResponse
2023-12-27 17:31:49 +09:00
var err error
offset := limit * ( page - 1 )
if q != "" {
2024-01-20 17:40:22 +09:00
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 + "%" ) .
Find ( & rows ) . Error
2023-12-27 17:31:49 +09:00
} else {
2024-01-20 17:40:22 +09:00
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" ) .
Find ( & rows ) . Error
2023-12-27 17:31:49 +09:00
}
2024-01-20 17:40:22 +09:00
return rows , err
2023-12-27 17:31:49 +09:00
}
func ( r * userQuizPaperRepository ) Total ( q string ) ( int64 , error ) {
var total int64
var err error
if q != "" {
2024-01-20 17:40:22 +09:00
err = r . DB . Model ( & models . UserQuizPaper { } ) .
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 + "%" ) . Count ( & total ) . Error
2023-12-27 17:31:49 +09:00
} else {
err = r . DB . Model ( & models . UserQuizPaper { } ) . Count ( & total ) . Error
}
return total , err
}
2024-01-20 17:40:22 +09:00
func ( r * userQuizPaperRepository ) Find ( id int64 ) ( * models . UserQuizPaperResponse , error ) {
var row * models . UserQuizPaperResponse
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 * userQuizPaperRepository ) Get ( id int64 ) ( * models . UserQuizPaper , error ) {
var row * models . UserQuizPaper
if err := r . DB . Where ( "id=?" , id ) . First ( & row ) . Error ; err != nil {
return nil , err
}
return row , nil
2023-12-27 17:31:49 +09:00
}
func ( r * userQuizPaperRepository ) Create ( userQuizPaper * models . UserQuizPaper ) ( * models . UserQuizPaper , error ) {
err := r . DB . Create ( & userQuizPaper ) . Error
return userQuizPaper , err
}
func ( r * userQuizPaperRepository ) Insert ( items [ ] models . UserQuizPaper ) ( [ ] models . UserQuizPaper , error ) {
2023-12-29 00:27:12 +09:00
err := r . DB . Clauses ( clause . OnConflict { DoNothing : true } ) . Create ( & items ) . Error
2023-12-27 17:31:49 +09:00
return items , err
}
func ( r * userQuizPaperRepository ) Update ( userQuizPaper * models . UserQuizPaper ) ( * models . UserQuizPaper , error ) {
var row * models . UserQuizPaper
if err := r . DB . Where ( "id=?" , userQuizPaper . ID ) . First ( & row ) . Error ; err != nil {
return nil , err
}
err := r . DB . Model ( & row ) . Select ( "*" ) . Updates ( & userQuizPaper ) . Error
return row , err
}
func ( r * userQuizPaperRepository ) Delete ( id string ) error {
var userQuizPaper * models . UserQuizPaper
if err := r . DB . Where ( "id=?" , id ) . First ( & userQuizPaper ) . Error ; err != nil {
return err
}
err := r . DB . Delete ( & userQuizPaper ) . Error
return err
}