84 lines
2.5 KiB
Go
84 lines
2.5 KiB
Go
package dal
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gitlab.guxuan.icu/jinshan_community/internal/mods/ai/schema"
|
|
"gitlab.guxuan.icu/jinshan_community/pkg/errors"
|
|
"gitlab.guxuan.icu/jinshan_community/pkg/util"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// Get ai request storage instance
|
|
func GetAiRequestDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
|
|
return util.GetDB(ctx, defDB).Model(new(schema.AiRequest))
|
|
}
|
|
|
|
// Defining the `AiRequest` data access object.
|
|
type AiRequest struct {
|
|
DB *gorm.DB
|
|
}
|
|
|
|
// Query ai requests from the database based on the provided parameters and options.
|
|
func (a *AiRequest) Query(ctx context.Context, params schema.AiRequestQueryParam, opts ...schema.AiRequestQueryOptions) (*schema.AiRequestQueryResult, error) {
|
|
var opt schema.AiRequestQueryOptions
|
|
if len(opts) > 0 {
|
|
opt = opts[0]
|
|
}
|
|
|
|
db := GetAiRequestDB(ctx, a.DB)
|
|
|
|
var list schema.AiRequests
|
|
pageResult, err := util.WrapPageQuery(ctx, db, params.PaginationParam, opt.QueryOptions, &list)
|
|
if err != nil {
|
|
return nil, errors.WithStack(err)
|
|
}
|
|
|
|
queryResult := &schema.AiRequestQueryResult{
|
|
PageResult: pageResult,
|
|
Data: list,
|
|
}
|
|
return queryResult, nil
|
|
}
|
|
|
|
// Get the specified ai request from the database.
|
|
func (a *AiRequest) Get(ctx context.Context, id string, opts ...schema.AiRequestQueryOptions) (*schema.AiRequest, error) {
|
|
var opt schema.AiRequestQueryOptions
|
|
if len(opts) > 0 {
|
|
opt = opts[0]
|
|
}
|
|
|
|
item := new(schema.AiRequest)
|
|
ok, err := util.FindOne(ctx, GetAiRequestDB(ctx, a.DB).Where("id=?", id), opt.QueryOptions, item)
|
|
if err != nil {
|
|
return nil, errors.WithStack(err)
|
|
} else if !ok {
|
|
return nil, nil
|
|
}
|
|
return item, nil
|
|
}
|
|
|
|
// Exists checks if the specified ai request exists in the database.
|
|
func (a *AiRequest) Exists(ctx context.Context, id string) (bool, error) {
|
|
ok, err := util.Exists(ctx, GetAiRequestDB(ctx, a.DB).Where("id=?", id))
|
|
return ok, errors.WithStack(err)
|
|
}
|
|
|
|
// Create a new ai request.
|
|
func (a *AiRequest) Create(ctx context.Context, item *schema.AiRequest) error {
|
|
result := GetAiRequestDB(ctx, a.DB).Create(item)
|
|
return errors.WithStack(result.Error)
|
|
}
|
|
|
|
// Update the specified ai request in the database.
|
|
func (a *AiRequest) Update(ctx context.Context, item *schema.AiRequest) error {
|
|
result := GetAiRequestDB(ctx, a.DB).Where("id=?", item.ID).Select("*").Omit("created_at").Updates(item)
|
|
return errors.WithStack(result.Error)
|
|
}
|
|
|
|
// Delete the specified ai request from the database.
|
|
func (a *AiRequest) Delete(ctx context.Context, id string) error {
|
|
result := GetAiRequestDB(ctx, a.DB).Where("id=?", id).Delete(new(schema.AiRequest))
|
|
return errors.WithStack(result.Error)
|
|
}
|