unos/illust-data/illust/data/illust_data_repository.go

29 lines
748 B
Go

package data
import (
"context"
"git.usbharu.dev/usbharu/unos/illust"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type IllustDataRepository interface {
Save(illust *illust.Illust) error
}
type MongodbIllustDataRepository struct {
mongo.Collection
}
func NewMongodbIllustDataRepository(database mongo.Database) *MongodbIllustDataRepository {
return &MongodbIllustDataRepository{Collection: *database.Collection("illusts")}
}
func (m *MongodbIllustDataRepository) Save(illust *illust.Illust) error {
if _, err := m.ReplaceOne(context.Background(), bson.D{{"_id", illust.Id}}, illust, options.Replace().SetUpsert(true)); err != nil {
return err
}
return nil
}