package Database
|
|
|
|
import (
|
|
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Models"
|
|
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
)
|
|
|
|
func GetUserConversationById(id string) (Models.UserConversation, error) {
|
|
var (
|
|
message Models.UserConversation
|
|
err error
|
|
)
|
|
|
|
err = DB.First(&message, "id = ?", id).
|
|
Error
|
|
|
|
return message, err
|
|
}
|
|
|
|
func GetUserConversationsByUserId(id string, page int) ([]Models.UserConversation, error) {
|
|
var (
|
|
conversations []Models.UserConversation
|
|
offset int
|
|
err error
|
|
)
|
|
|
|
offset = page * PageSize
|
|
|
|
err = DB.Offset(offset).
|
|
Limit(PageSize).
|
|
Order("created_at DESC").
|
|
Find(&conversations, "user_id = ?", id).
|
|
Error
|
|
|
|
return conversations, err
|
|
}
|
|
|
|
func CreateUserConversation(userConversation *Models.UserConversation) error {
|
|
var err error
|
|
|
|
err = DB.Session(&gorm.Session{FullSaveAssociations: true}).
|
|
Create(userConversation).
|
|
Error
|
|
|
|
return err
|
|
}
|
|
|
|
func CreateUserConversations(userConversations *[]Models.UserConversation) error {
|
|
var err error
|
|
|
|
err = DB.Create(userConversations).
|
|
Error
|
|
|
|
return err
|
|
}
|
|
|
|
func UpdateUserConversation(userConversation *Models.UserConversation) error {
|
|
var err error
|
|
|
|
err = DB.Model(Models.UserConversation{}).
|
|
Updates(userConversation).
|
|
Error
|
|
|
|
return err
|
|
}
|
|
|
|
func UpdateUserConversations(userConversations *[]Models.UserConversation) error {
|
|
var err error
|
|
|
|
err = DB.Model(Models.UserConversation{}).
|
|
Updates(userConversations).
|
|
Error
|
|
|
|
return err
|
|
}
|
|
|
|
func UpdateOrCreateUserConversations(userConversations *[]Models.UserConversation) error {
|
|
var err error
|
|
|
|
err = DB.Model(Models.UserConversation{}).
|
|
Clauses(clause.OnConflict{
|
|
Columns: []clause.Column{{Name: "id"}},
|
|
DoUpdates: clause.AssignmentColumns([]string{"admin"}),
|
|
}).
|
|
Create(userConversations).
|
|
Error
|
|
|
|
return err
|
|
}
|
|
|
|
func DeleteUserConversation(userConversation *Models.UserConversation) error {
|
|
return DB.Session(&gorm.Session{FullSaveAssociations: true}).
|
|
Delete(userConversation).
|
|
Error
|
|
}
|