Repository: usememos/memos
File: store/db/sqlite/idp.go (Go)

Code before:
	"github.com/pkg/errors"

	"github.com/usememos/memos/store"
)

func (d *DB) CreateIdentityProvider(ctx context.Context, create *store.IdentityProvider) (*store.IdentityProvider, error) {
	var configBytes []byte
	if create.Type == store.IdentityProviderOAuth2Type {
		bytes, err := json.Marshal(create.Config.OAuth2Config)
		if err != nil {
			return nil, err
		}
		configBytes = bytes
	} else {
		return nil, errors.Errorf("unsupported idp type %s", string(create.Type))
	}

	placeholders := []string{"?", "?", "?", "?"}
	fields := []string{"`name`", "`type`", "`identifier_filter`", "`config`"}
	args := []any{create.Name, create.Type, create.IdentifierFilter, string(configBytes)}

	if create.ID != 0 {
		fields, placeholders, args = append(fields, "`id`"), append(placeholders, "?"), append(args, create.ID)
	}

	stmt := "INSERT INTO `idp` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholders, ", ") + ") RETURNING id"
	if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(&create.ID); err != nil {
		return nil, err
	}

	identityProvider := create
	return identityProvider, nil
}

func (d *DB) ListIdentityProviders(ctx context.Context, find *store.FindIdentityProvider) ([]*store.IdentityProvider, error) {
	where, args := []string{"1 = 1"}, []any{}
	if v := find.ID; v != nil {
		where, args = append(where, fmt.Sprintf("id = $%d", len(args)+1)), append(args, *v)
	}

	rows, err := d.db.QueryContext(ctx, `
		SELECT
			id,
			name,
			type,
			identifier_filter,
			config
		FROM idp
		WHERE `+strings.Join(where, " AND ")+` ORDER BY id ASC`,
		args...,
	)

Review comment (suggestion):
```suggestion
	stmt := "INSERT INTO `idp` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholders, ", ") + ") RETURNING `id`"
```

Code after:
	"github.com/pkg/errors"

	"github.com/usememos/memos/store"
)

func (d *DB) CreateIdentityProvider(ctx context.Context, create *store.IdentityProvider) (*store.IdentityProvider, error) {
	var configBytes []byte
	if create.Type == store.IdentityProviderOAuth2Type {
		bytes, err := json.Marshal(create.Config.OAuth2Config)
		if err != nil {
			return nil, err
		}
		configBytes = bytes
	} else {
		return nil, errors.Errorf("unsupported idp type %s", string(create.Type))
	}

	placeholders := []string{"?", "?", "?", "?"}
	fields := []string{"`name`", "`type`", "`identifier_filter`", "`config`"}
	args := []any{create.Name, create.Type, create.IdentifierFilter, string(configBytes)}

	if create.ID != 0 {
		fields, placeholders, args = append(fields, "`id`"), append(placeholders, "?"), append(args, create.ID)
	}

	stmt := "INSERT INTO `idp` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholders, ", ") + ") RETURNING `id`"
	if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(&create.ID); err != nil {
		return nil, err
	}

	identityProvider := create
	return identityProvider, nil
}

func (d *DB) ListIdentityProviders(ctx context.Context, find *store.FindIdentityProvider) ([]*store.IdentityProvider, error) {
	where, args := []string{"1 = 1"}, []any{}
	if v := find.ID; v != nil {
		where, args = append(where, fmt.Sprintf("id = $%d", len(args)+1)), append(args, *v)
	}

	rows, err := d.db.QueryContext(ctx, `
		SELECT
			id,
			name,
			type,
			identifier_filter,
			config
		FROM idp
		WHERE `+strings.Join(where, " AND ")+` ORDER BY id ASC`,
		args...,
	)