105 lines
2.7 KiB
Go
105 lines
2.7 KiB
Go
package biz
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"gitlab.guxuan.icu/jinshan_community/internal/mods/ai/dal"
|
|
"gitlab.guxuan.icu/jinshan_community/internal/mods/ai/schema"
|
|
"gitlab.guxuan.icu/jinshan_community/pkg/errors"
|
|
"gitlab.guxuan.icu/jinshan_community/pkg/util"
|
|
)
|
|
|
|
// Defining the `AiRequest` business logic.
|
|
type AiRequest struct {
|
|
Trans *util.Trans
|
|
AiRequestDAL *dal.AiRequest
|
|
}
|
|
|
|
// Query ai requests from the data access object based on the provided parameters and options.
|
|
func (a *AiRequest) Query(ctx context.Context, params schema.AiRequestQueryParam) (*schema.AiRequestQueryResult, error) {
|
|
params.Pagination = true
|
|
|
|
result, err := a.AiRequestDAL.Query(ctx, params, schema.AiRequestQueryOptions{
|
|
QueryOptions: util.QueryOptions{
|
|
OrderFields: []util.OrderByParam{
|
|
{Field: "created_at", Direction: util.DESC},
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// Get the specified ai request from the data access object.
|
|
func (a *AiRequest) Get(ctx context.Context, id string) (*schema.AiRequest, error) {
|
|
aiRequest, err := a.AiRequestDAL.Get(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if aiRequest == nil {
|
|
return nil, errors.NotFound("", "Ai request not found")
|
|
}
|
|
return aiRequest, nil
|
|
}
|
|
|
|
// Create a new ai request in the data access object.
|
|
func (a *AiRequest) Create(ctx context.Context, formItem *schema.AiRequestForm) (*schema.AiRequest, error) {
|
|
aiRequest := &schema.AiRequest{}
|
|
|
|
if err := formItem.FillTo(aiRequest); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err := a.Trans.Exec(ctx, func(ctx context.Context) error {
|
|
if err := a.AiRequestDAL.Create(ctx, aiRequest); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return aiRequest, nil
|
|
}
|
|
|
|
// Update the specified ai request in the data access object.
|
|
func (a *AiRequest) Update(ctx context.Context, id string, formItem *schema.AiRequestForm) error {
|
|
aiRequest, err := a.AiRequestDAL.Get(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
} else if aiRequest == nil {
|
|
return errors.NotFound("", "Ai request not found")
|
|
}
|
|
|
|
if err := formItem.FillTo(aiRequest); err != nil {
|
|
return err
|
|
}
|
|
aiRequest.UpdatedAt = time.Now()
|
|
|
|
return a.Trans.Exec(ctx, func(ctx context.Context) error {
|
|
if err := a.AiRequestDAL.Update(ctx, aiRequest); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// Delete the specified ai request from the data access object.
|
|
func (a *AiRequest) Delete(ctx context.Context, id string) error {
|
|
exists, err := a.AiRequestDAL.Exists(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
} else if !exists {
|
|
return errors.NotFound("", "Ai request not found")
|
|
}
|
|
|
|
return a.Trans.Exec(ctx, func(ctx context.Context) error {
|
|
if err := a.AiRequestDAL.Delete(ctx, id); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
}
|