84 lines
2.6 KiB
Go
84 lines
2.6 KiB
Go
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 metting room storage instance
|
|
func GetMettingRoomDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
|
|
return util.GetDB(ctx, defDB).Model(new(schema.MettingRoom))
|
|
}
|
|
|
|
// Defining the `MettingRoom` data access object.
|
|
type MettingRoom struct {
|
|
DB *gorm.DB
|
|
}
|
|
|
|
// Query metting rooms from the database based on the provided parameters and options.
|
|
func (a *MettingRoom) Query(ctx context.Context, params schema.MettingRoomQueryParam, opts ...schema.MettingRoomQueryOptions) (*schema.MettingRoomQueryResult, error) {
|
|
var opt schema.MettingRoomQueryOptions
|
|
if len(opts) > 0 {
|
|
opt = opts[0]
|
|
}
|
|
|
|
db := GetMettingRoomDB(ctx, a.DB)
|
|
|
|
var list schema.MettingRooms
|
|
pageResult, err := util.WrapPageQuery(ctx, db, params.PaginationParam, opt.QueryOptions, &list)
|
|
if err != nil {
|
|
return nil, errors.WithStack(err)
|
|
}
|
|
|
|
queryResult := &schema.MettingRoomQueryResult{
|
|
PageResult: pageResult,
|
|
Data: list,
|
|
}
|
|
return queryResult, nil
|
|
}
|
|
|
|
// Get the specified metting room from the database.
|
|
func (a *MettingRoom) Get(ctx context.Context, id string, opts ...schema.MettingRoomQueryOptions) (*schema.MettingRoom, error) {
|
|
var opt schema.MettingRoomQueryOptions
|
|
if len(opts) > 0 {
|
|
opt = opts[0]
|
|
}
|
|
|
|
item := new(schema.MettingRoom)
|
|
ok, err := util.FindOne(ctx, GetMettingRoomDB(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 metting room exists in the database.
|
|
func (a *MettingRoom) Exists(ctx context.Context, id string) (bool, error) {
|
|
ok, err := util.Exists(ctx, GetMettingRoomDB(ctx, a.DB).Where("id=?", id))
|
|
return ok, errors.WithStack(err)
|
|
}
|
|
|
|
// Create a new metting room.
|
|
func (a *MettingRoom) Create(ctx context.Context, item *schema.MettingRoom) error {
|
|
result := GetMettingRoomDB(ctx, a.DB).Create(item)
|
|
return errors.WithStack(result.Error)
|
|
}
|
|
|
|
// Update the specified metting room in the database.
|
|
func (a *MettingRoom) Update(ctx context.Context, item *schema.MettingRoom) error {
|
|
result := GetMettingRoomDB(ctx, a.DB).Where("id=?", item.ID).Select("*").Omit("created_at").Updates(item)
|
|
return errors.WithStack(result.Error)
|
|
}
|
|
|
|
// Delete the specified metting room from the database.
|
|
func (a *MettingRoom) Delete(ctx context.Context, id string) error {
|
|
result := GetMettingRoomDB(ctx, a.DB).Where("id=?", id).Delete(new(schema.MettingRoom))
|
|
return errors.WithStack(result.Error)
|
|
}
|