package repository

import (
	"context"

	"github.com/tidbcloud/auth-server/internal/entity"
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
)

func CreateOauth2CodeGrant(ctx context.Context, tx *gorm.DB, obj *entity.Oauth2CodeGrant) error {
	return obj.Create(tx)
}

func GetOauth2CodeGrantWithLock(ctx context.Context, tx *gorm.DB, code string) (*entity.Oauth2CodeGrant, error) {
	var obj entity.Oauth2CodeGrant
	err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).Where("code = ?", code).First(&obj).Error
	return &obj, err
}

func DeleteOauth2CodeGrant(ctx context.Context, tx *gorm.DB, obj *entity.Oauth2CodeGrant) error {
	return tx.Delete(obj).Error
}
