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