persistence

package
v0.0.0-...-18998a6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 11, 2020 License: MIT Imports: 8 Imported by: 0

README

Persistence Infrastructure層

Domain層のRepositoryで定義したインタフェースを実装する。
今回はDBとしてFirebase Authentication, Firestoreを使用する。

テンプレート

package persistence

import (
	"context"

	"github.com/calmato/gran/api/sample/internal/domain"
	"github.com/calmato/gran/api/sample/internal/domain/repository"
	"github.com/calmato/gran/api/sample/lib/firebase/authentication"
	"github.com/calmato/gran/api/sample/lib/firebase/firestore"
)

type samplePersistence struct {
	auth      *authentication.Auth
	firestore *firestore.Firestore
}

// SampleCollection - SampleCollection名
const SampleCollection = "samples"

// NewSamplePersistence - SampleRepositoryの生成
func NewSamplePersistence(fa *authentication.Auth, fs *firestore.Firestore) repository.SampleRepository {
	return &samplePersistence{
		auth:      fa,
		firestore: fs,
	}
}

func (r *samplePersistence) Create(ctx context.Context, u *domain.Sample) error {
	uid, err := createSampleInAuth(ctx, r.auth, u)
	if err != nil {
		return err
	}

	u.ID = uid

	if err = setInFirestore(ctx, r.firestore, u); err != nil {
		return err
	}

	return nil
}

func (r *samplePersistence) GetUIDByEmail(ctx context.Context, email string) (string, error) {
	uid, err := getUIDByEmailInAuth(ctx, r.auth, email)
	if err != nil {
		return "", err
	}

	return uid, nil
}

func getUIDByEmailInAuth(ctx context.Context, fa *authentication.Auth, email string) (string, error) {
	return fa.GetUIDByEmail(ctx, email)
}

func createSampleInAuth(ctx context.Context, fa *authentication.Auth, u *domain.Sample) (string, error) {
	return fa.CreateSample(ctx, u.Email, u.Password)
}

func setInFirestore(ctx context.Context, fs *firestore.Firestore, u *domain.Sample) error {
	return fs.Set(ctx, SampleCollection, u.ID, u)
}

Documentation

Index

Constants

View Source
const (
	// UserCollection - UserCollection名
	UserCollection = "users"
)

Variables

This section is empty.

Functions

func NewUserPersistence

func NewUserPersistence(fa *authentication.Auth, fs *firestore.Firestore) repository.UserRepository

NewUserPersistence - UserRepositoryの生成

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL