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

39 lines
1.0 KiB
Go
Raw Normal View History

2024-03-17 06:58:19 +00:00
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
2024-03-25 02:32:18 +00:00
FindById(id string) (*illust.Illust, error)
2024-03-17 06:58:19 +00:00
}
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
}
2024-03-25 02:32:18 +00:00
func (m *MongodbIllustDataRepository) FindById(id string) (*illust.Illust, error) {
var i = &illust.Illust{}
err := m.FindOne(context.Background(), bson.D{{"_id", id}}).Decode(i)
if err != nil {
return nil, err
}
return i, nil
}