package dal import ( "context" "gitlab.guxuan.icu/jinshan_community/internal/mods/common/schema" "gitlab.guxuan.icu/jinshan_community/pkg/errors" "gitlab.guxuan.icu/jinshan_community/pkg/util" "gorm.io/gorm" ) // Get web site storage instance func GetWebSiteDB(ctx context.Context, defDB *gorm.DB) *gorm.DB { return util.GetDB(ctx, defDB).Model(new(schema.WebSite)) } // Defining the `WebSite` data access object. type WebSite struct { DB *gorm.DB } // Query web sites from the database based on the provided parameters and options. func (a *WebSite) Query(ctx context.Context, params schema.WebSiteQueryParam, opts ...schema.WebSiteQueryOptions) (*schema.WebSiteQueryResult, error) { var opt schema.WebSiteQueryOptions if len(opts) > 0 { opt = opts[0] } db := GetWebSiteDB(ctx, a.DB) var list schema.WebSites pageResult, err := util.WrapPageQuery(ctx, db, params.PaginationParam, opt.QueryOptions, &list) if err != nil { return nil, errors.WithStack(err) } queryResult := &schema.WebSiteQueryResult{ PageResult: pageResult, Data: list, } return queryResult, nil } // Get the specified web site from the database. func (a *WebSite) Get(ctx context.Context, id string, opts ...schema.WebSiteQueryOptions) (*schema.WebSite, error) { var opt schema.WebSiteQueryOptions if len(opts) > 0 { opt = opts[0] } item := new(schema.WebSite) ok, err := util.FindOne(ctx, GetWebSiteDB(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 web site exists in the database. func (a *WebSite) Exists(ctx context.Context, id string) (bool, error) { ok, err := util.Exists(ctx, GetWebSiteDB(ctx, a.DB).Where("id=?", id)) return ok, errors.WithStack(err) } // Create a new web site. func (a *WebSite) Create(ctx context.Context, item *schema.WebSite) error { result := GetWebSiteDB(ctx, a.DB).Create(item) return errors.WithStack(result.Error) } // Update the specified web site in the database. func (a *WebSite) Update(ctx context.Context, item *schema.WebSite) error { result := GetWebSiteDB(ctx, a.DB).Where("id=?", item.ID).Select("*").Omit("created_at").Updates(item) return errors.WithStack(result.Error) } // Delete the specified web site from the database. func (a *WebSite) Delete(ctx context.Context, id string) error { result := GetWebSiteDB(ctx, a.DB).Where("id=?", id).Delete(new(schema.WebSite)) return errors.WithStack(result.Error) }