diff options
author | Alexander Neonxp Kiryukhin <i@neonxp.ru> | 2024-10-12 02:52:22 +0300 |
---|---|---|
committer | Alexander Neonxp Kiryukhin <i@neonxp.ru> | 2024-10-12 02:53:52 +0300 |
commit | d05ea66f4bbcf0cc5c8908f3435c68de1b070fa1 (patch) | |
tree | 7c7a769206646f2b81a0eda0680f0be5033a4197 /pkg/service/crud/crud.go |
Начальная версияv0.0.1
Diffstat (limited to 'pkg/service/crud/crud.go')
-rw-r--r-- | pkg/service/crud/crud.go | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/pkg/service/crud/crud.go b/pkg/service/crud/crud.go new file mode 100644 index 0000000..799eacb --- /dev/null +++ b/pkg/service/crud/crud.go @@ -0,0 +1,87 @@ +package crud + +import ( + "context" + "errors" + + "github.com/uptrace/bun" + "github.com/uptrace/bun/driver/pgdriver" +) + +var ErrRecordAlreadyExists = errors.New("record already exists") + +type Service[T any] struct { + db *bun.DB +} + +func NewService[T any](db *bun.DB) *Service[T] { + return &Service[T]{ + db: db, + } +} + +func (s *Service[T]) Insert(ctx context.Context, model *T) error { + if _, err := s.db.NewInsert().Model(model).Returning("*").Exec(ctx); err != nil { + pqErr := pgdriver.Error{} + if errors.As(err, &pqErr) { + if pqErr.Field('C') == "23505" { + return ErrRecordAlreadyExists + } + } + + return err + } + + return nil +} + +func (s *Service[T]) UpdatePk(ctx context.Context, model *T, columns []string) error { + if _, err := s.db.NewUpdate().Model(model).WherePK(columns...).Column(columns...).Exec(ctx); err != nil { + pqErr := pgdriver.Error{} + if errors.As(err, &pqErr) { + if pqErr.Field('C') == "23505" { + return ErrRecordAlreadyExists + } + } + + return err + } + + return nil +} + +//nolint:revive +func (s *Service[T]) Update(ctx context.Context, model *T, columns []string, query string, args ...any) error { + if _, err := s.db.NewUpdate().Model(model).Where(query, args...).Column(columns...).Exec(ctx); err != nil { + pqErr := pgdriver.Error{} + if errors.As(err, &pqErr) { + if pqErr.Field('C') == "23505" { + return ErrRecordAlreadyExists + } + } + + return err + } + + return nil +} + +func (s *Service[T]) FindOne(ctx context.Context, query string, args ...any) (*T, error) { + m := new(T) + + return m, s.db.NewSelect().Model(m).Where(query, args...).Scan(ctx, m) +} + +//nolint:revive +func (s *Service[T]) Find(ctx context.Context, query string, args ...any) ([]T, int, error) { + m := make([]T, 0) + c, err := s.db.NewSelect().Model(m).Where(query, args...).ScanAndCount(ctx, &m) + + return m, c, err +} + +func (s *Service[T]) Delete(ctx context.Context, query string, args ...any) error { + _, err := s.db.NewDelete().Model((*T)(nil)).Where(query, args...).Exec(ctx) + + return err +} |