From 93a3ea8854507c81e390afe1826158c2a79bba03 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sun, 17 Mar 2024 15:58:19 +0900 Subject: [PATCH] =?UTF-8?q?illust-data:=20repository=E3=81=AB=E5=88=87?= =?UTF-8?q?=E3=82=8A=E5=87=BA=E3=81=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- illust-data/config.ini | 0 illust-data/go.mod | 2 ++ illust-data/go.sum | 11 ++++++++ .../illust/data/illust_data.service.go | 20 ++++++++----- .../illust/data/illust_data_repository.go | 28 +++++++++++++++++++ illust-data/illust/illust_validator.go | 27 ++++++++++++++++++ illust-data/main.go | 7 +++-- 7 files changed, 85 insertions(+), 10 deletions(-) create mode 100644 illust-data/config.ini create mode 100644 illust-data/illust/data/illust_data_repository.go create mode 100644 illust-data/illust/illust_validator.go diff --git a/illust-data/config.ini b/illust-data/config.ini new file mode 100644 index 0000000..e69de29 diff --git a/illust-data/go.mod b/illust-data/go.mod index b7a5cc2..c58ac47 100644 --- a/illust-data/go.mod +++ b/illust-data/go.mod @@ -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 diff --git a/illust-data/go.sum b/illust-data/go.sum index 8b3db96..3abe03b 100644 --- a/illust-data/go.sum +++ b/illust-data/go.sum @@ -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= diff --git a/illust-data/illust/data/illust_data.service.go b/illust-data/illust/data/illust_data.service.go index 6b54ea9..3127bdd 100644 --- a/illust-data/illust/data/illust_data.service.go +++ b/illust-data/illust/data/illust_data.service.go @@ -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 diff --git a/illust-data/illust/data/illust_data_repository.go b/illust-data/illust/data/illust_data_repository.go new file mode 100644 index 0000000..98c4846 --- /dev/null +++ b/illust-data/illust/data/illust_data_repository.go @@ -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 +} diff --git a/illust-data/illust/illust_validator.go b/illust-data/illust/illust_validator.go new file mode 100644 index 0000000..fd8e94d --- /dev/null +++ b/illust-data/illust/illust_validator.go @@ -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), + ) +} diff --git a/illust-data/main.go b/illust-data/main.go index 3cc31a5..3dd10bd 100644 --- a/illust-data/main.go +++ b/illust-data/main.go @@ -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)