39 lines
1.0 KiB
Go
39 lines
1.0 KiB
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
|
|
FindById(id string) (*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
|
|
}
|
|
|
|
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
|
|
}
|