illust-data: repositoryに切り出し

This commit is contained in:
usbharu 2024-03-17 15:58:19 +09:00
parent 915a021491
commit 93a3ea8854
Signed by: usbharu
GPG Key ID: 6556747BF94EEBC8
7 changed files with 85 additions and 10 deletions

0
illust-data/config.ini Normal file
View File

View File

@ -8,6 +8,8 @@ require (
)
require (
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible // indirect
github.com/go-ozzo/ozzo-validation/v4 v4.3.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/klauspost/compress v1.13.6 // indirect

View File

@ -1,3 +1,9 @@
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible h1:msy24VGS42fKO9K1vLz82/GeYW1cILu7Nuuj1N3BBkE=
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible/go.mod h1:gsEKFIVnabGBt6mXmxK0MoFy+cZoTJY6mu5Ll3LVLBU=
github.com/go-ozzo/ozzo-validation/v4 v4.3.0 h1:byhDUpfEwjsVQb1vBunvIjh2BHQ9ead57VkAEY4V+Es=
github.com/go-ozzo/ozzo-validation/v4 v4.3.0/go.mod h1:2NKgrcHl3z6cJs+3Oo940FPRiTzuqKbvfrL2RxCj6Ew=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
@ -10,6 +16,9 @@ github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQ
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
@ -63,5 +72,7 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View File

@ -3,22 +3,28 @@ 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 IllustDataServiceImpl struct {
UnimplementedIllustDataServiceServer
mongo.Collection
IllustDataRepository
}
func NewIllustDataServiceImpl(database mongo.Database) *IllustDataServiceImpl {
return &IllustDataServiceImpl{Collection: *database.Collection("illusts")}
func NewIllustDataServiceImpl(repository IllustDataRepository) *IllustDataServiceImpl {
return &IllustDataServiceImpl{
IllustDataRepository: repository,
}
}
func (s *IllustDataServiceImpl) Save(ctx context.Context, req *illust.Illust) (*illust.Illust, error) {
if _, err := s.ReplaceOne(context.TODO(), bson.D{{"_id", req.Id}}, req, options.Replace().SetUpsert(true)); err != nil {
err := req.Validate()
if err != nil {
return nil, err
}
err = s.IllustDataRepository.Save(req)
if err != nil {
return nil, err
}
return req, nil

View File

@ -0,0 +1,28 @@
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
}

View File

@ -0,0 +1,27 @@
package illust
import validation "github.com/go-ozzo/ozzo-validation"
type IllustValidator struct {
}
func (iv *IllustValidator) Validate(i interface{}) error {
if c, ok := i.(validation.Validatable); ok {
return c.Validate()
}
return nil
}
func (i Illust) Validate() error {
return validation.ValidateStruct(
&i,
validation.Field(&i.Id, validation.Required),
validation.Field(&i.Name, validation.Required),
validation.Field(&i.Sha256, validation.Required),
validation.Field(&i.S3Id, validation.Required),
validation.Field(&i.Tags, validation.Required),
validation.Field(&i.Originals, validation.Required),
validation.Field(&i.Characters, validation.Required),
validation.Field(&i.Description, validation.Required),
)
}

View File

@ -22,7 +22,7 @@ func main() {
return
}
port := load.Section("gRPC").Key("port").MustInt(50051)
mongoUrl := load.Section("mongodb").Key("url").MustString("mongodb://localhost:20071")
mongoUrl := load.Section("mongodb").Key("url").MustString("mongodb://localhost:27017")
listen, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
@ -33,12 +33,13 @@ func main() {
connect, err := mongo.Connect(context.Background(), options.Client().ApplyURI(mongoUrl))
if err != nil {
log.Fatal(err)
return
}
database := connect.Database("")
database := connect.Database("illust-data")
data.RegisterIllustDataServiceServer(server, data.NewIllustDataServiceImpl(*database))
data.RegisterIllustDataServiceServer(server, data.NewIllustDataServiceImpl(data.NewMongodbIllustDataRepository(*database)))
reflection.Register(server)