jinshan/internal/mods/common/biz/web_site.biz.go
2025-06-19 10:35:26 +08:00

105 lines
2.6 KiB
Go

package biz
import (
"context"
"time"
"gitlab.guxuan.icu/jinshan_community/internal/mods/common/dal"
"gitlab.guxuan.icu/jinshan_community/internal/mods/common/schema"
"gitlab.guxuan.icu/jinshan_community/pkg/errors"
"gitlab.guxuan.icu/jinshan_community/pkg/util"
)
// Defining the `WebSite` business logic.
type WebSite struct {
Trans *util.Trans
WebSiteDAL *dal.WebSite
}
// Query web sites from the data access object based on the provided parameters and options.
func (a *WebSite) Query(ctx context.Context, params schema.WebSiteQueryParam) (*schema.WebSiteQueryResult, error) {
params.Pagination = true
result, err := a.WebSiteDAL.Query(ctx, params, schema.WebSiteQueryOptions{
QueryOptions: util.QueryOptions{
OrderFields: []util.OrderByParam{
{Field: "created_at", Direction: util.DESC},
},
},
})
if err != nil {
return nil, err
}
return result, nil
}
// Get the specified web site from the data access object.
func (a *WebSite) Get(ctx context.Context, id string) (*schema.WebSite, error) {
webSite, err := a.WebSiteDAL.Get(ctx, id)
if err != nil {
return nil, err
} else if webSite == nil {
return nil, errors.NotFound("", "Web site not found")
}
return webSite, nil
}
// Create a new web site in the data access object.
func (a *WebSite) Create(ctx context.Context, formItem *schema.WebSiteForm) (*schema.WebSite, error) {
webSite := &schema.WebSite{}
if err := formItem.FillTo(webSite); err != nil {
return nil, err
}
err := a.Trans.Exec(ctx, func(ctx context.Context) error {
if err := a.WebSiteDAL.Create(ctx, webSite); err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
return webSite, nil
}
// Update the specified web site in the data access object.
func (a *WebSite) Update(ctx context.Context, id string, formItem *schema.WebSiteForm) error {
webSite, err := a.WebSiteDAL.Get(ctx, id)
if err != nil {
return err
} else if webSite == nil {
return errors.NotFound("", "Web site not found")
}
if err := formItem.FillTo(webSite); err != nil {
return err
}
webSite.UpdatedAt = time.Now()
return a.Trans.Exec(ctx, func(ctx context.Context) error {
if err := a.WebSiteDAL.Update(ctx, webSite); err != nil {
return err
}
return nil
})
}
// Delete the specified web site from the data access object.
func (a *WebSite) Delete(ctx context.Context, id string) error {
exists, err := a.WebSiteDAL.Exists(ctx, id)
if err != nil {
return err
} else if !exists {
return errors.NotFound("", "Web site not found")
}
return a.Trans.Exec(ctx, func(ctx context.Context) error {
if err := a.WebSiteDAL.Delete(ctx, id); err != nil {
return err
}
return nil
})
}