follow sql schema
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package identity
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"mediarr/database"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
db database.DatabaseClient
|
||||
}
|
||||
|
||||
func NewService(db database.DatabaseClient) *Service {
|
||||
return &Service{db: db}
|
||||
}
|
||||
|
||||
func (s *Service) withTx(ctx context.Context, fn func(*database.Queries) error) error {
|
||||
tx, err := s.db.WriteDB.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
_ = tx.Rollback()
|
||||
}()
|
||||
|
||||
if err := fn(s.db.WriteQueries.WithTx(tx)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func isUniqueViolation(err error) bool {
|
||||
return err != nil && strings.Contains(strings.ToLower(err.Error()), "unique constraint failed")
|
||||
}
|
||||
|
||||
func (s *Service) groupByName(ctx context.Context, name string) (database.Principal, error) {
|
||||
group, err := s.db.Queries.GetPrincipalByNameAndKind(ctx, database.GetPrincipalByNameAndKindParams{
|
||||
Kind: KindGroup,
|
||||
Name: name,
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return database.Principal{}, fmt.Errorf("%w: %s", ErrGroupNotFound, name)
|
||||
}
|
||||
return database.Principal{}, err
|
||||
}
|
||||
|
||||
return group, nil
|
||||
}
|
||||
Reference in New Issue
Block a user