refactor: clean code

This commit is contained in:
Fu Diwei
2025-01-16 20:27:30 +08:00
parent dea4106569
commit 8ecb71fb55
39 changed files with 440 additions and 258 deletions

View File

@@ -4,9 +4,12 @@ import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/pocketbase/pocketbase/models"
"github.com/usual2970/certimate/internal/app"
"github.com/usual2970/certimate/internal/domain"
"github.com/usual2970/certimate/internal/pkg/utils/types"
)
type AccessRepository struct{}
@@ -15,7 +18,7 @@ func NewAccessRepository() *AccessRepository {
return &AccessRepository{}
}
func (a *AccessRepository) GetById(ctx context.Context, id string) (*domain.Access, error) {
func (r *AccessRepository) GetById(ctx context.Context, id string) (*domain.Access, error) {
record, err := app.GetApp().Dao().FindRecordById("access", id)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
@@ -24,6 +27,18 @@ func (a *AccessRepository) GetById(ctx context.Context, id string) (*domain.Acce
return nil, err
}
if !types.IsNil(record.Get("deleted")) {
return nil, domain.ErrRecordNotFound
}
return r.castRecordToModel(record)
}
func (r *AccessRepository) castRecordToModel(record *models.Record) (*domain.Access, error) {
if record == nil {
return nil, fmt.Errorf("record is nil")
}
access := &domain.Access{
Meta: domain.Meta{
Id: record.GetId(),